Basic Python Assignment
1. Write a program to iterate the first 10 numbers, and in each iteration, print the sum of the
current and previous number.
Expected Output:
Printing current and previous number sum in a range(10)
Current Number 0 Previous Number 0 Sum: 0
Current Number 1 Previous Number 0 Sum: 1
Current Number 2 Previous Number 1 Sum: 3
Current Number 3 Previous Number 2 Sum: 5
Current Number 4 Previous Number 3 Sum: 7
Current Number 5 Previous Number 4 Sum: 9
Current Number 6 Previous Number 5 Sum: 11
Current Number 7 Previous Number 6 Sum: 13
Current Number 8 Previous Number 7 Sum: 15
Current Number 9 Previous Number 8 Sum: 17
2. Write a program to accept a string from the user and display characters that are present at
an even index number.
For example, str = "suvarna" so you should display ‘s’, ‘v’, ‘r’, ‘a’.
3. Write a program to remove characters from a string starting from zero up to n and return a
new string.
For example:
remove_chars("suvarna", 4) so output must be positive. Here, we need to remove the first
four characters from a string.
remove_chars("suvarna", 2) so output must be native. Here, we need to remove the first two
characters from a string.
Note: n must be less than the length of the string.
4. Check if the first and last number of a list is the same
Write a function to return True if the first and last number of a given list is same. If numbers
are different then return False.
Test Case:
Input:
numbers_x = [10, 20, 30, 40, 10]
numbers_y = [75, 65, 35, 75, 30]
Output:
Given list: [10, 20, 30, 40, 10]
result is True
numbers_y = [75, 65, 35, 75, 30]
result is False
5. Iterate the given list of numbers and print only those numbers which are divisible by 5
Test Case:
Input:
num_list = [10, 20, 33, 46, 55]
Expected Output:
Given list is [10, 20, 33, 46, 55]
Divisible by 5
10
20
55
6. Write a program to find how many times substring “Suvarna” appears in the given string.
a. Use the string method - count() method
b. Without string method
Test Case:
Input:
str_x = "Suvarna is good program developer. Suvarna is a Chef"
Expected Output:
Suvarna appeared 2 times
7. Print the following pattern
8. Write a program to check if the given number is a palindrome number.
Test Case:
original number 121
Yes. given number is palindrome number
original number 125
No. given number is not palindrome number
9. Create a new list from two list using the following condition
Given two list of numbers, write a program to create a new list such that the new list should
contain odd numbers from the first list and even numbers from the second list.
Test Case:
Input:
list1 = [10, 20, 25, 30, 35]
list2 = [40, 45, 60, 75, 90]
Output:
result list: [25, 35, 40, 60, 90]
10. Write a Program to extract each digit from an integer in the reverse order.
example, If the given int is 7536, the output shall be “6 3 5 7“, with a space separating the
digits.
11. Convert Decimal number to octal using print() output formatting
12. Write a program to display only those numbers from a list that satisfy the following
conditions
The number must be divisible by five
If the number is greater than 150, then skip it and move to the next number
If the number is greater than 500, then stop the loop
Test Case:
Input:
numbers = [12, 75, 150, 180, 145, 525, 50]
Output:
75
150
145
13. Write a program to count the total number of digits in a number using a while loop.
For example, the number is 75869, so the output should be 5.
14. Sort a tuple of tuples by 2nd item
Test case:
Input:
tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Output:
(('c', 11), ('a', 23), ('d', 29), ('b', 37))
15. Return a set of elements present in Set A or B, but not both
Test Case:
Input:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Output:
{20, 70, 10, 60}