Python Practice Set 2
Python Practice Set 2
Set 2
Q1. Create a list in python using the followings: 2,3,4,5,6,7 with variable ‘a’
Q2.
L = [1,2,3,4,5,6,7]
Q3. Reverse the order of given string L = [4,5,6,8,3] Without using reverse() function.
Q5. Create a function that takes in a tuple of integers and returns the sum of the integers. Test the
function with a tuple of your choice.
Q6. Create two sets of your favourite fruits, and use the union() method to combine them into a single set.
Print the resulting set to the console.
Q7. Create a set of random words, and use the add() method to add a new word to the set. Print the
resulting set to the console.
Q8. Create a set of your favourite animals, and use the remove() method to remove one animal from the
set. Print the resulting set to the console.
Use the zip() function to combine the book set and movie list into a list of tuples representing book/
Q10. Write a Python program to find the difference between consecutive numbers in a list.
Solutions:
# Exercise 6
combined_fruits = fruits1.union(fruits2)
print(combined_fruits)
# Exercise 7
words.add("programming")
print(words)
animals.remove("cat")
print(animals)
# Solution 9
print(book_movie_pairs)
# Solutions 10
def find_diff_consecutive_numbers(lst):
diffs = []
diffs.append(diff)
return diffs
# example usage
diffs = find_diff_consecutive_numbers(my_list)