-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_conditionals.py
More file actions
80 lines (69 loc) · 1.86 KB
/
03_conditionals.py
File metadata and controls
80 lines (69 loc) · 1.86 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
# If statements - making decisions in code
# Basic if statement
temperature = 30
if temperature > 25:
print("It's hot outside")
print("Wear light clothes")
# if-else
age = 17
if age >= 18:
print("You can vote")
else:
print("Too young to vote")
# if-elif-else (multiple conditions)
score = 75
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
# Nested if statements
has_ticket = True
age = 20
if has_ticket:
if age >= 18:
print("Welcome to the concert")
else:
print("Sorry, adults only")
else:
print("Please buy a ticket first")
# Multiple conditions
money = 50
is_weekend = True
if money >= 30 and is_weekend:
print("Let's go to the movies")
# Checking multiple things
weather = "rainy"
if weather == "sunny" or weather == "cloudy":
print("Good day for a walk")
else:
print("Stay inside")
# Real world example - login system
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "12345":
print("Login successful")
else:
print("Invalid credentials")
# Another practical example - discount calculator
purchase_amount = float(input("Enter purchase amount: "))
if purchase_amount >= 100:
discount = purchase_amount * 0.2 # 20% discount
print(f"You get 20% off! Discount: ${discount}")
print(f"Final price: ${purchase_amount - discount}")
elif purchase_amount >= 50:
discount = purchase_amount * 0.1 # 10% discount
print(f"You get 10% off! Discount: ${discount}")
print(f"Final price: ${purchase_amount - discount}")
else:
print(f"Total: ${purchase_amount}")
print("Spend $50 or more for a discount!")
# Ternary operator (shorthand if-else)
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)