7 practice questions & exercises to test your proficiency and understanding of Python’s decision-making statements. Whether you’re a beginner or an experienced programmer, it’s important to test your knowledge and skills regularly to ensure you’re staying up-to-date with the latest developments and best practices. In this article, we’ll provide a set of 7 questions that will help you gauge your proficiency in the Python decision making statements and identify areas where you can improve. So, let’s get started!
Practice Questions
Below are the lists of questions that you should solve to check your understanding of if else statement. You can check the article about how to use if else statements before solving these practise questions. The questions have been categorized as basic, intermediate, and Advanced. Solutions are given at the end of the page. It’s always advised to try to solve it yourself and then if needed look at the solutions. Feel free to comment or ask queries.
Basic Questions
1: Write a Python program to check if a given number is even or odd.
Hint: Input: 7, Output: 7 is odd
2: Write a Python program to check if a given year is a leap year or not.
Hint: Input: 2020 Output: 2020 is a leap year
Intermediate Questions
3: Write a Python program to find the largest of three numbers.
Hint: Input: 4, 8, 2 Output: 8
4: Write a Python program to check if a given string is a palindrome or not.
Hint: Input: ‘racecar’ Output: ‘racecar’ is a palindrome
Advanced Questions
5: Write a Python program to find the largest product of any two numbers in a given list of integers.
Hint: Input: [1, 5, 2, 6, 7, 0, 10, 11] Output: 110
6: Write a Python program to find the intersection of two lists.
Hint: Input: [1, 2, 3, 4], [3, 4, 5, 6] Output: [3, 4]
7: Write a Python program to find the largest prime factor of a given number.
Hint: Input: 13195 Output: 29
Solutions
1: Solution Question 1
number = int(input("Enter a number: ")) if number % 2 == 0: print(number, "is even") else: print(number, "is odd")
2: Solution Question 2
year = int(input("Enter a year: ")) if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): print(year, "is a leap year") else: print(year, "is not a leap year")
3: Solution Question 3
a,b,c = map(int, input().split()) largest_number = a if b > a: largest_number = b if c > largest_number: largest_number = c print(largest_number)
4: Solution Question 4
string = input("Enter a string: ") if string == string[::-1]: print(string, "is a palindrome") else: print(string, "is not a palindrome")
5: Solution Question 5
list1 = [1, 5, 2, 6, 7, 0, 10, 11] list1.sort(reverse=True) total = 1 for num in range(2): total *= list1[num] print(total)
6: Solution Question 6
list1 = [1,2,3,4] list2 = [3,4,5,6] output_list = [] for element in list1: if element in list2: output_list.append(element) print(output_list)
7: Solution Question 7
def largest_prime_factor(n): i = 2 while i <= n: if n % i == 0: n = n / i largest_factor = i else: i += 1 return largest_factor print(largest_prime_factor(13195))
Solving the above Python decision-making problems can significantly improve your problem-solving skills and logical thinking ability. These problems cover a wide range of difficulty levels, which will help you gain a deep understanding of how decision-making constructs work in Python and how they can be used to solve complex problems. These skills are valuable for anyone looking to develop their programming skills, especially for those interested in data science, machine learning, and artificial intelligence. Additionally, practicing with these problems can help you prepare for coding interviews and improve your chances of landing a job in the tech industry.