Centre for Data Science
Institute of Technical Education & Research, SOA, Deemed to be University
Applied Computational Thinking (CSE 1401)
M INOR A SSIGNMENT-5: L OOPS AND FUNCTIONS IN P YTHON
1. Create a simple text-based game where the player can choose actions (e.g., ”move forward,” ”turn
left,” ”quit”). Use a break statement to exit the loop when the player chooses ”quit.”
2. Write a Python program that takes a number as input from the user and checks whether it is a perfect
square. Display an appropriate message based on the result.
3. Create a program that checks if the temperature is either below 0°C (cold) or above 30°C (hot) and
alerts the user accordingly. Use Boolean operators to determine the conditions.
4. Write a Python program that prints Floyd’s triangle.
5. Write a Python program that prints an inverted triangle of stars, where the number of rows is taken as
input from the user.
6. Write a Python program that uses nested loops to count how many times each character appears in a
given string.
7. Write a Python program that takes an integer n and prints the sum of even and odd numbers separately
from 1 to n.
8. Write a Python program that prints a multiplication table in reverse order. The user should input the
value of n for which the table needs to be printed.
9. Write a Python program that checks for Armstrong numbers in a given range from 1 to n. An Arm-
strong number is a number that is equal to the sum of its digits each raised to the power of the number
of digits.
10. Write a Python program that prints a pattern using both numbers and stars. The pattern should alter-
nate between numbers and stars in each row.
1*2*3*
*4*5*6
7*8*9*
* 10 * 11 *
11. Write a function hours to seconds(hours) that converts hours to seconds (1 hour = 3600 seconds).
12. rite a function is divisible(a, b) that checks if a is divisible by b. Test the function with different pairs
of numbers.
13. Write a function multiply numbers(a, b) that returns the product of two numbers a and b. Call the
function with any two numbers and print the result.
14. Write a function swap numbers(a, b) that returns the product of two numbers a and b. Call the function
with any two numbers and print the result.
15. Write a function greet(name=”Student”) that prints a greeting message. If no name is provided, it
should use the default value ”Student”. Test the function with and without passing a name.
1
Centre for Data Science
Institute of Technical Education & Research, SOA, Deemed to be University
16. Write a function is even(n) that checks whether a number n is even or odd. Test the function with
different numbers.
17. Write a function factorial(n) that calculates the factorial of a number n using recursion. Test the
function with a number (e.g., 5).
18. Write a function fibonacci(n) that returns the nth Fibonacci number. Test the function by printing the
first 10 Fibonacci numbers.
19. Write a function multiplication table(n) that prints the multiplication table of a number n (up to 10).
Test the function with a number (e.g., 6).
20. Write a function is prime(n) that checks if a number n is prime. Test the function with several num-
bers.
21. Write a function count vowels(string) that counts the number of vowels in a given string. Test the
function with some strings.
22. Write a function reverse string(s) that reverses the given string s. Test the function with a word or
sentence.
23. Write a function calculator() that takes two numbers and an operation (+, -, *, /) as input and returns
the result. Test the function with different operations.
24. Write a function sort list(lst) that sorts a list of numbers in ascending order without using the built-in
sort() method. Test the function with a list of unsorted numbers.
25. Write a function sum of digits in string(s) that calculates the sum of all numeric digits in a string s.
Test the function with strings containing digits and letters.
26. Write a function find longest word(words) that returns the longest word in a list of strings. Test the
function with a list of words.
27. Write a recursive function factorial recursive(n) to calculate the factorial of a number n. Compare the
result with the iterative version.