-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional_statements.py
More file actions
114 lines (97 loc) · 2.22 KB
/
conditional_statements.py
File metadata and controls
114 lines (97 loc) · 2.22 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
temperature = 15
if temperature > 30:
print("It's warm")
print("Drink water")
elif temperature > 20:
print("It's nice")
else:
print("It's cold")
print("Done")
# ternery operator
age = 22
# if age >= 18:
# message = "Eligible"
# else:
# message = "Not eligible"
message = "Eligible" if age >= 18 else "Not eligible"
print(message)
# logical operators
high_income = False
good_credit = True
student = False
# and
# or
# not
if (high_income or good_credit) and not student:
print("Eligible")
else:
print("Not eligible")
# if high_income and good_credit:
# print("Eligible")
# else:
# print("Not eligible")
# short circuiting concept - if one condition found false it do not checks further it stops
# if found false in case of and operator
# true in case of or operator
# chaining comparison operators
age = 22
if 18 <= age < 65:
print("Eligible")
# quiz
if 10 == "10":
print("a")
elif "bag" > "apple" and "bag" > "cat":
print("b")
else:
print("c")
# for loops
for number in range(3):
print("Attempt", number + 1, (number + 1) * ".")
# for number in range(1,4):
# for number in range(1, 10, 2):
# print("Attempt", number, number * ".")
for i in range(1, 6):
print(i * "*")
for i in range(5, 0, -1):
print(i * "*")
for i in range(1, 6):
print((5-i) * " " + i * "*")
for i in range(1, 6):
print((5 - i) * " " + (2*i - 1) * "*")
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end="")
print()
successful = False
if successful:
print("Successful")
break
else:
print("Attempted 3 times and failed")
for x in range(5):
for y in range(3):
print(f"{x}, {y})")
# iterable
for x in range(5):
# while loops
number = 100
while number > 0:
print(number)
number //= 2
command = ""
while command != "quit":
command = input(">")
print("ECHO", command)
count = 0
for number in range(1, 10):
if number % 2 == 0:
count += 1
print(number)
print(f"We have {count} even numbers")
def greet(first_name, last_name):
# parameters inside def
print(f"Hi {first_name} {last_name}")
print("Welcome aboard")
greet("Mosh", "Hamedani")
greet("John", "Smith")
# arguments are the actual values given to the parameter