0% found this document useful (0 votes)
12 views2 pages

Python If Else Dictionary Questions

Uploaded by

todrishti18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Python If Else Dictionary Questions

Uploaded by

todrishti18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python If-Else and Dictionary/ Tuple Tricky Questions with

Solutions

Q1. Check if a Number is Odd or Even

num = int(input("Enter a number: ")) if num % 2 == 0: print("Even") else:


print("Odd")

Q2. Find the Greatest of Three Numbers

a, b, c = 10, 25, 7 if a > b and a > c: print("a is greatest") elif b > c:


print("b is greatest") else: print("c is greatest")

Q3. Traffic Light System

light = input("Enter light color: ").lower() if light == "red":


print("Stop") elif light == "yellow": print("Wait") elif light ==
"green": print("Go") else: print("Invalid color")

Q4. Grade Assignment Based on Marks

marks = int(input("Enter marks: ")) if 90 <= marks <= 100: print("O")


elif 80 <= marks < 90: print("A+") elif 70 <= marks < 80: print("A") elif
60 <= marks < 70: print("B") elif 50 <= marks < 60: print("C") elif 40 <=
marks < 50: print("P") elif marks < 40: print("F") else: print("Invalid
Marks")

Q5. Bonus Tricky Questions

# Palindrome check s = "madam" if s == s[::-1]: print("Palindrome") else:


print("Not Palindrome") # Divisible by 3 and 5 num = 15 if num % 3 == 0
and num % 5 == 0: print("Divisible by 3 and 5") else: print("Not
Divisible by 3 and 5") # Vowel or Consonant ch = 'a' if [Link]() in
"aeiou": print("Vowel") else: print("Consonant")

Q6. Dictionary Tricky Questions

sentence = "apple banana apple mango banana apple" words =


[Link]() freq = {} for w in words: freq[w] = [Link](w, 0) + 1
print(freq)

data = {"a": 10, "b": 55, "c": 32} max_key = max(data, key=[Link])
print(max_key)
fruits = {"apple": 1, "banana": 2, "mango": 3} swapped = {v: k for k, v
in [Link]()} print(swapped)

d1 = {"a": 1, "b": 2} d2 = {"c": 3, "d": 4} merged = {**d1, **d2}


print(merged) # Alternative: merged = d1 | d2 (Python 3.9+) # Or:
[Link](d2)

data = {"x": 5, "y": 10} key = "y" if key in data: print("Key exists")
else: print("Key does not exist")

n = 5 squares = {i: i**2 for i in range(1, n+1)} print(squares)

data = {"a": 1, "b": 2, "c": 1, "d": 3} unique = {} for k, v in


[Link](): if v not in [Link](): unique[k] = v print(unique)

data = {"x": 40, "y": 10, "z": 25} sorted_dict =


dict(sorted([Link](), key=lambda item: item[1])) print(sorted_dict)

You might also like