Skip to content

Commit 15ff777

Browse files
author
Sahil
committed
Add loops and update lecture2.md
1 parent 971f80c commit 15ff777

8 files changed

+507
-5
lines changed

day2/functions.py

Whitespace-only changes.

day2/jump_statements.py

Whitespace-only changes.

day2/loops.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# print numbers from 1 to 10 to the screen
2+
'''
3+
print(1)
4+
print(2)
5+
...
6+
print(10)
7+
8+
The above is not a good practice since our requirement
9+
might change and we might have to print from 1 to n, where n is the input.
10+
Also, the code is repititive, that is why language provides loop constructs!
11+
'''
12+
13+
# for loops in python do not have initialization, condition, update statement like in other langs
14+
# in keyword is used along with for, to iterate over items in a container or sequence
15+
16+
for n in range(10):
17+
print(n, end=' ')
18+
print() # by default, end = '\n'
19+
20+
# the above code prints '0 1 2 ... 9'
21+
# we want to print '1 2 3 ... 10'
22+
23+
for n in range(1, 11):
24+
print(n, end=' ')
25+
print()
26+
27+
# let us say we want to print all even numbers from 1 to 10
28+
29+
for n in range(2, 11, 2):
30+
print(n, end=' ')
31+
print()
32+
33+
# can use list operator before range, to print the items in the range
34+
print(list(range(2, 11, 2)))
35+
36+
# doing the same thing using a while loop
37+
counter = 0
38+
while (counter < 10):
39+
counter += 1
40+
print(counter, end=' ')
41+
print()
42+
43+
# NOTE: while loops usually have a statement to update the sentinel value (being checked in condition)
44+
# otherwise the loop can run infinitely, use Ctrl-C to exit the infinite loop
45+
# the loop below runs infinitely
46+
47+
'''
48+
counter = 0
49+
while (counter < 10):
50+
print(counter, end=' ')
51+
'''

day2/market.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vegetable_market = ["onion", "tomato", "broccoli", "cabbage"]
2+
3+
if "carret" in vegetable_market:
4+
print("Bought carret!")
5+
elif "cabbage" in vegetable_market:
6+
print("Bought cabbage!")
7+
else:
8+
print("Came back empty handed...")

day2/multiplication_tables.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Program to print multiplication tables of numbers from 1 to n
3+
'''
4+
n = int(input())
5+
6+
for multiple in range(1, 11):
7+
for number in range(1, n + 1):
8+
print(number*multiple, end=' ')
9+
print()
10+
11+
# formatted output
12+
for multiple in range(1, 11):
13+
for number in range(1, n + 1):
14+
print("%4d"%(number*multiple), end=' ')
15+
print()

day2/operators.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,48 @@
1616
print(res_two) # True
1717

1818
user_logged_in = True
19-
print(not user_logged_in)
19+
print(not user_logged_in)
20+
21+
# the integer 0 is always False, and every other number, is True
22+
x = bool(0)
23+
y = bool(-1)
24+
z = bool(1)
25+
print(x, y, z) # False True True
26+
27+
# strings are compared lexicographically, i.e. by ASCII value of the characters
28+
# you can remeber that capital letters come before lower case ones
29+
print("Scaler" > "Interviewbit") # True as 'S' comes after 'I'
30+
print('s' > 'S') # True
31+
print("Scaler" == "Interviewbit") # False
32+
33+
# Identity comparisons, is keyword is used
34+
# if the compared objects are stored in the same memory location, returns true
35+
a = "Scaler"
36+
b = "Scaler"
37+
print(a is b)
38+
print(id(a))
39+
print(id(b))
40+
41+
# bitwise operators
42+
a = 3
43+
b = 5
44+
c = a & b
45+
'''
46+
Bitwise AND (&)
47+
In result, bit is set at those positions where it is set in both the operands
48+
011
49+
& 101
50+
---
51+
001
52+
---
53+
'''
54+
print(c)
55+
56+
# Exercise
57+
a = False
58+
b = True
59+
c = True
60+
61+
print (a or b and c)
62+
63+
# Read about operator precedences

day2/sum_of_digits.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Program to find sum of digits of a number
3+
'''
4+
n = int(input())
5+
6+
# say n = 1234, how do you compute the sum of the digits?
7+
# you see all the digits, 1, 2, 3, 4, and add them up in a go, boom => 10 is the answer
8+
9+
# How do we program the computer to do this for us?
10+
# We should follow the same technique, right?
11+
# Loop through all the digits, and add them up one by one!
12+
# So, our computer will store the result at some place, we need to create a variable
13+
# Say sum = 0
14+
# How to find a particular digit?
15+
# n = 1234, can we say last digit is the one we get after finding remainder when n is divided by 10
16+
# i.e. n % 10 = 4, add this 4 to the sum, now sum = 4
17+
# Now, we can divide n by 10, to get n' = 1234//10 = 123 (integer division!)
18+
# We continue this process, until n becomes zero!!!
19+
# Pseudocode:
20+
'''
21+
n = input from user
22+
sum = 0
23+
while (n > 0)
24+
d = last digit of n (i.e. n % 10)
25+
add d to sum
26+
divide n by 10
27+
print(sum)
28+
'''
29+
30+
sum = 0
31+
while (n > 0):
32+
d = n % 10
33+
sum += d # shorthand for sum = sum + d
34+
n //= 10 # shorthand for n = n//10
35+
print(sum)
36+
37+
# Try yourself:
38+
# 1. Count digits in a number
39+
# 2. Reverse a number
40+
# 3. Find all factors of a number

0 commit comments

Comments
 (0)