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

00.01. Python Exercises For Sessions 03-06

Uploaded by

oro mars
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)
17 views4 pages

00.01. Python Exercises For Sessions 03-06

Uploaded by

oro mars
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/ 4

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

3. Write a program to accept a number from a user and calculate the


sum of all numbers from 1 to a given number, n.
4. Write a program to print multiplication table (up to 10) of a given
number, n. Constraints 1≤ n ≤ 10
5. Write a program to count the total number of digits in a number
using a while loop.
6. Write a program to use for loop to print the following reverse
number pattern.

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

7. Display numbers from -10 to -1 using for loop.


8. Use else block to display a message “Done” after successful execution
of for loop
9. Write a program to display all prime numbers within a range. Take
the range lower and upper bounds from the user as an input.
Exercises on Functions
10. Write a program to create a function that takes two arguments, name
and age, and print their value.
11. Write a program to create function which accepts a variable length of
arguments and print their value.
12. Write a program to create function calculation() such that it can
accept two variables and calculate addition and subtraction. Also, it
must return both addition and subtraction in a single return call.
13. Write a program to create a function show_employee() using the
following conditions.
 It should accept the employee’s name and salary and display
both.
 If the salary is missing in the function call then assign default
value 9000 to salary
14. Write a program to create a recursive function to calculate the sum
of numbers from 0 to 10.

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.

You might also like