#To print a sentence # printing multiple data types in single print function
print("This is Python class") name= "Sikandar"
Use only small letter, ie Print() is invalid Age=18
Sigle quote (‘ ...’) may be use in place of double print(name," is ",Age," year old.")
quote Output
Sikandar is 18 year old.
#To print a string using a variable
x="Kolkata" # Addition of two integers by taking input fromusers
print(x) x=int(input("Enter 1st value "))
x="Kolkata" Print(x)is invalid y=int(input("Enter 2nd value "))
x=Kolkata print(x) is invalid z=x+y
print(z)
# To find and print sum of two integers / expression Output
x=30 Enter 1st value 34
y=20 Enter 2nd value 16
z=x+y 50
print(z) # To print a string including double quote
print(x-y) print('He said, "He is happy"')
Output Output
50 He said, "He is happy"
10 # if statement
x=int(input("Enter a value "))
# Concatenation of two strings if(x%2==0):
x= "Azaz" print(x,"is even")
y= 'Afzal' else:
z=x+y print(x,"is Odd")
print(z) Output
Output Enter a value 22
AzazAfzal 22 is even
Print(x-y) is invalid for strings
# Different types of comments in python
# Concatenation of two strings by taking input from 1. Single line comment
user # This is a single line comment
x=input("Enter 1st string ") 2. Multiple lines comments
y=input("Enter 2nd string ") “”” This is
z=x+y a multiple lines
print(z) comment
Output “””
Enter 1st string This is 3. Docstring Comment
Enter 2nd string Himayatul Ghurba def Docstring():
This isHimayatul Ghurba """
This line is supposed to be print as docstring
"""
print("This line is within function. ") dict_values([1, 2, 3])
return dict_items([('a', 1), ('b', 2), ('c', 3)])
Docstring()
print(Docstring.__doc__) # end Argument
Output print("Learning")
This line is within function. print("End")
This line is supposed to be print as docstring print("Argument")
print("Learning",end=' ')
# Complex Numbers print("End",end=' ')
a=2+3j print("Argument")
b=3+4j Output
c=a+b Learning
print(c) End
Output Argument
(5+7j) Learning End Argument
Use of j is mandatory
# sep argument
# List & tuple print("Learning","sep","Argument")
list1=["a","b","c","d"] print("Learning","sep","Argument",sep="; ")
tuple1=(1,2,3,4) Output
print(list1[1]) Learning sep Argument
print(tuple1[0]) Learning; sep; Argument
list1[2]="f"
print(list1[2]) # Python operatrs
tuple1[2]=5 x=30
print(tuple[2]) y=8
Output z=2
b print(x+y)
1 print(x-y)
f print(x*y)
Error print(x/y)
print(x//y)
# Mapping (Dictionary) print(x%y)
map1={"a":1,"b":2,"c":3} print(y**2)
print(map1["a"]) print(z & y) # bitwise and
print(map1.get("b")) print(z | y) # bitwise or
print(map1.keys()) print(z ^ y) # bitwise xor
print(map1.values()) print(y >> z) # bitwise right shift
print(map1.items()) print(y << z) # bitwise left shift
Output Output
1 38
2 22
dict_keys(['a', 'b', 'c']) 240
3.75
3
6
64
0
10
10
2
32
# Difference between identity operator is and relational
operator ==
x=["a"]
y=["a"]
z=x
print(x==y)
print(x is y)
print(x==z)
print(x is z)
Output
True
False
True
True