You are on page 1of 1

Q19.

How can you loop through all the keys in a dictionary and perform operations on their corresponding
values?

my_dict = {'a': 1, 'b': 2, 'c': 3}


for key in my_dict.keys():
value = my_dict[key]
# Perform operations on 'value'
doubled_value = value * 2
print("Key:", key, "Original Value:", value, "Doubled Value:", doubled_value)

In this example, we iterate through each key in the dictionary my_dict. For each key, we access its
corresponding value using my_dict[key] and store it in the value variable. Then, we can perform any desired
operations on value. In this case, we multiply value by 2 and store the result in the doubled_value variable.
Finally, we print the key, original value, and doubled value for demonstration purposes.

Q20. Discuss the importance of if statements in programming and how they contribute to the control flow
of a program.

If statements are important in programming as they allow us to execute different blocks of code based on
specific conditions. They control the flow of a program by determining which sections of code should be
executed and which should be skipped based on the evaluation of conditions.

Q21. Explain the difference between the if-else and if-elif-else statements and when to use each one.

The if-else statement is used when we have two possible outcomes based on a condition. The if-elif-else
statement is used when we have multiple conditions and we want to select one of the branches based on
the first condition that evaluates to true.

Q29. Write a Python program that takes a number as input and checks if it is positive, negative, or zero.
Print an appropriate message based on the condition.

num = float(input("Enter a number: "))


if num > 0:
print("The number is positive.")
elif num < 0:

You might also like