Variables and Expressions in Python
In this tutorial, I am going to discuss about variables and expressions in python.
Variables in Python
A variable in python is a named location which refers to a value and these values can be used and processed during program run. A variable is not created in python until some value is assigned to it.
For instance, below given statement creates two variables users and user_name.
users = 6 user_name = 'user@1'
Python variables are created by assigning the value of desired type to them. Like in the above statements a numeric value is assigned to 'users' making it a numeric variable and a string is assigned to 'user_name' making it a string variable.
Lvalues and Rvalues
Lvalues are the objects to which you can assign a value or expression. Lvalues can come on LHS or RHS side of an assignment statement.
Rvalues are the literals and expressions that are assigned to lvalues. Rvalues can come on RHS of an assignment statement.
a = 20 #here a is Lvalue and 20 is Rvalue b = a + 10 #here b and a are Lvalue and 10 is Rvalue.
Python is a versatile language, there are many different ways in which you can assign values to variables.
1. Same value to Multiple variables :
Multiple variables can be assigned same value in a single statement.
a = b = c = 50 print("a =",a," b =",b," c =",c)
a = 50 b = 50 c = 50
This statement assigns the value 50 to all three variables a,b and c.
2. Multiple values to Multiple variables :
In python, you can assign multiple values to multiple variables in a single statement.
x, y, z = 10, 11, 12 print("x =",x," y =",y," z =",z)
x = 10 y = 11 z = 12
The above statement assigns the value order wise i.e. first variable is given first value, second variable is given second value and so on.
A variable pointing to a value of a certain type, can be made to point to a value/object of different type. This is called dynamic typing.
x = 10 print("x =",x) x = "Hello" print("x =",x)
x = 10 x = Hello
So, we can think of a python variable as labels associated with objects(or values). With dynamic typing python makes the label refer to new object(or value).
Although python makes it easier for changing types of a variable, we should be more cautious while using such variables in our program as it can result in error while performing operations on the variable as some operations are only possible for certain data types. Let's have a look at such an example :
x = 10 y = 0 y = x / 2 x = 'day' y = x / 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)
in 3 y = x / 2 4 x = 'day' ----> 5 y = x / 5 TypeError: unsupported operand type(s) for /: 'str' and 'int'
As we can see using a different data type with incompatible operators can result in errors. So programmers need to make sure that variables of right type should be used in correct expressions.
Expressions in Python
In python, an expression is any valid combination of variables, constants, literals and operators.
It performs one or more operations with one or more operators, variables and literals.
The type of operators and operands in the expression determines the type of expression. There are different types of expressions in Python like : arithmetic expressions, string expressions, relational expressions, logical expressions and compound expressions.
Let's learn about these one by one.
- Arithmetic Expressions : Arithmetic expressions involve numbers (integers, floating-point numbers or complex numbers) and arithmetic operators.
For example : a = 2+4, b = 8.4 - 4.5 etc.
- Relational Expressions : An expression which have literals and/or variables of valid type and relational operators is a relational expression.
For example : a > b, c <= d, x == y etc.
- Logical Expressions : An expression which have literals and/or variables of valid type and logical operators is a logical expression.
For example : a and b, c or d, x and not y etc.
- String Expresions : In python, we also have two operators + and * which when used with string operands form string expressions.
For example: "for"+"them" becomes "forthem" (concatenation operation) and "hi"*2 becomes "hihi"(replication operation).
- Compound Expressions : If an expression involves multiple types of operators then it is called a compound expression.
For example a+b < c**d is a compound expression as it involves arithmetic operators as well as relational operators.
You will learn about expression evaluation when we will discuss about different types of operators avaiable in python language.
In today's tutorial, we have discussed about Variables in python, assigning values to variable in different ways, dynamic typing, Expressions in python and their types.
If you have any questions please comment below and also share your views and suggestions in the comment box.