0% found this document useful (0 votes)
40 views7 pages

Python Programming Exercises

The document contains a series of Python programming exercises that cover various topics such as string manipulation, list operations, dictionary handling, and user input. Each exercise includes a problem statement followed by a solution in Python code. The exercises are designed to help users practice and improve their programming skills.

Uploaded by

Rawan Tamer
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)
40 views7 pages

Python Programming Exercises

The document contains a series of Python programming exercises that cover various topics such as string manipulation, list operations, dictionary handling, and user input. Each exercise includes a problem statement followed by a solution in Python code. The exercises are designed to help users practice and improve their programming skills.

Uploaded by

Rawan Tamer
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

1.

Write a python program to print the following :

Sample string
A string that you don’t have to escape

This

Is a ……... multi-line

Here doc string--------> example

Solution
message = """
Sample string
A string that you don’t have to escape
This
Is a ……. multi-line
Here doc string--------> example
"""
print(message)

2. Write a python program to display the date from ex_date=(11,6,2022)


Sample output: The exam date will start from : 11 / 6 / 2022
Solution
ex_date = (11, 6, 2022)
print("The exam date will start from : %d / %d / %d"
% (ex_date[0], ex_date[1], ex_date[2]))

3. Write a python program to display the first and last colors from the following
list:
Color_list = [“Red”, “Green”, ”White”, “Black”] (Hint: use %s format)
Solution
Color_list = ["Red", "Green", "White", "Black"]
print("The First Color Is %s And The Last Color Is %s"
%(Color_list[0], Color_list[-1]))

4. Write a python program to print out a set containing all the colors from

color_list1 which are not present in color_list2.


Test Data:
Color_list1 = [“Red”,”White”, “Black”]
Color_list2 = [“Red”, “Green”] Expected
Output:
{‘Black’, ‘White’}
Solution
Color_list1 = ["Red", "White", "Black"]
Color_list2 = ["Red", "Green"]
print(set(Color_list1)-set(Color_list2))

5. Write a python program to input two integers in a single line.


Solution
a, b = map(int, input("Enter Two Integers = ").split())
print(a, "\n", b)

6. Create a list of two sports. Ask the user what their favorite sport is and add this

to the end of the list. Sort the list and display it.

Solution
sports = ["Judo", "Boxing"]
[Link](input("What Is Your Favorite Sport ?"))
print(sports)
[Link]()
print(sports)

7. Create a list of six school subjects. Ask the user which of these subjects they don’t

like. Delete the subject they have chosen from the list before you display the list
again.

Solution

subjects = ['Math', 'Arabic', 'English', 'Physics',


'Chemistry', 'Biology']
[Link](input('Which of these subjects you don’t
like ? '))
print(subjects)

8. Ask the user to enter four of their favourite foods and store them in a dictionary

so that they are indexed with numbers starting from 1. Display the dictionary in
full, showing the index number and the item. Ask them which they want to get
rid of and remove it from the list. Sort the remaining data and display the
dictionary.

Solution
F1 = input("Enter First Of Your Favourite Food : ")
F2 = input("Enter First Of Your Favourite Food : ")
F3 = input("Enter First Of Your Favourite Food : ")
F4 = input("Enter First Of Your Favourite Food : ")
Foods = {1: F1, 2: F2, 3: F3, 4: F4}
print(Foods)
d = int(input("Which Of They You Want To Get Rid Of ? "))
del Foods[d]
data = list([Link]())
[Link]()
print(data)
print(Foods)

9. Enter a list of ten colours. Ask the user for a starting number between 0 and 4

and an end number between 5 and 9. Display the list for those colours between
the start and end numbers the user input.

Solution
Colors = ['white', 'black', 'red', 'green', 'blue',
'yellow', 'orange', 'pink', 'gray', 'gold']
start = int(input('choose starting number between 0 and 4
: '))
end = int(input('choose end number between 5 and 9 : '))
print(Colors[start:end])

10. Using the 2D list, ask the user to select a row and a column and display that value.
Solution
list = [[2, 5, 8], [3, 7, 4], [1, 6, 9], [4, 2, 0]]
row = int(input('Select a Row : '))
column = int(input('Select a Column : '))
print(list[row][column])

11. Using the 2D list from program 10 , ask the user which row they would like

displayed and display just that row. Ask them to enter a new value and add it to
the end of the row and display the row again.

Solution
List = [[2, 5, 8], [3, 7, 4], [1, 6, 9], [4, 2, 0]]
row = int(input('Select a Row : '))
print(List[row])
value = int(input('Enter a New Value : '))
List[row].append(value)
print(List[row])

12. Create the following using a 2D dictionary showing the sales each person has

made in the different geographical regions:


Solution
sales = {
'John': {'N': 3056, 'S': 8463, 'E': 8441, 'W': 2694},
'Tom': {'N': 4832, 'S': 6786, 'E': 4737, 'W': 3612},
'Anne': {'N': 5239, 'S': 4802, 'E': 5820, 'W': 1859},
'Fiona': {'N': 3904, 'S': 3645, 'E': 8821, 'W': 2451}
}
print(sales)

13. Using program 12, ask the user for a name and a region. Display the relevant data.

Ask the user for the name and region of data they want to change and allow them
to make the alteration to the sales figure. Display the sales for all regions for the
name they choose.

Solution
sales = {
'John': {'N': 3056, 'S': 8463, 'E': 8441, 'W': 2694},
'Tom': {'N': 4832, 'S': 6786, 'E': 4737, 'W': 3612},
'Anne': {'N': 5239, 'S': 4802, 'E': 5820, 'W': 1859},
'Fiona': {'N': 3904, 'S': 3645, 'E': 8821, 'W': 2451}
}
print(sales)

name = input('What Is Your Name ? ')


region = input('What Is Your Region ? ')
print(sales[name][region])

Cname = input('What Is Name Of Data You Want To Change ? ')


Cregion = input('What Is Region Of Data You Want To Change
? ')
data = int(input('New Data : '))
sales[Cname][Cregion] = data
print(sales[Cname])

You might also like