Basic Python Assignment
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.
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
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
Input:
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.