SET 1 Exercise
SET 1 Exercise
Exercise1 (Lists)
1. Create a list with 5 items (names of people) and write a python program to output the
2nd item.
2. Write a python program to change the value of the first item to a new value
3. Write a python program to add a sixth item to the list
4. Write a python program to add “Bathel” as the 3rd item in your list
5. Write a python program to remove the 4th item from the list
6. Use negative indexing to print the last item in your list
7. Create a new list with 7 items and use a range of indexes to print the 3 rd, 4th and 5th
items.
8. Write a list of countries and make a copy of it.
9. Write a python program to loop through the list of countries
10. Write a list of animal names and sort them in both descending and ascending order.
11. Using the list above, write a python program to output only animal names with the
letter ‘a’ in them
12. Write two lists, one containing your first names and the other your second names. Join
the two lists.
Exercise2 (Tuples)
2. Use negative indexing to print the 2nd last item in your tuple.
3. Using the phones list above, write a python program to update “iphone” to “itel”
4. Write a python program to add “Huawei” to your tuple.
5. Write a python program to loop through the tuple above.
6. Write a python program to remove/delete the first item in your tuple.
7. Using the tuple() constructor, create a tuple of the cities in Uganda.
8. Write a python program to unpack your tuple.
9. Use a range of indexes to print the 2nd, 3rd and 4th cities in your tuple above.
10. Write two tuples, one containing your first names and the other your second names.
Join the two tuples.
11. Create a tuple of colors and multiply it by 3.
12. Return the number of times 8 appears in this tuple
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
Exercise3 (Sets)
Exercise4 (Strings)
1. Declare two variables, an integer and a string and use the correct method to
concatenate the two variables.
2. Consider the example below;
txt = “ Hello, Uganda! ”
Output the string without spaces at the beginning, in the middle and at the end.
3. Write a python program to convert the value of ‘txt’ to uppercase.
4. Write a python program to replace character ‘U’ with ‘V’ in the string above.
5. Using the code below, write a python program to return a range of characters in the 2 nd,
3rd and 4th position.
y = “I am proudly Ugandan”
6. The piece of code below will give an error when output;
x = “All “Data Scientists” are cool!”
Write a python program to correct it.
Exercise5 (Dictionaries)
1. With reference to the dictionary below, write a python program to print the value of the
shoe size.
Add this dictionary to your .py file
Shoes = {
“brand” : “Nick”,
“color” : “black”,
“size” : 40
}
2. Write a python program to change the value “Nick” to “Adidas”
3. Write a python program to add a key/value pair "type”: "sneakers" to the dictionary
4. Write a python program to return a list of all the keys in the dictionary above.
5. Write a python program to return a list of all the values in the dictionary above.
6. Check if the key “size” exists in the dictionary above.
7. Write a python program to loop through the dictionary above.
8. Write a python program to remove “color” from the dictionary.
9. Write a python program to empty the dictionary above.
10. Write a dictionary of your choice and make a copy of it.
11. Write a python program to show nested dictionaries