Python Variables
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 x
stores 10
, y
stores a string 'Hello'
, and z
stores a boolean value True
.
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
Objects
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 10
.
In the same way, amount
is an object of the float
class, greet
is an object of the str
class,
isActive
is an object of the bool
class.
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'>
The +
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
Object's Identity
Each object in Python has an id. It is the object's address in memory represented by an integer value.
The 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.
Naming Conventions
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,
myVar
,MyVar
,_myVar
,MyVar123
are valid variable names, butm*var
,my-var
,1myVar
are invalid variable names. -
Variable names in Python are case sensitive. So,
NAME
,name
,nAME
, andnAmE
are treated as different variable names. - Variable names cannot be a reserved keywords in Python.