-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-conditionals.py
More file actions
71 lines (55 loc) · 1.36 KB
/
4-conditionals.py
File metadata and controls
71 lines (55 loc) · 1.36 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
# Python uses indentation to define blocks of code, not curly brackets or other symbols.
temp = 28
if temp > 30:
print("It's hot outside")
elif temp > 20:
print("It's a nice day!")
else:
print("It's cold outside")
# Checking multiple conditions with logical operators
age = 25
has_licence = True
if age >= 18 and has_licence:
print("you can drive a car")
elif age >= 18 and not has_licence:
print("you need to get a driver's lincese first")
else:
print("You are too young to drive.")
# Nested conditionals
score = 85
if score >= 60:
print("You passed!")
if score >= 90:
print("You got an A")
elif score >= 80:
print("You got a B!")
elif score >= 70:
print("You got a C!")
else:
print("You got a D!")
else:
print("You failed")
# using the "in" operator with conditionals
fruit = "apple"
if fruit in ["banana", "orange"]:
print(f"{fruit} is in the list")
# Ternary operator (one-line if-else)
age = 20
status = "adult" if age >= 18 else "minor"
print(f"Status: {status}")
# Comparing strings
password = "secret123"
if password == "secret123":
print("Access granted")
else:
print("No access!")
# Chaining comparisons
x = 15
if 10 < x < 20:
print("x is between 10 and 20")
# truthy or falsy
user_input = ""
if user_input:
print("Input provided.")
else:
print("no input provided")