1.
If-Else Statement
python
# Program to check if a number is positive, negative,
or zero
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
2. Nested If Statement
python
# Program to check if a number is positive, negative,
or zero, using nested if
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
3. For Loop
python
# Program to print the first 10 natural numbers
for i in range(1, 11):
print(i)
4. While Loop
python
# Program to calculate the factorial of a number
using a while loop
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factorial does not exist for negative
numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
while num > 0:
factorial *= num
num -= 1
print("Factorial is", factorial)
5. Break Statement
python
# Program to demonstrate break in a loop
for i in range(1, 11):
if i == 5:
break
print(i)
6. Continue Statement
python
# Program to demonstrate continue in a loop
for i in range(1, 11):
if i == 5:
continue
print(i)
7. Pass Statement
python
# Program to demonstrate the use of pass statement
for i in range(1, 6):
if i == 3:
pass # Pass does nothing, just a placeholder
print(i)
8. Else with Loop
python
# Program to show the use of else with a loop
for i in range(1, 6):
print(i)
else:
print("Loop ended.")
9. Switch Case (Using Dictionary)
Python doesn’t have a switch-case construct, but it can be mimicked using a
dictionary.
python
# Program to mimic switch-case using a dictionary
def switch_case(argument):
switcher = {
1: "January",
2: "February",
3: "March",
4: "April"
}
return switcher.get(argument, "Invalid month")
month = int(input("Enter a month number: "))
print(switch_case(month))
10. For Loop with Else
Python
# Program to demonstrate for-else loop
for i in range(1, 6):
print(i)
else:
print("The loop ended successfully")