2 Python Variables
2 Python Variables
OBJECTIVES
• Students will be able to learn about the introduction of
variables.
• Learn how to create variables, print variables and change
values assigned to variables
VARIABLES
INTRODUCTION
Storing data allows us to use the
same data later in the program. For
this, we use something
called variables.
PYTHON VARIABLES
In programming, a variable is a container
(storage area) to hold data.
x is a variable
it is storing an integer value 25
the = operator is used to assign values to variables
x = 25
name = 'Mia‘
Example
age = 25
print('age')
Output: ???
Example 3
print(age)
Output:???
print('age') - prints the 'age'
string. The output is age.
Example 1
age = 25
print(age)
Output: ???
Example 2
age = 26
print(age)
Output: ???
HERE,
var = 25
var = 'John'
print(var)
Output: ???
HERE,
By the way, city2 is not a string because it's not wrapped inside quotation
marks.
TIP:
Always read the error message
carefully. It may help us fix the error.
In the above output, the error
message is clearly stating that city2
is not defined.
VALUE OF ONE VARIABLE TO
ANOTHER
It's also possible to assign the value of one variable to another. Let's take an
example.
color1 = "blue“
print(color1)
Output: ???
color2 = "pink"
# Assigning the value stored in color2 to color1
color1 = color2
print(color1)
# Output:???
ASSIGNING MULTIPLE VALUES TO MULTIPLE
VARIABLES
a, b, c = 5, 3.2, 'Hello'
print(a) # prints 5
print(b) # prints 3.2
print(c) # prints Hello
RULES FOR NAMING PYTHON VARIABLES
2. Create a name that makes sense. For example, vowel makes more sense
than v.
3. If you want to create a variable name having two words, use underscore
to separate them.
For example:
my_name
current_salary
4. Python is case-sensitive. So num and Num are different variables.
For example,
var num = 5
var Num = 55
print(num) # 5
print(Num) # 55
5. Avoid using keywords like if, True, class, etc. as variable names.
DIFFERENCE BETWEEN CONSTANT AND
VARIABLES
Constant Variables
A constant is used to hold the fixed values which we can A variable is used to hold some value that can be changed
retrieve later but cannot change. according to the requirement.
The constants are generally stored in the text segment as The variables are stored inside a data segment, heap, or
they are read-only stack depending on the environment it is declared in.
We can only assign a value to the constant while defining it. We can assign value to the variable anytime.
A constant can be defined by A variable can only be defined using the standard variable
using #define or const keyword. definition syntax.
It's important to build a habit of giving good variable names. Let's see why.
Can you guess which variable name is preferable among these two?
ms = 4969.5
Or
monthly_salary = 4969.5
SO, WHAT MAKES A GOOD VARIABLE NAME?
Create Variables
We create variables by assigning values to them.
age = 25
print(age)
Changing Values of Variables
It's possible to change values stored in variables.
current_salary = 77287.5
print(current_salary)
# Output: 77287.5
# changing value stored in the variable
current_salary = 80000.5
print(current_salary)
# Output: 80000.5
Assign One Variable to Another
We can also assign one variable to another.
color1 = "blue“
color2 = "pink"
# Assign the value stored in color2 to color1
color1 = color2
print(color1)
# Output: pink
DON'T CONFUSE STRINGS AND VARIABLES
Strings are always wrapped inside quotation marks but variables are not.