0% found this document useful (0 votes)
2 views2 pages

Python Practical All Questions

The document contains a collection of Python programming exercises with complete solutions. Each exercise addresses a specific task, such as checking for palindromes, counting characters, and manipulating lists and dictionaries. The solutions are provided in code format, demonstrating various Python functionalities and techniques.

Uploaded by

bhattshreeyansh3
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)
2 views2 pages

Python Practical All Questions

The document contains a collection of Python programming exercises with complete solutions. Each exercise addresses a specific task, such as checking for palindromes, counting characters, and manipulating lists and dictionaries. The solutions are provided in code format, demonstrating various Python functionalities and techniques.

Uploaded by

bhattshreeyansh3
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

<b>PYTHON PRACTICAL FILE – COMPLETE SOLUTIONS</b>

<b>1. Write a program to check whether a string is palindrome or not.</b>

<pre>a = input("Enter string: ") b = a[::-1] if a == b: print("Palindrome") else: print("Not


Palindrome")</pre>

<b>2. Write a program to count alphabets, digits, uppercase, lowercase, spaces and other
characters.</b>

<pre>a = input("Enter line: ") u = l = d = s = o = 0 for i in a: if [Link](): u += 1 elif [Link](): l += 1


elif [Link](): d += 1 elif [Link](): s += 1 else: o += 1 print("Uppercase:",u) print("Lowercase:",l)
print("Digits:",d) print("Spaces:",s) print("Others:",o)</pre>

<b>3. Write a program to display frequency of list elements.</b>

<pre>a = [1,2,2,3,4] b = {} for i in a: b[i] = [Link](i,0) + 1 print(b)</pre>

<b>4. Write a program to display strings starting with 'A'.</b>

<pre>a = ["Apple","Ant","Ball","Cat"] for i in a: if [Link]("A"): print(i)</pre>

<b>5. Write a program to find sum of values ending with 3.</b>

<pre>a = [13,23,45,33] s = 0 for i in a: if i % 10 == 3: s += i print("Sum:",s)</pre>

<b>6. Write a program to remove duplicate elements from list.</b>

<pre>a = [1,2,2,3,4,4] b = [] for i in a: if i not in b: [Link](i) print(b)</pre>

<b>7. Write a program to swap element with next if divisible by 7.</b>

<pre>a = [14,5,7,9,21] for i in range(len(a)-1): if a[i] % 7 == 0: a[i], a[i+1] = a[i+1], a[i] print(a)</pre>

<b>8. Write a program to create a tuple from user input.</b>

<pre>a = int(input("Enter number of values: ")) b = [] for i in range(a): [Link](input("Enter value:


")) c = tuple(b) print(c)</pre>

<b>9. Write a program to store countries and capitals in dictionary.</b>

<pre>a = int(input("Enter number of countries: ")) d = {} for i in range(a): c = input("Country: ") k =


input("Capital: ") d[c] = k print(d)</pre>

<b>10. Write a program to find GCD of two numbers.</b>

<pre>a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) while b != 0: a, b =
b, a % b print("GCD:",a)</pre>

<b>11. Write a function to display words less than four characters from file.</b>

<pre>f = open("[Link]","r") for i in [Link]().split(): if len(i) < 4: print(i) [Link]()</pre>

<b>12. Write a program to count words starting with 'The' or 'the'.</b>

<pre>f = open("[Link]","r") c = 0 for i in [Link]().split(): if [Link]("The") or [Link]("the"):


c += 1 print("Count:",c) [Link]()</pre>

<b>13. Write a program to display vowels and consonants in each word.</b>


<pre>f = open("[Link]","r") for i in [Link]().split(): v = c = 0 for j in [Link](): if j in "aeiou": v += 1
elif [Link](): c += 1 print(i,"Vowels:",v,"Consonants:",c) [Link]()</pre>

<b>14. Write a program to display words with maximum and minimum length.</b>

<pre>f = open("[Link]","r") a = [Link]().split() print("Max:",max(a,key=len))


print("Min:",min(a,key=len)) [Link]()</pre>

<b>15. Write a program to display employees earning between 20000 and 40000.</b>

<pre>import pickle f = open("[Link]","rb") try: while True: a = [Link](f) if 20000 <= a["salary"]
<= 40000: print(a) except: [Link]()</pre>

<b>16. Write a program to insert list data into CSV file.</b>

<pre>import csv a = [1,2,3,4] f = open("[Link]","w",newline="") w = [Link](f) [Link](a)


[Link]()</pre>

<b>17. Write a program to update stock in [Link].</b>

<pre>import pickle f = open("[Link]","rb") a = [] try: while True: [Link]([Link](f))


except: [Link]() p = input("Enter product code: ") s = int(input("Enter new stock: ")) for i in a: if
i["prod_code"] == p: i["stock"] = s f = open("[Link]","wb") for i in a: [Link](i,f)
[Link]()</pre>

<b>18. Write a program to read [Link] and count records.</b>

<pre>import pickle f = open("[Link]","rb") c = 0 try: while True: print([Link](f)) c += 1


except: print("Total records:",c) [Link]()</pre>

<b>19. Write a menu driven program for stack.</b>

<pre>s = [] while True: print("[Link] [Link] [Link]") c = int(input("Choice: ")) if c == 1:


[Link](input("Enter value: ")) elif c == 2: if s: print("Popped:",[Link]()) else: print("Empty") else:
break</pre>

<b>20. Write a program to create dictionary from string.</b>

<pre>a = "computeerr" d = {} for i in a: d[i] = [Link](i,0) + 1 print(d)</pre>

<b>21. Write a program to combine two dictionaries.</b>

<pre>from collections import Counter d1 = {'a':100,'b':200,'c':300} d2 = {'a':300,'b':200,'d':400} d =


Counter(d1) + Counter(d2) print(d)</pre>

<b>22. Write a program to sort tuple by second item.</b>

<pre>a = (('a',23),('b',37),('c',11),('d',29)) b = sorted(a,key=lambda x:x[1]) print(tuple(b))</pre>

You might also like