Python Programming Lab Record
Description
Python is a high-level, easy-to-understand programming language that is
widely used for learning programming concepts. It supports simple syntax,
which makes it suitable for beginners. In this lab work, basic Python
programs are written to understand loops, conditions, and simple problem-
solving techniques. Both for loop and while loop are used to strengthen the
understanding of iterative statements.
Objective
To understand the fundamentals of Python programming
To practice the use of for loop and while loop
To develop logical thinking and problem-solving ability
To learn basic operations on numbers and lists
Problems
Problem 1: Sum of Odd and Even Numbers (for Loop)
numbers = [1, 2, 3, 4, 5, 6]
odd_sum = 0
even_sum = 0
for n in numbers:
if n % 2 == 0:
even_sum = even_sum + n
else:
odd_sum = odd_sum + n
print("Odd Sum =", odd_sum)
print("Even Sum =", even_sum)
# Output:
# Odd Sum = 9
# Even Sum = 12
Problem 2: Sum of Odd and Even Numbers (while Loop)
numbers = [1, 2, 3, 4, 5, 6]
odd_sum = 0
even_sum = 0
i = 0
while i < len(numbers):
if numbers[i] % 2 == 0:
even_sum = even_sum + numbers[i]
else:
odd_sum = odd_sum + numbers[i]
i = i + 1
print("Odd Sum =", odd_sum)
print("Even Sum =", even_sum)
# Output:
# Odd Sum = 9
# Even Sum = 12
Problem 3: Smallest Number from a Set (for Loop)
numbers = [23, 45, 12, 67, 10]
smallest = numbers[0]
for n in numbers:
if n < smallest:
smallest = n
print("Smallest Number =", smallest)
# Output:
# Smallest Number = 10
Problem 4: Smallest Number from a Set (while Loop)
numbers = [23, 45, 12, 67, 10]
smallest = numbers[0]
i = 0
while i < len(numbers):
if numbers[i] < smallest:
smallest = numbers[i]
i = i + 1
print("Smallest Number =", smallest)
# Output:
# Smallest Number = 10
Problem 5: Sum of Numbers Between 50 and 100 (for Loop)
total = 0
for n in range(50, 101):
if n % 3 == 0 and n % 5 != 0:
total = total + n
print("Sum =", total)
# Output:
# Sum = 963
Problem 6: Sum of Numbers Between 50 and 100 (while Loop)
n = 50
total = 0
while n <= 100:
if n % 3 == 0 and n % 5 != 0:
total = total + n
n = n + 1
print("Sum =", total)
# Output:
# Sum = 963
Problem 7: Second Highest Number (for Loop)
numbers = [10, 20, 5, 40, 30]
numbers = list(set(numbers))
[Link]()
print("Second Highest =", numbers[-2])
# Output:
# Second Highest = 30
Problem 8: Second Highest Number (while Loop)
numbers = [10, 20, 5, 40, 30]
numbers = list(set(numbers))
[Link]()
i = 0
while i < 1:
print("Second Highest =", numbers[-2])
i = i + 1
# Output:
# Second Highest = 30
Problem 9: Factorial of a Number (for Loop)
num = 5
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
print("Factorial =", factorial)
# Output:
# Factorial = 120
Problem 10: Factorial of a Number (while Loop)
num = 5
factorial = 1
i = 1
while i <= num:
factorial = factorial * i
i = i + 1
print("Factorial =", factorial)
# Output:
# Factorial = 120
Problem 11: Fibonacci Series (for Loop)
terms = 7
a = 0
b = 1
for i in range(terms):
print(a, end=" ")
c = a + b
a = b
b = c
# Output:
# 0 1 1 2 3 5 8
Problem 12: Fibonacci Series (while Loop)
terms = 7
a = 0
b = 1
count = 0
while count < terms:
print(a, end=" ")
c = a + b
a = b
b = c
count = count + 1
# Output:
# 0 1 1 2 3 5 8
Conclusion
Through this lab work, basic Python programming concepts such as loops,
conditional statements, and list operations were practiced. Writing the same
problems using both for loop and while loop helped in clearly understanding
the difference between the two looping methods. These programs build a
strong foundation for learning advanced programming concepts.