File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ #https://newdigitals.org/2024/01/23/basic-python-programming/#conditionals
2
+ # Conditionals
3
+ # Python Conditions and If statements support the usual logical conditions
4
+ #Equals: a == b
5
+ #Not Equals: a != b
6
+ #Less than: a < b
7
+ #Less than or equal to: a <= b
8
+ #Greater than: a > b
9
+ #Greater than or equal to: a >= b
10
+
11
+ #If statement:
12
+ a = 33
13
+ b = 200
14
+ if b > a :
15
+ print ("b is greater than a" )
16
+ Output :
17
+ b is greater than a
18
+
19
+ #The elif keyword is Python’s way of saying “if the previous conditions were not true, then try this condition”
20
+ a = 33
21
+ b = 33
22
+ if b > a :
23
+ print ("b is greater than a" )
24
+ elif a == b :
25
+ print ("a and b are equal" )
26
+ Output :
27
+ a and b are equal
28
+
29
+ #The else keyword catches anything which isn’t caught by the preceding conditions.
30
+
31
+ a = 200
32
+ b = 33
33
+ if b > a :
34
+ print ("b is greater than a" )
35
+ elif a == b :
36
+ print ("a and b are equal" )
37
+ else :
38
+ print ("a is greater than b" )
39
+ Output :
40
+ a is greater than b
41
+
42
+ #Test if a is greater than b, AND if c is greater than a:
43
+ a = 200
44
+ b = 33
45
+ c = 500
46
+ if a > b and c > a :
47
+ print ("Both conditions are True" )
48
+ Output :
49
+ Both conditions are True
50
+
51
+ #Test if a is greater than b, OR if a is greater than c:
52
+ a = 200
53
+ b = 33
54
+ c = 500
55
+ if a > b or a > c :
56
+ print ("At least one of the conditions is True" )
57
+ Output :
58
+ At least one of the conditions is True
59
+
60
+ #Test if a is NOT greater than b:
61
+ a = 33
62
+ b = 200
63
+ if not a > b :
64
+ print ("a is NOT greater than b" )
65
+ Output :
66
+ a is NOT greater than b
67
+ #You can have if statements inside if statements, this is called nested if statements
68
+ x = 41
69
+
70
+ if x > 10 :
71
+ print ("Above ten," )
72
+ if x > 20 :
73
+ print ("and also above 20!" )
74
+ else :
75
+ print ("but not above 20." )
76
+ Output :
77
+ Above ten ,
78
+ and also above 20 !
79
+
80
+
You can’t perform that action at this time.
0 commit comments