You are on page 1of 3

#Printing variable

name="prajwal"
age=25
print("my name is ",name,"and I'm ",age ,"years old")
my name is prajwal and I'm 25 years old
In [9]:
#concatenating strings
city="wonderland"
print("i live in " +city)
i live in wonderland
In [10]:
#print using triple quotes for the multi line string
fruit ="apple"
age=30
print("name:",name,"age:",age)
name: prajwal age: 30
In [12]:
#print multiple variables with commma
message='''this is a multiline string'''
print(message)
this is a multiline string
In [13]:
#concatenate string using +:
fruit="apple"
print(" i like " + fruit)
i like apple
In [14]:
#using f-string
animal="cat"
color="black"
print(f"the {color} {animal} is sleeping")
height=1.75
print(f"my height is {height} meters")
the black cat is sleeping
my height is 1.75 meters
In [15]:
#format method for string formatting
item="book"
price=20.5
print("the {} costs ${}".format(item,price))
the book costs $20.5
In [24]:
item="book"
price=20.5
print("the",item,"costs $", price)
the book costs $ 20.5
In [29]:
#print without newline (end parameter)
print("this is on the same line ",end="")
print(" this is on the same line")
this is on the same line this is on the same line
In [30]:
#print with specific separator (sep paramter)
fruit1="apple"
fruit2="banana"
fruit3="orange"
print(fruit1,fruit2,fruit3,sep=",")
apple,banana,orange
In [31]:
fruit1="apple"
fruit2="banana"
fruit3="orange"
print(fruit1,fruit2,fruit3)
apple banana orange
In [34]:
#print with escape character:
print("this is a new line\nthis is another line")
print("this is a tab character\tthis text comes after the tab")
this is a new line
this is another line
this is a tab character this text comes after the tab
In [35]:
#printing numbers with different bases
num=42
print(f"decimal:{num}, Binary: {bin(num)},Hex: {hex(num)},octal: {oct(num)}")
decimal:42, Binary: 0b101010,Hex: 0x2a,octal: 0o52
In [38]:
#formatting output using % opoerator (older method)
name="ravi"
age=25
print("my name is %s and i am %d years old"%(name,age))
my name is ravi and i am 25 years old
In [40]:
#arthematic operators
num=10
num2=3
result_add= num+num2
result_sub=num-num2
result_div=num/num2
print("sum:",result_add,"sub:",result_sub,"division:",result_div)
sum: 13 sub: 7 division: 3.3333333333333335
In [41]:
#comparison operator
x=5
y=8
print("is x equal to y",x==y)
print("is x not equal to y.?",x!=y)
is x equal to y False
is x not equal to y.? True
In [42]:
is_sunny=True
is_warm=False
if is_sunny and is_warm:
print("its a sunny and warm day")
elif is_sunny or is_warm:
print("its either sunny or warm")
else:
print("its neither sunny nor warm")
its either sunny or warm
In [43]:
temperature=30
if temperature > 25:
print("its hot day")
else:
print("its not hot day")
its hot day
In [45]:
#checking multiple conditions
grade=75
if grade>=90:
print("A")
elif grade>=80:
print("B")
else:
print("C")
C
In [ ]:

You might also like