-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path01_if_else.py
More file actions
145 lines (109 loc) · 4.41 KB
/
01_if_else.py
File metadata and controls
145 lines (109 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
================================================================================
File: 01_if_else.py
Topic: Conditional Statements - if/else
================================================================================
This file demonstrates the fundamentals of conditional statements in Python.
Conditional statements allow your program to make decisions and execute
different code blocks based on whether conditions are True or False.
Key Concepts:
- if statement: Executes code block if condition is True
- else statement: Executes code block if condition is False
- Comparison operators: ==, !=, <, >, <=, >=
- Logical operators: and, or, not
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. Simple if Statement
# -----------------------------------------------------------------------------
# The most basic form - executes code only if condition is True
age = 18
if age >= 18:
print("You are an adult.")
# -----------------------------------------------------------------------------
# 2. if-else Statement
# -----------------------------------------------------------------------------
# Provides an alternative when the condition is False
temperature = 15
if temperature > 25:
print("It's a hot day!")
else:
print("It's not that hot today.")
# -----------------------------------------------------------------------------
# 3. Comparison Operators
# -----------------------------------------------------------------------------
# Used to compare values and return True or False
x = 10
y = 20
print("\n--- Comparison Operators ---")
print(f"x == y: {x == y}") # Equal to
print(f"x != y: {x != y}") # Not equal to
print(f"x < y: {x < y}") # Less than
print(f"x > y: {x > y}") # Greater than
print(f"x <= y: {x <= y}") # Less than or equal to
print(f"x >= y: {x >= y}") # Greater than or equal to
# -----------------------------------------------------------------------------
# 4. Logical Operators (and, or, not)
# -----------------------------------------------------------------------------
# Combine multiple conditions
age = 25
has_license = True
print("\n--- Logical Operators ---")
# 'and' - Both conditions must be True
if age >= 18 and has_license:
print("You can drive a car.")
# 'or' - At least one condition must be True
is_weekend = True
is_holiday = False
if is_weekend or is_holiday:
print("You can relax today!")
# 'not' - Reverses the boolean value
is_raining = False
if not is_raining:
print("You don't need an umbrella.")
# -----------------------------------------------------------------------------
# 5. Nested if Statements
# -----------------------------------------------------------------------------
# You can place if statements inside other if statements
print("\n--- Nested if Statements ---")
score = 85
attendance = 90
if score >= 60:
print("You passed the exam!")
if attendance >= 80:
print("And you have excellent attendance!")
else:
print("But try to improve your attendance.")
else:
print("You need to study harder.")
# -----------------------------------------------------------------------------
# 6. Truthy and Falsy Values
# -----------------------------------------------------------------------------
# In Python, some values are considered False: 0, "", [], {}, None, False
# Everything else is considered True
print("\n--- Truthy and Falsy Values ---")
empty_list = []
full_list = [1, 2, 3]
if full_list:
print("The list has items.")
if not empty_list:
print("The list is empty.")
# -----------------------------------------------------------------------------
# 7. Ternary Operator (One-line if-else)
# -----------------------------------------------------------------------------
# A compact way to write simple if-else statements
age = 20
status = "adult" if age >= 18 else "minor"
print(f"\nTernary result: You are an {status}.")
# -----------------------------------------------------------------------------
# 8. Practical Example: Simple Login Check
# -----------------------------------------------------------------------------
print("\n--- Practical Example: Login Check ---")
username = "admin"
password = "secret123"
input_user = "admin"
input_pass = "secret123"
if input_user == username and input_pass == password:
print("Login successful! Welcome back.")
else:
print("Invalid username or password.")