00.01. Python Exercises For Sessions 03-06
00.01. Python Exercises For Sessions 03-06
Exercises on Loops
1. Print First 10 natural numbers using while loop.
2. Write a program to print the following number pattern using a loop.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Exercises on Strings
15. Write a program to create a new string made of an input string’s first,
middle, and last character.
16. Write a program to create a new string made of the middle three
characters of an input string.
17. Given two strings, s1 and s2. Write a program to create a new
string s3 by appending s2 in the middle of s1.
18. Given two strings, s1 and s2, write a program to return a new string
made of s1 and s2’s first, middle, and last characters.
Given: s1 = "America"
s2 = "Japan"
Expected Output: AJrpan
19. Given two strings, s1 and s2. Write a program to create a new string
s3 made of the first char of s1, then the last char of s2, Next, the
second char of s1 and second last char of s2, and so on. Any leftover
chars go at the end of the result.
Given:
s1 = "Abc"
s2 = "Xyz"
Expected Output:
Azbycx
20. Write a program to check if two strings are balanced. For example,
strings s1 and s2 are balanced if all the characters in the s1 are present
in s2. The character’s position doesn’t matter.
Exercises on Lists
21. Reverse a list in Python.
22. Write a program to concatenate (add) two lists index-wise. Create a
new list that contains the 0th index item from both the list, then the
1st index item, and so on till the last element. Any leftover items will
get added at the end of the new list.
23. Given a list of numbers. write a program to turn every item of a list
into its square.
24. You have given a Python list. Write a program to find value n in the
list, and if it is present, replace it with 10xn. Only update the first
occurrence of an item.
25. Given a Python list, write a program to remove all occurrences of an
item , n.