Data Science – Python Variables
5. PYTHON - VARIABLES
Contents
1. Variable ......................................................................................................... 2
2. Purpose of the variable ................................................................................. 2
3. Properties of variable .................................................................................... 2
4. Naming convention to a variable .................................................................. 2
5. Creating a variable ........................................................................................ 2
6. Creating multiple variables in single line ...................................................... 5
7. Single value for multiple variables ................................................................ 6
8. Variable re-initialization ................................................................................ 7
1|Page 5.PYTHON-VARIABLES
Data Science – Python Variables
5. PYTHON - VARIABLES
1. Variable
✓ Variable is a reserved memory location to store values.
2. Purpose of the variable
✓ The purpose of variable is to represent the data
✓ Data means a collection of facts
✓ Facts can be,
o Alphabets.
o Numbers.
o Alphanumeric
o Symbols
3. Properties of variable
✓ Every variable has its own properties,
✓ Every variable can contain,
o Name
o Type
o Value
4. Naming convention to a variable
✓ Name should start with lower case.
✓ If name having multiple words then separating every word with
underscore symbol is a good practice
5. Creating a variable
✓ We need to follow below syntax to create a variable.
Syntax
name_of_the_variable = value
2|Page 5.PYTHON-VARIABLES
Data Science – Python Variables
Program Creating a single variable
Name demo1.py
age = 16
print(age)
output
16
Program Creating two variables
Name demo2.py
name = "Daniel"
age = 16
print(name)
print(age)
output
Daniel
16
Program Creating two variables and using one print
Name demo3.py
name = " Daniel"
age = 16
print(name, age)
output
Daniel 16
3|Page 5.PYTHON-VARIABLES
Data Science – Python Variables
Program Creating a variable with meaningful text message
Name demo4.py
name = "Daniel"
print("I am", name)
output
I am Daniel
Important note
✓ Python having dynamic data type nature.
✓ It means, while creating variable we no need to provide the data
type explicitly in python.
✓ Regarding data type we will learn in Data type chapter.
4|Page 5.PYTHON-VARIABLES
Data Science – Python Variables
6. Creating multiple variables in single line
✓ We can creating multiple variables in single line
✓ # Rule
o There should be same number on the left and right-hand sides.
Otherwise we will get error.
Program Creating multiple variables in single line
Name demo5.py
a, b, c = 1, 2, 3
print(a)
print(b)
print(c)
output
1
2
3
5|Page 5.PYTHON-VARIABLES
Data Science – Python Variables
7. Single value for multiple variables
✓ We can assign a single value to multiple variables simultaneously.
Program Assign single value to multiple variables
Name demo6.py
a=b=c=3
print(a)
print(b)
print(c)
Output
3
3
3
6|Page 5.PYTHON-VARIABLES
Data Science – Python Variables
8. Variable re-initialization
✓ Based on requirement we can re-initialize existing variable value.
✓ In variable re-initialization old values will be replaced with new values.
Program Creating a variable
Name demo7.py
sal = 10000
print("My salary is:", sal)
Output
My salary is: 10000
Program Re-initialising variable
Name demo8.py
sal = 10000
sal = 12000
print("My salary is:", sal)
Output
My salary is: 12000
7|Page 5.PYTHON-VARIABLES