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

Python String, List, Tuple Exercises

The document contains 70 Python practice questions and answers organized into categories such as Strings, Lists, Tuples, Type Conversions, If-Else, Loops, and Slicing. Each category includes various coding exercises that cover fundamental programming concepts and techniques. The examples demonstrate how to manipulate strings, lists, and tuples, perform type conversions, and implement control flow and loops in Python.

Uploaded by

snehitha2703
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)
43 views4 pages

Python String, List, Tuple Exercises

The document contains 70 Python practice questions and answers organized into categories such as Strings, Lists, Tuples, Type Conversions, If-Else, Loops, and Slicing. Each category includes various coding exercises that cover fundamental programming concepts and techniques. The examples demonstrate how to manipulate strings, lists, and tuples, perform type conversions, and implement control flow and loops in Python.

Uploaded by

snehitha2703
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 Practice Questions and Answers (70 Q&A)

=== Strings ===


1. Count vowels:
s = 'hello world'; vowels = 'aeiouAEIOU'; count = sum(1 for ch in s if ch in vowels); print(count)
2. Reverse string:
s = 'python'; rev = ''; for ch in s: rev = ch + rev; print(rev)
3. Palindrome check:
s = 'madam'; print(s == s[::-1])
4. Character frequency:
s = 'banana'; freq = {}; [[Link]({ch:[Link](ch,0)+1}) for ch in s]; print(freq)
5. Remove duplicates:
s = 'programming'; result = ''.join([Link](s)); print(result)
6. Count words:
s = 'Python is fun'; print(len([Link]()))
7. Longest word:
s = 'I love programming in Python'; print(max([Link](), key=len))
8. Replace spaces:
s = 'hello world'; print([Link](' ', '-'))
9. To uppercase:
s = 'hello'; upper = ''.join(chr(ord(ch)-32) if 'a'<=ch<='z' else ch for ch in s); print(upper)
10. Anagram check:
s1, s2 = 'listen', 'silent'; print(sorted(s1) == sorted(s2))

=== Lists ===


1. Largest element:
lst = [3,7,2,9,1]; print(max(lst))
2. Remove duplicates:
lst = [1,2,2,3,4,4,5]; res=[]; [[Link](i) for i in lst if i not in res]; print(res)
3. Second largest:
lst=[10,20,4,45,99]; print(sorted(set(lst))[-2])
4. Rotate list:
lst=[1,2,3,4,5]; k=2; print(lst[k:]+lst[:k])
5. Count occurrences:
lst=[1,2,2,3,3,3]; count={}; [[Link]({i:[Link](i,0)+1}) for i in lst]; print(count)
6. Flatten nested:
nested=[[1,2],[3,4],[5]]; flat=[x for sub in nested for x in sub]; print(flat)
7. Merge sorted lists:
a=[1,3,5]; b=[2,4,6]; print(sorted(a+b))
8. Common elements:
a=[1,2,3]; b=[3,4,5]; print([x for x in a if x in b])
9. Remove evens:
lst=[1,2,3,4,5,6]; print([x for x in lst if x%2!=0])
10. Sum positives:
lst=[-1,2,-3,4,5]; print(sum(x for x in lst if x>0))

=== Tuples ===


1. Tuple to string:
t=('p','y','t','h','o','n'); print(''.join(t))
2. Max & Min:
t=(3,7,1,9,2); print(max(t),min(t))
3. Reverse tuple:
t=(1,2,3,4); print(t[::-1])
4. Check existence:
t=(1,2,3,4); print(3 in t)
5. List of tuples->dict:
lst=[(1,'a'),(2,'b')]; print(dict(lst))
6. Longest tuple:
lst=[(1,2),(3,4,5),(6,)]; print(max(lst,key=len))
7. Sort by 2nd element:
lst=[(1,3),(2,1),(3,2)]; print(sorted(lst,key=lambda x:x[1]))
8. Concatenate tuples:
t1,t2=(1,2),(3,4); print(t1+t2)
9. Multiply elements:
t=(2,3,4); prod=1; [prod:=prod*i for i in t]; print(prod)
10. Repeated elements:
t=(1,2,2,3,4,4,5); print([x for x in set(t) if [Link](x)>1])

