Variables in Python
Welcome to the series of python language tutorials. We will cover basic to advance level usage of language. I will explain each topic in detail and try to make some exercises at the end of each topic. Python language is very high in demand.
In this tutorial, I will cover Variables in Python
Variables
Creating variables in python is very easy. There is no such common set of protocols that you have to follow to declare the variable.
x = 10
y = "Abdul"
print(x)
print(y)This will print10
Abdul
The variable type can be changed easily even if it is declared earlier.
x = 5 # x is of type int
x = "Basit" # x is now of type str
print(x)The above line printsBasit
Let me explain the above snippet of code.
In the first line, the value of x is 5 and the type is Int
In the Second line, the value is updated and Now X is storing “ Basit” which is the type of Str. So when I call the Print function and pass the variable X it prints “Basit”
Casting
Casting is the process in which you specify the type of the variable
x = str(6) # x will be '6'
y = int(6) # y will be 6
z = float(6) # z will be 6.0
If you want to check the type of the variable you can use the built-in function call type()
x = 5
y = "Jack"
print(type(x))
print(type(y))The output of these two print functions will be like this
int
str
There are some rules for naming the python variables.
A variable can have a short name (like x and y) or a more descriptive name (age, surname, total_volume). Rules for Python variables:
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0–9, and _ )
- Variable names are case-sensitive (age, Age, and AGE are three different variables)
myvar = "Abdul"
my_var = "Abdul"
_my_var = "Abul"
myVar = "Abul"
MYVAR = "Abul"
myvar2 = "Abul"print(myvar)
print(my_var)
print(_my_var)
print(myVar)
print(MYVAR)
print(myvar2)
output of this
Abul
Abul
Abul
Abul
Abul
Abul
Examples of the illegal and Prophitted variable names
2myvar = "Abdul"
my-var = "Abdul"
my var = "Abdul"
It will produce an error.
Assign Multiple values to the multiple variables
x, y, z = "Mango", "Apple", "Grapes"
print(x)
print(y)
print(z)It will produce the output of
Mango
Apple
Grapes
One value to Multiple Variable
x = y = z = "Milk"
print(x)
print(y)
print(z)The Output is
Milk
Milk
Milk
Unpack the variables
Python allows you to assign variables to the values of the list.
fruits = ["Mango", "Grape", "Apple"]
x, y, z = fruits
print(x)
print(y)
print(z)It will prints
Mango
Grape
Apple
In the above example, you have to be careful that the number of variables should be equal to the values. let me give you an example.
fruits = ["Mango", "Grape", "Apple"]
x, y, z, v = fruits
print(x)
print(y)
print(z)
It will prints
Mango
Grape
Apple
This will produce the error because in the list you have 3 values and you are assigning 4 variables
Output Variable
Normally python print function is used as an output variable
x = 5
print(x)
You can use operator in the print function to display the result
x = 5
y = 10
print(x+y)The output will be 15
Global Variables
A global variable is one which created outside of the function and can be accessed in the function
Create the variable outside the function and then use that variable inside the function
x = "Fun"
def func():
print("Python is " + x)
func()The output of the function is
"Python is Fun"
Exercise
Use subtractoperatorr in the print function
change the value of x to an integer in the global variable