Variables
Variables are used to store data types like string, integer and float values.
In [1]: message1 = 'Hello python world'
message2 = "Hello python world"
message3 = """Hello python world"""
message4 = 100
print(message1)
print(message2)
print(message3)
print(message4)
Hello python world
Hello python world
Hello python world
100
A variables holds a value that can be modified or changed at any point.
Naming Rules:
1. Variables name can only contain letters, numbers, and underscores.
2. Variable names can start with a letter or an underscore, but can not start with a number.
3. Spaces are not allowed in variable names, so we use underscores instead of spaces. For example, use student_name instead of "student name".
4. You cannot use Python keywords as variable names.
5. Variable names should be descriptive, without being too long. For example mc_wheels is better than just "wheels", and number_of_wheels_on_a_motorycle.
6. Variable names are case-sensitive (Ram, ram and RAM are three different variables)
In [2]: #Valid variable names:
myvar = "Hello World"
my_var = "Hello World"
_my_var = "Hello World"
myVar = "Hello World"
MYVAR = "Hello World"
myvar2 = "Hello World"
#Invalid variable names:
2myvar = "Hello World"
my-var = "Hello World"
my var = "Hello World"
in = "Hello World"
Cell In[2], line 9
2myvar = "Hello World"
^
SyntaxError: invalid decimal literal
Name Error:
There is one common error when using variables, that the user will almost certainly encounter at some point. Take a look at this code, and see if you can figure out why it causes an error.
In [3]: # my assigned variable name is message and not messages
message = "Hello World"
print(messages)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 3
1 # my variable name is message and not messages
2 message = "Hello World"
----> 3 print(messages)
NameError: name 'messages' is not defined
In [4]: # Similarly, my assigned variable namem in this code is variable and not Variable
variable = "Hello World"
print(Variable)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 3
1 # Similarly, my assigned variable namem in this code is variable and not Variable
2 variable = "Hello World"
----> 3 print(Variable)
NameError: name 'Variable' is not defined
Assigning a value to a variable
In [5]: #Assigning a value to a variable
school = "Elpro International School: A Powerhouse of Learning"
print(school)
Elpro International School: A Powerhouse of Learning
Changing value of a variable
In [6]: #Changing value of a variable
school = "Elpro International School: A Powerhouse of Learning"
print(school)
school = "Best School in Pune"
print(school)
Elpro International School: A Powerhouse of Learning
Best School in Pune
Assigning different values to different variables in single line
In [7]: # Assigning different values to different variables in single line
x,y,z = 7,3.1415,"Hello World"
print(x)
print(type(x))
print(y)
print(type(y))
print(z)
print(type(z))
7
<class 'int'>
3.1415
<class 'float'>
Hello World
<class 'str'>
Assigning same value to different variables
In [9]: # Assigning same value to different variable
a=b=c="Hello World"
print(a)
print(b)
print(c)
Hello World
Hello World
Hello World
In [10]: # Assigning same value to different variable
l=m=n="Hello World"
print(l)
print(m)
print(n)
Hello World
Hello World
Hello World
Deleting a variable
In [11]: # deleting a variable
var = "Newton's third law states that for every action in nature there is an equal and opposite reaction."
print(var)
del var
print(var)
Newton's third law states that for every action in nature there is an equal and opposite reaction.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 5
3 print(var)
4 del var
----> 5 print(var)
NameError: name 'var' is not defined
Types of Variables
In [12]: #Types of Variables
var = "Hello Friends, Good Morning" # string type variable
var1 = 99 # integer type variable
var2 = 3.1415 # float type variable
var3 = True # boolean type variable
var4 = False # boolean type variable
Exercises:
1. Hello World - variable: Store your own version of the message "Hello World" in a variable, and print it.
In [13]: variable = "Hello World"
print(variable)
Hello World
1. One Variable, Two Messages: Store a message in a variable, and then print that message. Store a new message in the same variable, and then print that new message.
In [14]: variable = "Hello Python"
print(variable)
variable = "Hello Java"
print(variable)
Hello Python
Hello Java