=== Type Conversions ===


1. Int->Str->Int:
x=123; s=str(x); print(int(s))
2. String digits->list ints:
s='12345'; print([int(ch) for ch in s])
3. List->Tuple:
lst=[1,2,3]; print(tuple(lst))
4. Tuple->List:
t=(1,2,3); print(list(t))
5. String->Set:
s='hello'; print(set(s))
6. Dict keys->List:
d={'a':1,'b':2}; print(list([Link]()))
7. List of strings->String:
lst=['Python','is','fun']; print(' '.join(lst))
8. Float->Int:
f=3.7; print(round(f) if f-int(f)>=0.5 else int(f))
9. Binary str->Int:
b='1011'; print(int(b,2))
10. Int->bin,oct,hex:
n=25; print(bin(n),oct(n),hex(n))

=== If-Else ===


1. Even/Odd:
n=7; print('Even' if n%2==0 else 'Odd')
2. Greatest of 3:
a,b,c=10,25,15; print(max(a,b,c))
3. Pos/Neg/Zero:
n=-5; print('Positive' if n>0 else 'Negative' if n<0 else 'Zero')
4. Leap year:
y=2024; print('Leap' if (y%400==0 or (y%4==0 and y%100!=0)) else 'Not Leap')
5. Vowel/Consonant:
ch='e'; print('Vowel' if [Link]() in 'aeiou' else 'Consonant')
6. Grade:
marks=85; print('A' if marks>=90 else 'B' if marks>=75 else 'C' if marks>=50 else 'F')
7. Divisible by 5&7:
n=35; print(n%5==0 and n%7==0)
8. Voting eligibility:
age=17; print('Eligible' if age>=18 else 'Not Eligible')
9. Absolute value:
n=-12; print(-n if n<0 else n)
10. Char classification:
ch='A'; print('Uppercase' if [Link]() else 'Lowercase' if [Link]() else 'Digit' if [Link]() else 'Special')

=== Loops ===


1. Numbers 1-100:
for i in range(1,101): print(i,end=' ')
2. Factorial:
n=5; fact=1; [fact:=fact*i for i in range(1,n+1)]; print(fact)
3. Multiplication table:
n=7; [print(n,'x',i,'=',n*i) for i in range(1,11)]
4. Sum of digits:
n=1234; print(sum(int(ch) for ch in str(n)))
5. Fibonacci:
n=7; a,b=0,1; [print((a:=b),(b:=a+b),end=' ') for _ in range(n)]
6. Primes 1-100:
for num in range(2,101):
for i in range(2,int(num**0.5)+1):
if num%i==0: break
else: print(num,end=' ')
7. Odd numbers:
for i in range(1,51,2): print(i,end=' ')
8. Reverse number:
n=1234; rev=0; while n>0: rev=rev*10+n%10; n//=10; print(rev)
9. Pyramid pattern:
for i in range(1,6): print('*'*i)
10. Sum first n naturals:
n=10; print(sum(range(1,n+1)))

=== Slicing ===


1. Reverse string:
s='python'; print(s[::-1])
2. First 3 chars:
s='python'; print(s[:3])
3. Every 2nd char:
s='python'; print(s[::2])
4. Last 3 of list:
lst=[1,2,3,4,5,6]; print(lst[-3:])
5. Tuple slice:
t=(10,20,30,40,50,60); print(t[2:6])
6. Alternate elements:
lst=[1,2,3,4,5,6]; print(lst[::2])
7. Middle elements:
lst=[1,2,3,4,5,6]; mid=lst[len(lst)//2-1:len(lst)//2+1]; print(mid)
8. Remove 1st & last:
s='python'; print(s[1:-1])
9. Rotate list:
lst=[1,2,3,4,5]; k=2; print(lst[k:]+lst[:k])
10. Palindrome check:
s='level'; print(s==s[::-1])

You might also like