1.
Count Uppercase and Lowercase
s = "Python Is Fun"
upper = sum(1 for ch in s if ch.isupper())
lower = sum(1 for ch in s if ch.islower())
print("Upper:", upper, "Lower:", lower)
Output:
Upper: 3 Lower: 8
2. Remove Punctuation
import string
s = "Hello, World!!"
clean = "".join(ch for ch in s if ch not in string.punctuation)
print(clean)
Output:
Hello World
3. Find Longest Word
s = "Python makes coding fun and powerful"
words = s.split()
print("Longest word:", max(words, key=len))
Output:
Longest word: powerful
4. Count Digits and Alphabets
s = "Python3.10"
digits = sum(ch.isdigit() for ch in s)
alphas = sum(ch.isalpha() for ch in s)
print("Digits:", digits, "Alphabets:", alphas)
Output:
Digits: 3 Alphabets: 6
5. Pangram Check
import string
s = "The quick brown fox jumps over the lazy dog"
print("Pangram" if set(string.ascii_lowercase).issubset(set(s.lower())) else "Not Pangram")
Output:
Pangram
6. Remove Duplicates
s = "programming"
result = ""
for ch in s:
if ch not in result:
result += ch
print(result)
Output:
progamin
7. First Non-Repeated Character
s = "aabbcdeff"
for ch in s:
if s.count(ch) == 1:
print("First non-repeated:", ch)
break
Output:
First non-repeated: c
8. String Rotation Check
s1, s2 = "abcde", "cdeab"
print("Rotation" if s2 in s1+s1 else "Not Rotation")
Output:
Rotation
9. Count Words Ending with Vowel
s = "I love python and java"
words = s.split()
count = sum(1 for w in words if w[-1] in "aeiou")
print("Words ending with vowel:", count)
Output:
Words ending with vowel: 2
10. Title Case
s = "welcome to python world"
print(s.title())
Output:
Welcome To Python World
11. Remove Digits
s = "pyt3h0n2"
print("".join(ch for ch in s if not ch.isdigit()))
Output:
python
12. Most Frequent Character
s = "mississippi"
freq = {ch: s.count(ch) for ch in set(s)}
print("Most frequent:", max(freq, key=freq.get))
Output:
Most frequent: i
13. Reverse Each Word
s = "Python is fun"
rev = " ".join(word[::-1] for word in s.split())
print(rev)
Output:
nohtyP si nuf
14. Count Substring Occurrence
s = "banana"
print("Occurrences of 'ana':", s.count("ana"))
Output:
Occurrences of 'ana': 1
15. Replace Vowels with *
s = "hello world"
for v in "aeiou":
s = s.replace(v, "*")
print(s)
Output:
h*ll* w*rld
1. Check Leap Year
year = 2024
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Output:
Leap Year
2. Find Maximum of List (without max())
nums = [5, 9, 1, 12, 7]
largest = nums[0]
for n in nums:
if n > largest:
largest = n
print("Largest:", largest)
Output:
Largest: 12
3. Count Even and Odd Numbers
nums = [10, 21, 4, 45, 66, 93]
even, odd = 0, 0
for n in nums:
if n % 2 == 0:
even += 1
else:
odd += 1
print("Even:", even, "Odd:", odd)
Output:
Even: 3 Odd: 3
4. Print Prime Numbers in Range
for n in range(10, 21):
for i in range(2, n):
if n % i == 0:
break
else:
print(n, end=" ")
Output:
11 13 17 19
5. Perfect Number
n = 28
s = sum(i for i in range(1, n) if n % i == 0)
print("Perfect" if s == n else "Not Perfect")
Output:
Perfect
6. Sum of Digits
n = 12345
s = 0
while n > 0:
s += n % 10
n //= 10
print("Sum of digits:", s)
Output:
Sum of digits: 15
7. Product of Digits
n = 234
prod = 1
while n > 0:
prod *= n % 10
n //= 10
print("Product:", prod)
Output:
Product: 24
8. Armstrong Numbers (100–999)
for n in range(100, 130):
s = sum(int(d)**3 for d in str(n))
if s == n:
print(n, end=" ")
Output:
153
9. Fibonacci up to 50
a, b = 0, 1
while a <= 50:
print(a, end=" ")
a, b = b, a+b
Output:
0 1 1 2 3 5 8 13 21 34
10. Reverse List
nums = [1, 2, 3, 4, 5]
rev = []
for i in range(len(nums)-1, -1, -1):
rev.append(nums[i])
print(rev)
Output:
[5, 4, 3, 2, 1]
11. Multiplication Tables 1–2
for n in range(1, 3):
for i in range(1, 6):
print(f"{n} x {i} = {n*i}")
print()
Output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
12. Sum of Squares
n = 5
s = 0
for i in range(1, n+1):
s += i**2
print("Sum of squares:", s)
Output:
Sum of squares: 55
13. Palindrome Number Check
n = 1221
temp, rev = n, 0
while temp > 0:
rev = rev*10 + temp%10
temp //= 10
print("Palindrome" if rev == n else "Not Palindrome")
Output:
Palindrome
14. LCM of Two Numbers
a, b = 15, 20
mx = max(a, b)
while True:
if mx % a == 0 and mx % b == 0:
print("LCM:", mx)
break
mx += 1
Output:
LCM: 60
15. Pyramid Pattern
n = 5
for i in range(1, n+1):
print(" "*(n-i) + "*"*(2*i-1))
Output:
*
***
*****
*******
*********