Variable
Variable
print(x)
print(y)
print(z)
Variables Assignment in Python
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
# display
print( Number)
print(a)
print(b)
print(c)
Assigning different values to multiple variables
Python allows adding different values in a single line with “,”
operators.
Python
a, b, c = 1, 20.2, "Alankar PG Girls College"
print(a)
print(b)
print(c)
Can We Use the Same Name for Different Types?
If we use the same name, the variable starts referring to a new value
and type.
a = 10
a = "BCAIII"
print(a)
How does + operator work with variables?
The Python plus operator + provides a convenient way to add a value
if it is a number and concatenate if it is a string. If a variable is
already created it assigns the new value back to the same variable.
Python
a = 10
b = 20
print(a+b)
a = "Alankar PG"
b = "Girls College"
print(a+b)
Can we use + for different Datatypes also?
No use for different types would produce an error.
Python
a = 10
b = "students"
print(a+b)
Example
Create a variable outside of a function, and use it inside the function
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Example
Create a variable inside a function, with the same name as the
global variable
x = "awesome"
def myfun():
x = "fantastic"
print("Python is " + x)
myfun()
print("Python is " + x)