Variables are used to store data, they take memory space based on the type of value we assigning to them. Creating variables in Python is simple, you just have write the variable name on the left side of = and the value on the right side, as shown below. You do not have to explicitly mention the type of the variable, python infer the type based on the value we are assigning.
num = 100 #num is of type int str = "Chaitanya" #str is of type string
Variable name – Identifiers
Variable name is known as identifier. There are few rules that you have to follow while naming the variables in Python.
1. The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
4. Variable name is case sensitive in Python which means
NUM are two different variables in python.
Python Variable Example
num = 100 str = "BeginnersBook" print(num) print(str)
Python multiple assignment
We can assign multiple variables in a single statement like this in Python.
x = y = z = 99 print(x) print(y) print(z)
99 99 99
Another example of multiple assignment
a, b, c = 5, 6, 7 print(a) print(b) print(c)
Plus and concatenation operation on the variables
x = 10 y = 20 print(x + y) p = "Hello" q = "World" print(p + " " + q)
However if you try to use the + operator with variable
p then you will get the following error.
unsupported operand type(s) for +: 'int' and 'str'
A data type defines the type of data, for example 123 is an integer data while “hello” is a String type of data. The data types in Python are divided in two categories:
1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed
Immutable data types in Python are:
Mutable data types in Python are:
Leave a Reply