You are on page 1of 5

Department: Information Technology

Course Name: Python


Sheet No.4
1. Create variables named x, y and z and assign them values 5, 8 and
12, then calculate the result of the following comparisons and
demonstrate the process you are doing:
a) Is x equal to y?
b) Is y less than z?
c) Is z greater than x?
d) Is x less than or equal to y?
e) Is y not equal to x?
f) Is z not less than or equal to x?

2. Create variables named a, b, c and d and assign them values 2, 4, 7


and 9, then calculate the result of the following logical Operations:
a) Is d greater than b and c not equal a?
b) Is b greater than d and a less than d?
c) Is d less than b or c not equal a?
d) Is b greater than d or a less than d?
e) Negates the value of (is a greater than or equal to d)
f) a >= 2 and (a/b) > 2

3. Create variables named student_one, student_two and


student_three and assign them values Omar, omar and Omar,
then calculate the output of the following operations:
a) student_one!= student_two
b) student_two== student_three

4. Take an integer number from user and check whether it is even or


not.

5. check if the input number is positive or negative or zero and display


an appropriate message ("Positive number", "Zero" and
"Negative number").

6. Write a program to find largest of three input numbers.

7. Take two number and check whether the sum is greater than, less than or
equal to 20

1/5
Department: Information Technology
Course Name: Python

Solutions Sheet No.4


1. x=5
y=8
z=12
print("x==y is",x==y)
print("y<z is", y<z)
print("z>x is",z>x)
print("x<=y is",x<=y)
print("y!=x is",y!=x)
print("z>=x is",y>=x)

2. a=2
b=4
c=7
d=9
print(d>b and c!=a)
print(b>d and a<d)
print(d<b or c!=a)
print(b>d or a<d)
print(not a>=d)
print(a >= 2 and (a/b) > 2)

3. student_one="Omar"
student_two="omar"
student_three="Omar"
print(student_one!= student_two)
print(student_two == student_three)

4. a=int(input("Enter an integer: "))


if a % 2 == 0:
print("Even Number")
else:
print("Odd Number")

2/5
Department: Information Technology
Course Name: Python

5. a=float(input("enter a number: "))


if a > 0:
print("Positive number")
elif a == 0:
print("Zero")
else:
print("Negative number")

6. a=float(input("Enter number one: "))


b=float(input("Enter number two: "))
c=float(input("Enter number three: "))
if a>=b and a>=c:
g=a
elif b>=a and b>=c:
g=b
else:
g=c
print("the greater number is ",g)

7. x=float(input("Enter the first number: "))


y=float(input("Enter the second number: "))
z=x+y
if z>20:
print ("the sum"+" ("+ str(z)+") "+"is greater than 20")
elif z==20:
print ("the sum"+" ("+ str(z)+") "+"is equal to 20")
else:
print ("the sum"+" ("+ str(z)+") "+"is less than 20")

3/5
Department: Information Technology
Course Name: Python

Summry
1. Comparison Operators
Operator Meaning
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to

2. Logical operators
Operator Meaning
and True if both the operands are true
or True if either of the operands is true
True if operand is false (complements
not
the operand)

x y Returns
True True True
True and False False
False True False
False False False

x y Returns
True True True
True or False True
False True True
False False False

x Returns
not True False
False True

4/5
3. Syntax of if...else Statement
if test condition:
Body of if
else:
Body of else
The if..else statement evaluates the test condition and will execute body of
if only when test condition is True.
If the condition is False, body of else is executed.

4. Syntax of if elif else statement


if test condition:
block_of_code_1
elif test condition_2:
block_of_code_2
elif test condition_3:
block_of_code_3
..
..
..
else:
block_of_code_n

The elif is short for else if. It allows us to check for multiple expressions.
If the test condition for if is False, it checks the test condition of the next
elif block and so on.
If all the test conditions are False, body of else is executed.
The if block can have only one else block. But it can have multiple elif
blocks.

5/5

You might also like