63e5e070392faQCR Lab 2pdf
63e5e070392faQCR Lab 2pdf
Namal University
07 February 2023
Outline
• Python Basics
• Input from User
• Type Conversion
• String Value
• Arithmetic Operator
• Comparison Operator
• Logical Operator
• Conditional Statement
Python Basics
Python is a popula language and it compile code line by line. To print any value
we use:
print(”hello world”)
we can print value by assigning it to any variable.
x = 48
print(x)
1
Type Conversion
If we get value from user f = input(”first ”)
s = input(”second ”)
total= f+s
print(total)
In such situation, compiler print just input. It doesn’t add both values, just
print it. Compiler consider these values as strings.
v1 = input(”first ”)
v2 = input(”second ”)
total value= int(v1)+int(v2)
print(total value)
Another way is:
c1 = float(input(”first floating value”))
c2 = float(input(”second floating value”))
c total=c1+c2
print(c total)
String value
course = ”First year CS”
print(course.upper())
print(course.lower())
print((course.find(’y’)))
print((course.find(’E’)))
print(course.replace(’F’,’S’))
print(course.find(’year’))
print(”First” in course)
print(course)
Arithmetic Operator
print(10+5)
print(10*5)
print(10/4)
print(10//4)
print(10 % 7)
print(10 ** 2)
x = 10
x=x+6
print(x)
2
Comparison Operator
x = 5 == 8
print(x)
Logical Operator
x = 10
print((x > 10 and x < 17))
print((x > 16 or x < 19))
print(not x < 10)
Conditional Statement
if
x = 10
if x > 18:
print(”QCR lab”)
print(”first year”)
print(”Done”)
if-elif
a=5
b = 10
if b > a:
print(”b is greater than a”)
elif a == b:
print(”a and b are equal”)
if-else
a=5
b = 10
if b > a:
print(”b is greater than a”)
elif a == b:
print(”a and b are equal”)
else:
print(”a is greater than b”)