Python Questions
1. Use a loop to display elements from a given list present at odd index positions
Input:
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Expected output:
20 40 60 80 100
2. Write a program to calculate the sum of series up to n term. For example, if n =5 the
series will become 2 + 22 + 222 + 2222 + 22222 = 24690
Input:
number of terms = 5
Expected output:
24690
3. Write a program to print the following star pattern using the for loop
Input:
number of max starts = 5
Expected output:
*
**
***
****
*****
****
***
**
*
4. Append new string in the middle of a given string
Given two strings, s1 and s2. Write a program to create a new string s3 by
appending s2 in the middle of s1.
Input:
s1 = "Ault"
s2 = "Kelly"
Expected Output:
AuKellylt
5. Arrange string characters such that lowercase letters should come first
Given string contains a combination of the lower and upper case letters. Write a
program to arrange the characters of a string so that all lowercase letters should come
first.
Input:
str1 = PyNaTive
Expected Output:
yaivePNT
6. Concatenate two lists index-wise
Write a program to 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.
Input:
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]
Expected output:
['My', 'name', 'is', 'Kelly']
7. Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional
array. (Where X: number of raws, Y: number of columns).The element value in the i-th
row and j-th column of the array should be i*j.
Input:
3,5
Expected output:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
8. Write a program to print the following pattern using 2 inputs (X,Y) from the user.
Hint: assume a 2D matrix of X raws and Y columns (Where X: number of raws, Y:
number of columns).The element value in the i-th row and j-th column of the array
should be i*j.
Input:
3,5
Expected output:
00000
01234
02468
9. Write a program that accepts a sentence and calculate the number of upper case
letters and lower case letters.
Input:
Hello world!
Expected output:
UPPER CASE: 1
LOWER CASE: 9
10. You are required to write a program to sort the (name, age, height) tuples by
ascending order where name is string, age and height are numbers. The tuples are
input by the user. The sort criteria is:
1: Sort based on name;
2: Then sort based on age;
3: Then sort by score.
The priority is that name > age > score.
Input:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Expected output:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
1) Write a Python program to read an entire text file.
2) Write a program that creates a new text file and writes "Hello, World!" to it.
3) Write a Python program to read first n lines of a file
4) Write a program that reads a text file and counts the number of words in it.
5) Write a program that reads a text file and counts the number of lines in it.
6) Write a Python script to copy the contents of one text file to another.
7) Write a Python program to append text to a file and display the text.
8) Write a Python program to count the number of lines in a text file.
9) Write a Python program to make each word in the file start with capital letter.
10) Write a Python program to remove any (“# / * $ % @“) from the text file and print text.
11) Write a Python program to double any number in the file.
12) Write a Python program to replace any word start with “H” to “WOOW”.
13) Write a Python program to remove any word start with “M”.
14) Write a Python program to update any word start with “M” to make it start with “MO”.
15) Write a program in Python to count uppercase character in a text file
1. Python Program to count string and Avoid Spaces in string length.
2. Create a Python code that takes a string as input and counts the number of numbers, vowels
and constants in the string
3. Write a program to arrange string characters such that lowercase letters should come first.
4. Write a function reverse_words(sentence) that takes a string representing a sentence as a
parameter and returns a new string with the words reversed.
5. Write python function to Remove Duplicates from Sorted Array
6. write python function to Prime checking
7. write python function to calculate GCD
8. write python function to acts as a basic calculator.
9. Write a Python program to make each word in the file start with capital letter.
10. Write a Python program to remove any (“# / * $ % @“) from the text file and print text.
11. Write a program in Python to count uppercase character in a text file
12. Write a Python program to write a list to a file
13. Write a Python program to extract characters from various text files and puts them into a list.
14. Write a Python program to read a file and print unique words
15. Create a dictionary with student names as keys and their corresponding grades as values.
Write a program to calculate the average grade.
16. Write a program that takes a tuple of numbers and returns a new tuple with the elements
multiplied by a given factor.
17. Implement a function to check if a key exists in a dictionary.
18. Write a python program to find the longest words
19. Write a program to concatenate two tuples.
20. Write a Python function to find the intersection of two sets.