Conditional statements:
- Python conditional statements execute different blocks of code based on whether a specific condition evaluates to True or False.
1. The if Statement:
- The if statement evaluates a single condition.
- If the condition is true, the block of code runs.
Syntax:
if condition:
# block of code to execute if condition is True
Example:
age = 20
if age >= 18:
print("Eligible to vote.")
Output:
Eligible to vote.
2. The if-else Statement:
- If Else statement is used to execute one block of code when the condition is True and another block when the condition is False.
Syntax:
if condition:
# block of code if condition is True
else:
# block of code if condition is False
Example:
age = 10
if age <= 12:
print("Travel for free.")
else:
print("Pay for ticket.")
Output:
Travel for free.
3. If-elif-else Statement:
- elif statement is used to check multiple conditions in a program.
- It executes a block of code when its condition evaluates to True after previous conditions evaluate to False.
Syntax:
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# default code block if all above are False
Example:
mark1 = 90
mark2 = 95
if mark1 > mark2:
print("mark1 is greater")
elif mark2 > mark1:
print("mark2 is greater")
else:
print("both are equal")
Output:
mark2 is greater
Programming Golden Rules:
1) Never say "I don't know." Say "Let me try."
2) Known To Unknown
3) Don't Think about entire output
4) Think about very next step
5) Introduce a variable only when it simplifies the solution.
6) Micro To Macro
7) Dry Run Before You Run
8) Every Program Has Three Parts: Input, Process, Output
9) Logic First, Syntax Next
IndentationError:
- Indentation in Python refers to the whitespaces (spaces or tabs) added at the beginning of a line of code.
- Python strictly requires indentation to define blocks of code and program structure.
- An IndentationError occurs when the structural spacing of your code does not align correctly, which prevents the interpreter from determining where code blocks start or end.
SyntaxError:
- Python syntax is the set of rules that defines how Python programs are written and interpreted.
- A SyntaxError in Python occurs when the interpreter encounters code that breaks the grammatical and structural rules of the language.
Check if a character is a vowel or consonant(tbd)
char = input("Enter a character: ").lower()
if char in 'aeiou':
print(f"{char} is a vowel")
elif char.isalpha():
print(f"{char} is a consonant")
else:
print("Not a valid alphabet")
Determine the season based on month(tbd)
month = int(input("Enter month number (1-12): "))
if month in [12, 1, 2]:
season = "Winter"
elif month in [3, 4, 5]:
season = "Spring"
elif month in [6, 7, 8]:
season = "Summer"
elif month in [9, 10, 11]:
season = "Autumn"
else:
season = "Invalid month"
print(f"Season: {season}")
Check password strength(tbd)
password = input("Enter password: ")
if len(password) >= 8 and any(c.isupper() for c in password) and any(c.isdigit() for c in password):
strength = "Strong"
elif len(password) >= 6:
strength = "Medium"
else:
strength = "Weak"
print(f"Password strength: {strength}")



Top comments (0)