10/16/18

Python Variables & Data Types




Variables

a=10
b=10.5
name='pavan'
print(a)
print(b)
print(name)
print(a,b,name)

############################

#this statement assign 100 to a, 100.5 to b and Welcome to c. Multiple Values to multiple varaibles
a,b,c=100,100.5,"welcome"
print(a,b,c)

a = b = c = 100 # this statement assign 100 to c, b and a.  Single value to all variables
print(a,b,c)

#################
x = 1
y = 2
y, x = x, y  # assign y value to x and x value to y
print (x,y)

Data Types

x=100  # int type
y=10.5 # float type

s1="welcome"  # string/char type
s2='welcome' # string /char type

b=  True # boolean type

# To know th type of variables
print(type(x))
print(type(y))
print(type(s1))
print(type(s2))
print(type(b))

Concatenation

print(10+10) # valid
print(10.5+10.5) # valid
print("welcome"+"python") # valid
print (10+15.5) # valid

print(True+5) # valid
print(10+False) # valid
print(True+True)# valid

print(10+"welcome")  #Not valid  -TypeError: unsupported operand type(s)
print(10.5+"welcome")  #Not valid -TypeError: unsupported operand type(s)
print(True+"welcome")  #Not valid -TypeError: unsupported operand type(s)




0 comments:

Post a Comment