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

Micro Notes Python Programs

The document contains various Python code snippets demonstrating string manipulation, list operations, and tuple handling. Key functionalities include calculating string length, replacing characters, checking string conditions, sorting lists, and finding common elements. Each section provides a specific example of how to perform these tasks using Python syntax.

Uploaded by

ykotwani840
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

Micro Notes Python Programs

The document contains various Python code snippets demonstrating string manipulation, list operations, and tuple handling. Key functionalities include calculating string length, replacing characters, checking string conditions, sorting lists, and finding common elements. Each section provides a specific example of how to perform these tasks using Python syntax.

Uploaded by

ykotwani840
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
You are on page 1/ 2

1.

Length of String (Using len and Loop)


# Using len() function
string = "Hello"
print("Length using len():", len(string))

# Using loop
count = 0
for char in string:
count += 1
print("Length using loop:", count)
2. First & Last 2 Chars of a String
s = "hello"
if len(s) >= 2:
result = s[:2] + s[-2:]
else:
result = ''
print("Result:", result)
3. Replace All Except First Char
s = "restart"
first = s[0]
result = first + s[1:].replace(first, '$')
print("Result:", result)
4. Remove Odd Index Chars
s = "python"
result = ""
for i in range(len(s)):
if i % 2 == 0:
result += s[i]
print("Result:", result)
5. Swap First 2 Chars of 2 Strings
a = "abc"
b = "xyz"
result = b[:2]+a[2:] + ' ' + a[:2]+b[2:]
print("Swapped:", result)
6. Input in Upper & Lower Case
s = input("Enter text: ")
print("Upper:", s.upper())
print("Lower:", s.lower())
7. Check Start & End of String
s = "w3resource.com"
print(s.startswith("w3"))
print(s.endswith(".com"))
8. Format String 2 Ways
name = "Shyam"
age = 30
print("My name is {} and I am {} years old".format(name, age))
print(f"My name is {name} and I am {age} years old")
9. Sort List in Ascending Order
nums = [3, 1, 4, 2]
nums.sort()
print("Sorted:", nums)
10. Sum of List Items Using Loop
nums = [1, 2, 3]
total = 0
for num in nums:
total += num
print("Sum:", total)
11. Largest in List Using Loop
nums = [10, 20, 5]
max_val = nums[0]
for num in nums:
if num > max_val:
max_val = num
print("Max:", max_val)
12. 4th Element from Last (Tuple)
t = (10, 20, 30, 40, 50, 60)
print("4th from end:", t[-4])
13. Count Special Strings in List
words = ["madam", "wow", "aa", "a"]
count = 0
for word in words:
if len(word) >= 2 and word[0] == word[-1]:
count += 1
print("Count:", count)
14. First & Last 5 Squares 1-30
squares = []
for i in range(1, 31):
squares.append(i*i)
print("First 5:", squares[:5])
print("Last 5:", squares[-5:])
15. Common Member in Lists (No def)
list1 = [1, 2, 3]
list2 = [4, 5, 3]
found = False
for item in list1:
if item in list2:
found = True
break
print("Common:", found)
16. Check Element in Tuple
t = (1, 2, 3, 4)
x = 3
if x in t:
print("Exists")
else:
print("Not found")

You might also like