-
Notifications
You must be signed in to change notification settings - Fork 11
/
8-DecisionStatemets.py
123 lines (108 loc) · 2.76 KB
/
8-DecisionStatemets.py
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
#Python control flows
'''
Python uses the usual flow control statements known
from other languages, with some twists.
Perhaps the most well-known statement type is the
'''
#if statement.
'''
Think of an if statement as a way to check to see if
conditions are met!
'''
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even")
#if else
'''
If a condition is met, do something...
else do something different!
'''
age = int (input("Enter your age? "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
#elif 'elif' stands for 'else if'
'''
both elif and else are optional!
'''
number = int(input("Enter the number?"))
if number == 10:
print("number is equals to 10")
elif number == 50:
print("number is equal to 50");
elif number == 100:
print("number is equal to 100");
else:
print("number is not equal to 10, 50 or 100");
#Match statement
'''
Hot off the press in Python 3.10
A match statement takes an expression and compares
its value to successive patterns given as one or
more case blocks.
Note: We have a class in this demo. Don't get too caught
up in how it
works! We have a class video in this course :)
'''
#basics
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
def http_error(status):
match status:
case 400 | 401 | 403 | 404:
return "Not allowed"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
#Practise
#when to know everything about tuple and class
#Patterns can look like unpacking assignments, and can be used to bind variables:
# point is an (x, y) tuple
def http_error(point):
match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")
point_tuple = (0,0)
point_tuple = (0,123)
point_tuple = (123,0)
point_tuple = (123,456)
#Match class
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
def where_is(point):
match point:
case Point(x=0, y=0):
print("Origin")
case Point(x=0, y=y):
print(f"Y={y}")
case Point(x=x, y=0):
print(f"X={x}")
case Point():
print("Somewhere else")
case _:
print("Not a point")
where_is(Point(0, 0))
where_is(Point(0, 10))
where_is(Point(10, 0))
where_is(Point(10, 10))