3_Variables and Datatypes
3_Variables and Datatypes
Learning objective
• What is a Variable?
• Declaration of variable
• Variable assignment
• Constants
• Data types in Python
• Checking Data type
• Mutable and Immutable Collections
What is a variable?
• Generally, while doing programming in any programming language,
you have to utilise different variables to store different data.
• Simultaneous Assignment
• A very cool feature of Python is that it allows for simultaneous assignment.
• The syntax is as follows:
• var_name1, var_name2 = value1, value2
Assigning values to a variable
• For example :
Age = 20 # An integer assignment
Percentage = 71.58 # A floating point
Name = “Rossum" # A string
print (Age)
print (Percentage)
print (Name)
Here 20, 71.58 and Rossum are the values assigned to Age, Percentage
and Name variable respectively.
Multiple Assignment
• Python allows you to assign a single value to several variables
simultaneously.
x = y = z = 10
• Here, an integer object is created with the value 10, and all three
variables are assigned to the same memory location. You can also
assign multiple objects to multiple variables. For example:
a, b, c = 1, 2, "john"
• Here, two integer objects with values 1 and 2 are assigned to variables
a and b respectively, and one string object with the value "john" is
assigned to the variable c.
Constants
• In programming, a constant is like a variable , but, unlike
variables, constants are not variable, they are constant.
PI = 3.141592653589793
RED = "FF0000"
Data Types in Python
• In Python programming, objects have different data types.
• The data type determines both what an object can do and what
can be done to it.
• Example: x = True
set3 = frozenset([1,”arif”])
print(set3)
print(type(set3))
set3.append(40)
set3[0]=90
None Type
• Represents the absence of a value or a null value
A=None
How to check the data type?
• Use the function type()
• Display which class a variable or a value belongs to
A=100
print(type(A))
B=100.3433
print(type(B))
Benefits of Mutable Collections
• List: You can modify elements, add new elements, or remove
elements.
• Set: You can add or remove elements from a set, though sets are
unordered.
• Dictionary: You can add, remove, or modify key-value pairs.