Variables are created when we assign a value to it. Based on the data type of a variable, the interpreter allocates memory.
In Python the variable declaration happens automatically when you assign a value to a variable, = is symbol used to assign values to it.
Eg:
a=10
b=12.34
c=”Hello”
d=’A’
print(a)
print(b)
print(c)
print(d)
OUTPUT:
10
12.34
Hello
A
In variables we can use single quotes.
Eg:
q=’Hello’
print(q)
OUTPUT:
Hello
Assigning multiple values to a variable:
x,y,z="regu","ram","sv"
print(x)
print(y)
print(z)
OUTPUT:
regu
ram
sv
Assigning same values to multiple variables:
Eg:
a=b=c="svrr tech"
print(a)
print(b)
print(c)
OUTPUT:
svrrtech
svrrtech
svrrtech
Adding two variable using python:
x=”SVRR”
y=”TECH”
z=x+y
print(z)
OUTPUT:
SVRR TECH
Note: In python we cannot add a integer with a variable.
Eg:
x=12
y=”SVRR”
z=x+y
print(z)
OUTPUT:
Error
Global variable:
Eg:
a=10
def myfun():
print(a)
myfun()
OUTPUT:
10
إرسال تعليق