0% found this document useful (0 votes)
3 views4 pages

Python Lab Solutions

The document contains Python lab assignment solutions covering various programming concepts such as conditionals, loops, file handling, and data structures. Each solution is presented with a brief explanation of its functionality. Key topics include checking even/odd numbers, calculating reciprocals, determining voting eligibility, and implementing exception handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Python Lab Solutions

The document contains Python lab assignment solutions covering various programming concepts such as conditionals, loops, file handling, and data structures. Each solution is presented with a brief explanation of its functionality. Key topics include checking even/odd numbers, calculating reciprocals, determining voting eligibility, and implementing exception handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lab Assignment Solutions

1. Even or Odd
num = int(input())

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

Checks remainder when dividing by 2. If remainder is 0 → even, otherwise odd.

2. Decimal values 1/2 to 1/10


for i in range(2, 11):
print(1 / i)

Loops from 2 to 10 and calculates reciprocal each time.

3. Countdown
num = int(input())

while num >= 0:


print(num)
num -= 1

Decreases number until it reaches 0.

4. Voting eligibility
age = int(input())

if age >= 18:


print("Eligible")
else:
print("Not eligible")

Checks if age is at least 18.

5. Leap Year
year = int(input())

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):


print("Leap Year")
else:
print("Not Leap Year")

Uses leap year rule with divisibility conditions.

6. Characters and vowels


text = input()

vowels = "aeiouAEIOU"
count = 0

for ch in text:
if ch in vowels:
count += 1

print(len(text))
print(count)

Counts total characters and vowels separately.

7. max_of_three
def max_of_three(a, b, c):
return max(a, b, c)

print(max_of_three(1, 5, 3))

Returns the largest using built-in max().

8. Copy file
with open("[Link]", "r") as f1:
data = [Link]()

with open("[Link]", "w") as f2:


[Link](data)

Reads from one file and writes to another.

9. Half pyramid numbers


n = 5

for i in range(1, n + 1):


for j in range(1, i + 1):
print(j, end=" ")
print()

Each row prints numbers up to that row.

10. Frequency using dictionary


lst = [1,1,1,5,5,3,1,3,3,1,4,4,4,2,2,2,2]

freq = {}

for item in lst:


if item in freq:
freq[item] += 1
else:
freq[item] = 1

for k in sorted(freq):
print(k, ":", freq[k])

Counts occurrences using dictionary.


11. Sum of squares
n = int(input())

s = 0
for i in range(1, n + 1):
s += i * i

print(s)

Adds square of each number.

12. Exception handling


try:
a = int(input())
b = int(input())
print(a / b)
except ZeroDivisionError:
print("Division by zero error")

Prevents crash when dividing by zero.

13. Even numbers until 237


numbers = [386,462,47,418,907,344,236,375,823,566,597,978,328,615,953,345,399,162,758,219,918,237]

for num in numbers:


if num == 237:
break
if num % 2 == 0:
print(num)

Stops at 237 and prints even numbers only.

14. Flashcard class


class FlashCard:
def __init__(self, q, a):
self.q = q
self.a = a

def show(self):
print(self.q)
print(self.a)

card = FlashCard("What is Python?", "Programming language")


[Link]()

Uses class to store question and answer.

15. Remove duplicate values in dictionary


d = {'gfg':10, 'for':10, 'geeks':20, 'is':15, 'best':20}

new = {}
for k, v in [Link]():
if v not in [Link]():
new[k] = v

print(new)
Keeps only unique values.

16. Intersection of sets


def intersection_of_sets(A, B):
return A & B

print(intersection_of_sets({1,2,3}, {2,3,4}))

Uses '&' operator for common elements.

17. Full pyramid


n = 5

for i in range(1, n + 1):


print(" " * (n - i), end="")
print("*" * (2 * i - 1))

Uses spaces and star formula to form pyramid.

Tip: Focus on understanding loops and conditions. Most problems follow same pattern.

You might also like