22ise251-Qb-Module 2 Updated 1
22ise251-Qb-Module 2 Updated 1
Question Bank
Module 2
Functions
12. Write a function named collatz() that has one parameter named number.
If number is even, thencollatz() should print number // 2 and return this value.
If number is odd, then collatz() should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that keeps calling
collatz() on thatnumber until the function returns the value 1.
14. Write a Python function to take an integer as an argument and return the day of the week using elif
statement.
Module 2
Lists
1. Define List in Python. Explain how lists are created and accessed with suitable example.
2. Explain the working of following List Methods in Python with suitable examples.
index() method
insert() method
remove() method
sort() method
append() method
3. Explain the working of in and not in Operators in List with suitable examples.
4. Demonstrate the following in Python
Getting a List’s Length with len() function
Changing Values in a List with Indexes
Getting sub-lists with Slices
List Concatenation and List Replication
Removing Values from Lists with del Statements
9. Write a Python program to initialize with values in a list, input two positions a and b, swap
elements in the positions a and b, print the modified list.
10. Write a Python program to
Declare the empty list by the name sample
Update the list value to 1, 2, 3, 4, 5 in sequence
Insert value 10 at position 3 and print list
Swap first element with last element
Print the list after each operation
11. Write a Python program to initialize a list with 10 integers, sort them in descending order and print
the modified list.
12. Given the following List.
spam = ['apples', 'bananas', 'tofu', 'cats']
Write a function in Python that takes a list value as an argument and returns a string with all the
items separated by a comma and a space, with and inserted before the last item. For example, passing
the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function
should be able to work with any list value passed to it.
13. Write a Python program to find sum of all the items in a list using function.
14. Write a Python program to count positive and negative numbers in a List.
15. Write a Python program to count Even and Odd numbers in a List.
16. Write a Python program to find product of all the items in a list using function.