Python Data Types and Types of Variables in Python

Hey folks, in this module, we are going to discuss Python Data types and types of variables in Python in detail. These are the fundamental concepts that belong to the foundation of any programming language.

So, let’s start reading this module.

Python Data Types and Types of Variables in Python

Python is one of the easiest and sought out languages in today’s era and Programmers and developers only want to focus on the implementation part rather than spend their time writing complex programs. This is why Python becomes the easiest language and also provides an ease of readability.

python data types

Python Variables

Variables are like the container which contains or holds some value in it. It acts like a storage area that holds or stores value.

Variable as the name suggests that it contains a value which can vary. So, that means you can change the value of the variable as per requirement in the program.

It is also known as reserved memory locations in which the value is stored. This tells us that whenever you create a variable, some memory is assigned to it.

num = 100 #variable num created
print(num) #printing the value to the variable

#output
#100

How to declare a variable in Python?

In python, as soon as you have assigned a value to a variable, then that means you have declared a variable, there are no additional commands for declaring a variable in python.

num = 20
#The variable named num has declared and the value 20 is assigned to it.

Since there is no additional command for declaring a variable, there are some rules that have to be followed while declaring a variable.

  • Variables are case-sensitive in python which means “Num” and “num” are different variables.
  • Variable names can only contain underscores and alphanumeric characters.
  • A variable name cannot start with a number. It can only start with an underscore or a character.
  • A variable name cannot contain any special characters.
  • Reserved words cannot be given as the variable name.

So, these are some of the rules that have to be remembered while declaring a variable. Some examples of variable declaration are.

name = "use my notes"
num = 20
print(name)
print(num)

#output
#use my notes
#20

In the above code, we have created two variables named name and num and assigned a value to them, then printed the same.

Multiple Assignment in Python

In Python, we can assign single values to multiple variables at the same time.

num1 = num2 = num3 = 10
print(num1)
print(num2)
print(num3)

#output
#10
#10
#10

In the above code, we have assigned a single value to the multiple variables 10 is assigned to variables num1, num2, and num3 and then printed the same. Let’s see one more example to make the concept clearer.

num1, num2 = 10, 20
print(num1, num2)

#output
#10 20

In the above code, we have assigned multiple values to multiple variables in the same line and printed the value.

Python Data Types

Since then, we have declared a variable but we didn’t mention their type, but yes values have a type. Every value has a type in Python.

Data types tell which type of value a variable is holding. In Python, there are six data types and that is.

  1. Numbers
  2. Strings
  3. List
  4. Set
  5. Tuples
  6. Dictionary

Let’s see a little detail about each of them.

Numerical Data Types in Python

Numerical data types are responsible for holding numerical values, i.e., integers, floating-point numbers, complex numbers, and Boolean. Let’s see an example.

num = 10 #variable num holding integer value
num1 = 20.11 #variable num1 holding decimal or floating-point value
num2 = 1 + 2j #variable num2 holding complex number
num3 = True #variable num3 holding Boolean value
print(num)
print(num1)
print(num2)
print(num3)

#output
#10
#20.11
#(1+2j)
#True

In the above code, we have declared 4 variables, assigned the four different types of numerical values, and printed the same.

If we want to know the type of value that a particular variable is holding, we can do that using the “type()” function.

num = 10 #variable containing integer value
num1 = 20.11 #variable containing decimal value
print(type(num))
print(type(num1))

#output
#<class 'int'>
#<class 'float'>

In the above code, we can easily see that we got the type of value that a variable is holding, that variable num was holding integer value and variable num1 was holding floating-point value.

String’s data types in Python

As there is no Character data type, a Character is considered as a String in Python. The string is used for representing Unicode character values.

name = "use my notes"
print(name)
print(name[4]) #accessing the value stored at index 4

#output
#use my notes
#m

In the above code, we have declared a variable name and assigned a value to it, and then have printed the value as it is and also using the index number.

List data types in Python

List in Python contains an ordered and changeable collection of items that belongs to different data types. It allows duplicate values also. To declare the list, we use the square brackets. Let’s see an example.

nameList = ["use my notes", "python", 20, 10.12, "programming"] #list created
print(nameList) #printing the list item
print(nameList[3]) #printing the item stored at index 3
print(nameList[0]) #printing the item stored at index 0

#output
#['use my notes', 'python', 20, 10.12, 'programming']
#10.12
#use my notes

In the above code, we have declared a list name nameList and have defined different values to it and then printed the same, also using indexing to access the particular item from the list.

Tuple data types in Python

In Python, Tuple is declared using the round brackets. It contains a collection of items which is unchangeable or immutable. We can access the tuple values or items using the index number. Let’s see an example.

nameTuple = ("use my notes", "python", 20, 10.12, "programming") #tuple created
print(nameTuple) #printing the tuple items
print(nameTuple[3]) #accessing the item stored at index 3
print(nameTuple[0]) #accessing the item stored at index 0

#output
#('use my notes', 'python', 20, 10.12, 'programming')
#10.12
#use my notes

In the above code, we have created the tuple, assigned the values to it, and then printed the items of it, and also accessed the particular items using the index number.

Sets data types in Python

In Python, the set contains an unordered collection of items, which also does not have an index. We can declare the set using the curly brackets.

A set contains only unique values, it does not contain duplicate values in it.

nameSet = {"use my notes", "python", 20, 10.12, "programming"}
print(nameSet)

#output
#{10.12, 'use my notes', 20, 'python', 'programming'}

In the above code, we have created a set, assigned a value to it, and printed the same.

Dictionary data types in Python

A dictionary in Python is just like an array, it contains a key pair. It is unordered and unchangeable. We can access the elements of the dictionary by using the key. We can declare the dictionary by using the curly brackets.

nameDictionary = {'use my notes' : 'Programming',
'python' : 'easy',
'Machine Learning' : 'Pytorch'} #dictionary created
print(nameDictionary) #printing the items of the dictionary
print(nameDictionary['use my notes']) #accessing the elements using a keys

#output
#{'use my notes': 'programming', 'python': 'easy', 'Machine Learning': 'Pytorch'}
#Programming

In the above code, we have declared a dictionary and assigned a value and then printed the same and also accessed the value using keys.

I hope Python Data Types and Types of Variables in Python module was very much interesting and you must gain a lot of knowledge from the same and also must be excited to see more modules so that you can gear up yourself in Python programming. So until then, stay connected with us. Keep Learning, Keep reading, and Happy Coding.