Python – Learn from Basic to Advanced
Python: Variables
Variable:
-
Variable is an identifier whose value can change. For example, variable age can have a different value for a different person.
-
Variable name should be unique in a program.
-
Variable declaration is implicit in Python, means variables are automatically declared and defined when they are assigned a value the first time.
-
Variables must always be assigned values before they are used in the program, otherwise, it will lead to an error.
-
Wherever a variable name occurs in the program, the interpreter replaces it with the value of that particular variable.
Value of a variable can be a string (for example, ‘A’, ‘Excellent’), number (for example 98.5) or any combination of alphanumeric (alphabets and numbers for example ‘B10’) characters.
In Python, we can use an assignment statement to create new variables and assign specific values to them.
The basic assignment statement has this form:
<variable> = <expr>
Here, <variable> is an identifier and <expr> is an expression.
For Example:
Class = 12
Remark = "Keep it up!"
Percentage = 98.5
# Variable created of numeric (integer) type
# Variable created of string type
# Variable created of numeric (floating point) type
There are some rules you have to follow for naming variables:
​
-
A variable starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
-
Python does not allow special characters
-
Variable must not be a keyword of Python.
-
No blank spaces are allowed between the variable name.
-
Python is a case sensitive programming language.
Thus, Sname and sname are two different variables in Python.
The following variable names are valid:
roll_no
salary
sep98_score
index_age
percentage
amount
dob
GrossPay
Cust_no
D_o_j
__my_name
name_23
a1
b2_c3
Some invalid variable names which produce syntax errors are as follows:​​
>>> 98Sep_sco
>>> Your Age
>>> while
>>> myname@
SyntaxError: invalid syntax because variable start with a digit
SyntaxError: invalid syntax because variable contains space
SyntaxError: invalid syntax because variable is a reserve word
SyntaxError: invalid syntax because variable contains a special character
Variables can also be assigned values in different forms. These are
1. Multiple Assignments:
-
Assigning same value to multiple variables:
x = y = z = 100
-
It will assign value 100 to all three variables x, y and z.
-
Assigning multiple value to multiple variable in single line
p, q, r = 10, 20, 30
-
It will assign the value order wise that is value 10 assign to variable p, value 20 assign to variable q and value 30 assign to variable r.
2. Assigning an expression.
An expression can be assigned to a variable. For example,
x = 10 # x is an L-value
x = 3.9 * x * (1 - x) # (3.9 * x * (1 – x)) is an R-value
print(x)
Celsius = 40
Fahrenheit = 9.0 / 5.0 * Celsius + 32
print(Celsius, Fahrenheit)
OUTPUT:
-351.0
40 104.0
3. A variable can be assigned many times.
​
It always retains the value of the most recent assignment. For example,
>>> Var1 = 7 # Var1 is an L-value
>>> Var1
7 # OUTPUT
>>> Var1 = 0 # L-value can be modified
>>> Var1
0 # OUTPUT
>>> Var1 = Var1 + 10 # (Var1 + 10) is an R-value
>>> Var1
10 # OUTPUT
Notice that the above lines assigned many values to the same variable and produces different results. But the last assigned value is the correct value for Var1. The last assignment shows how the current value of a variable can be used to update its value.
4. Using sequences to assign multiple values at once.
​
Using the assignment operator, we can assign sequences like tuple and list values into multiple values. For example,
>>> days = (31,28,31,30,31,30,31,31,30,31,30,31)
>>> Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec) = days
>>> Jan
31 # OUTPUT
>>> Feb
28 # OUTPUT
Concepts of L-Value and R-Value
-
Lvalue refers to object to which you can assign value. It refers to memory location. It can appear LHS or RHS of assignment
-
Rvalue refers to the value we assign to any variable. It can appear on RHS of assignment
For Example:
# Num1 is an L-value
num1 = 100
# Num1 is an L-value
num2 = 200
#(Num1 +Num2) is an R-value
result = num1 + num2
print(result)
Example: Write a Python program to find the area of a triangle given that its base is 20 units and height is 40 units.
base = 20
height = 40
area = 0.5 * base * height
print("The Area is: ", area)
OUTPUT:
The Area is: 400
Python Basic Tutorial
1. Introduction to Python
2. Python: Programming fundamentals​
3. Python: Variables
-
Variable
-
Rules to follow for naming a variable
-
Multiple Assignment
-
Assigning an expression
-
A variable can be assigned many times
-
Using sequences to assign multiple values at once
-
Concepts of L-Value and R-Value
4. Python: input ( ) and print ( ) function
5. Formatting Data with print() Statement
​​
​
From the above declarations, days is a tuple of twelve elements, and (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) is a tuple of three variables. Assigning one to the other assigns each of the values of days to each of the variables (Jan, Feb, ….) in order.
OUTPUT:
300