✦ VidyaVerseOpen interactive version

← Python Lab Practical Record

EXPERIMENT No. 3

Python Lab Practical Record · Python programming laboratory

Read with diagrams, search and self-test

3A. Determine whether a number is even or odd

Aim

To write a Python program to determine whether a number is even or odd.

Program (even_or_odd.py)

# Exp 3A: Determine whether a number is even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

Output

Enter a number: 4
The number is even

Result

The given number was identified as even or odd using the modulus operator.

3B. Multiplication table using a for loop

Aim

To create a Python program that uses a for loop to print the multiplication table of a number.

Program (multiplication_table_for.py)

# Exp 3B: Multiplication table using for loop
num = int(input("Enter a number: "))
for i in range(1, 11):
    print(num, "x", i, "=", num * i)

Output

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Result

The multiplication table of the given number was printed using a for loop.

3C. Factorial of a number using a while loop

Aim

To write a Python program to calculate the factorial of a number using a while loop.

Program (factorial_while.py)

# Exp 3C: Factorial of a number using while loop
num = int(input("Enter a number: "))
factorial = 1
n = num
while n > 0:
    factorial = factorial * n
    n = n - 1
print("Factorial of", num, "=", factorial)

Output

Enter a number: 5
Factorial of 5 = 120

Result

The factorial of the given number was calculated using a while loop.

3D. Check whether a number is positive, negative or zero

Aim

To write a Python program to check whether the given number is positive, negative or zero.

Program (positive_negative_zero.py)

# Exp 3D: Check whether a number is positive, negative or zero
num = int(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

Output

Enter a number: 7
Positive

Result

The given number was classified as positive, negative or zero.