Skip to content

Commit 971f80c

Browse files
author
Sahil
committed
Add operators and conditionals
1 parent 2f36598 commit 971f80c

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

day2/conditionals.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
This program checks if a message is rude or polite
3+
A message is rude if the parameter x is less than 1 or greater than 10
4+
'''
5+
6+
x = int(input())
7+
8+
if (x < 1):
9+
print("Rude message")
10+
11+
if (x > 10):
12+
print("Rude message")
13+
14+
'''
15+
The problem with the above code is redundancy!
16+
A principal rule of programming is to never ever have
17+
two pieces of code to do the same thing.
18+
It can be improved as shown below:
19+
'''
20+
if (x < 1 or x > 10):
21+
print("Rude message")
22+
23+
if (x >= 1 and x <= 10):
24+
print("Polite message")
25+
26+
'''
27+
The problem with above code is robustness
28+
what if the condition of rudeness changes and we will have to
29+
rewrite both the conditions above,
30+
we can simplify it using an else construct :)
31+
'''
32+
33+
if (x < 1 or x > 10):
34+
print("Rude message")
35+
else:
36+
print("Polite message")
37+
38+
# if elif
39+
# say if only x < 1 are rude messages
40+
# from 1 to 10 are polite messages
41+
# and after 10 are other types!
42+
if (x < 1):
43+
print("Rude messsage")
44+
elif (x >= 1 and x <= 10):
45+
print("Polite message")
46+
else:
47+
print("Any other message")

day2/operators.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# comparison operators
2+
a = 3
3+
less_than_3 = a < 3
4+
print(less_than_3) # False
5+
print(type(less_than_3)) # <class 'bool'>
6+
7+
# other relational operators: >, >=, ==, !=
8+
print(a == 3) # True
9+
print(a != 3) # False
10+
11+
# logical operators: or, and, not
12+
x = 5
13+
res_one = x < 1 or x > 10
14+
res_two = x >= 1 and x <= 10
15+
print(res_one) # False
16+
print(res_two) # True
17+
18+
user_logged_in = True
19+
print(not user_logged_in)

lecture2.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@
44
[Basics of Python](day2/recap.py)
55

66
## Operators
7+
[Code](day2/operators.py)
8+
9+
### Relational Operators
10+
- comparison operators (<, >, <=, >=)
11+
- Equals (==)
12+
- Not equals (!=)
13+
14+
### Logical Operators
715
- and
816
- or
917
- not
10-
- comparisons (<, >, ==, !=)
1118

1219
## Conditionals
13-
- if
14-
- else
15-
- elif
20+
[Code](day2/conditionals.py)
21+
22+
- Decision making statements in programming languages decide the direction of flow of program execution.
23+
24+
- There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next.
25+
26+
- Similar situations arise in programming also where we need to make some decisions and based on these decision we will execute the next block of code.
27+
28+
- Majorly, the following 3 conditional constructs are used in Python:
29+
- if
30+
- else
31+
- elif
1632

1733
## Loops
1834
- for

0 commit comments

Comments
 (0)