0% found this document useful (0 votes)
7 views3 pages

Python Exercise Solutions

The document provides a series of Python exercises covering various topics such as variables, string handling, math operators, conditional statements, loops, lists, and tuples. Each section includes specific tasks with example code snippets demonstrating how to perform operations like type casting, string manipulation, and using control flow statements. The exercises are designed to help learners practice and reinforce their understanding of Python programming concepts.
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)
7 views3 pages

Python Exercise Solutions

The document provides a series of Python exercises covering various topics such as variables, string handling, math operators, conditional statements, loops, lists, and tuples. Each section includes specific tasks with example code snippets demonstrating how to perform operations like type casting, string manipulation, and using control flow statements. The exercises are designed to help learners practice and reinforce their understanding of Python programming concepts.
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 Exercise Solutions 1.

Variables & Type Casting 1) Print name, age, salary


name = "Nisha"
age = 35
salary = 30000
print(name, age, salary)

2) Type cast strings to int and add


a = int(input("Enter A: "))
b = int(input("Enter B: "))
print(a + b)

3) Swap variables without third variable


a, b = b, a

2. String Handling & Slicing 1) Reverse string


s = "hello"
print(s[::-1])

2) Count vowels
s = "hello world"
count = sum(1 for c in s if c in "aeiouAEIOU")
print(count)

3) Palindrome check
s = "madam"
print(s == s[::-1])

4) Extract every 2nd character


s = "pythonprogramming"
print(s[::2])

5) Split & Join


s = "welcome to python"
words = [Link]()
print("_".join(words))

3. Math Operators 1) Simple interest


SI = (p * r * t) / 100

2) Area of circle
area = 3.14 * r * r

3) Multiplication table
for i in range(1, 11):
print(n * i)

4. If, Elif, Nested If 1) Positive/Negative/Zero


if n > 0: print("Positive")
elif n < 0: print("Negative")
else: print("Zero")

2) Grading
if marks >= 90: print("A")
elif marks >= 80: print("B")
elif marks >= 70: print("C")
elif marks >= 60: print("D")
else: print("Fail")
3) Even & divisible by 5
if n % 2 == 0 and n % 5 == 0:
print("Valid")

4) Nested If (Driving)
if age >= 18:
if age >= 21:
print("Driving License Eligible")
else:
print("Adult but not DL eligible")
else:
print("Minor")

5. Loops / Break / Continue 1) Skip multiples of 7


for i in range(1, 51):
if i % 7 == 0: continue
print(i)

2) Break at 25
for i in range(1, 51):
if i == 25: break
print(i)

3) Sum of even numbers 1–100


total = 0
for i in range(2, 101, 2):
total += i
print(total)

4) Print digits of number (while loop)


n = 5432
while n > 0:
print(n % 10)
n //= 10

5) Pattern
for i in range(1, 5):
print("*" * i)

6. Lists 1) Max, Min, Sum


lst = [1,2,3,4,5]
print(max(lst), min(lst), sum(lst))

2) Remove duplicates
unique = list(set(lst))

3) Count occurrences
count_dict = {}
for x in lst:
count_dict[x] = count_dict.get(x, 0) + 1
print(count_dict)

4) Reverse list
print(lst[::-1])
5) Extract even numbers
evens = [x for x in lst if x % 2 == 0]

7. Tuples 1) Convert tuple → list → tuple


t = (1,2,3)
lst = list(t)
t2 = tuple(lst)

2) Count occurrences
[Link](2)

3) Safe index
if value in t:
print([Link](value))
else:
print("Not found")

You might also like