In Python, a variable is a container that stores a value. In other words, variable is the name given to a value, so that it becomes easy to refer a value later on.
Unlike C# or Java, it's not necessary to explicitly define a variable in Python before using it.
Just assign a value to a variable using the
= operator e.g.
variable_name = value. That's it.
The following creates a variable with the integer value.
num = 10
In the above example, we declared a variable named
num and assigned an integer value 10 to it.
Use the built-in print() function to display the value of a variable on the console or IDLE or REPL.
In the same way, the following declares variables with different types of values.
num = 10 #integer variable amount = 78.50 #float variable greet='Hello World' #string variable isActive = True #boolean variable
Multiple Variables Assignment
You can declare multiple variables and assign values to each variable in a single statement, as shown below.
x, y, z = 10, 20, 30 print(x, y, z) #10 20 30
In the above example, the first int value
10 will be assigned to the first variable x, the second value to the second variable y, and the third value to the third variable z. Assignment of values to variables must be in the same order in they declared.
You can also declare different types of values to variables in a single statement separated by a comma, as shown below.
x, y, z = 10, 'Hello', True print(x, y, z) #10 Hello True
Above, the variable
y stores a string
z stores a boolean value
The type of variables are based on the types of assigned value.
Assign a value to each individual variable separated by a comma will throw a syntax error, as shown below.
x = 10, y = 'Hello', z = True
Variables in Python are objects. A variable is an object of a class based on the value it stores. Use the type() function to get the class name (type) of a variable.
num = 10 type(num) #<class 'int'> amount = 78.50 type(amount) #<class 'float'> greet='Hello World' type(greet) #<class 'str'> isActive = True type(isActive) #<class 'bool'>
In the above example,
num is an object of the
int class that contains integre value
In the same way,
amount is an object of the
greet is an object of the
isActive is an object of the
Unlike other programming languages like C# or Java, Python is a dynamically-typed language, which means you don't need to declare a type of a variable. The type will be assigned dynamically based on the assigned value.
x = 100 print(type(x)) #<class 'int'> x = 'Hello World!' print(type(x)) #<class 'str'>
+ operator sums up two int variables, whereas it concatenates two string type variables.
x = 100 print(x + 10) #110 x = 'Hello' print(x + ' Python') #Hello Python
Each object in Python has an id. It is the object's address in memory represented by an integer value.
id() function returns the id of the specified object where it is stored, as shown below.
x = 100 id(x) greet='Hello' id(greet)
Variables with the same value will have the same id.
x = 100 y = x; z = 100 print(id(x), id(y), id(z))
Thus, Python optimize memory usage by not creating separate objects if they point to same value.
Any suitable identifier can be used as a name of a variable, based on the following rules:
- The name of the variable should start with either an alphabet letter (lower or upper case) or an underscore (_), but it cannot start with a digit.
- More than one alpha-numeric characters or underscores may follow.
- The variable name can consist of alphabet letter(s), number(s) and underscore(s) only. For example,
MyVar123are valid variable names, but
1myVarare invalid variable names.
Variable names in Python are case sensitive. So,
nAmEare treated as different variable names.
- Variable names cannot be a reserved keywords in Python.