Day 3

PS: These are notes, there could be grammar error and spelling errors.

Last time we were just starting with If else, the control flow,

if-else , the control flow

Control flow is used when we need to do some decisive task

if, if else, if elif else statement

If statement is simple as it is other language

1
2
if expression:
    do this
1
2
3
4
if expression:
    do this
else:
    do this
1
2
3
4
5
6
if expression:
    do this
elif expression:
    do this
else:
    do this

Things to note:

  • ‘:’ is must
  • Indentation must be proper.
  • if value is going to be true or false check it directly in if or elif expression without comparison operators.

Looping

Looping is used whenever you need to do repetitive task with some condition

while loop

1
2
3
while condition:
    statement1
    statement2

while loop we execute until condition is meet.

generating fibonacci using while

1
2
3
4
5
#!/usr/bin/env python3
a, b = 0, 1
while b < 100:
    print(b)
    a, b = b, a + b

By default print always prints in new line. We can change end by pass it into print function something like print(b, end=' '). This would not print \n <- newline character after every print statement instead it would print space.

break

break is used to break at loop. It could be because some conditions matched or whatever could be the case.

1
2
if term < 0.0001:
    break

This means if the value of term is less than 0.0001 then get out of the loop. If this statement is kept inside a loop.

Decorating prints

We can print space separator’s and character separators using print. We can multiply string or character with a number to print that n number of times. for example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
print("-" * 50)
--------------------------------------------------
>>> print("*" * 10)
**********
>>> print("#" * 20)
####################
>>> print("--" * 20)
----------------------------------------
>>> print("-" * 40)
----------------------------------------

Draw the pattern

One of the question which has haunt several to be under-graduate students(some graduates too) is drawing given pattern. I remember people using space and print statement to print exact same 5 line pattern then fighting with teacher to say When did you say we had to take input :D. Moving ahead.

So patterns in python becomes easy(even for those student) suppose we want below pattern:

1
2
3
4
5
*****
 ****
  ***
   **
    *

In python what we do is we take an input say draw 5 lines. we would print two things number of spaces and stars. Need to decrease number of stars and increase number of space.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/usr/bin/env python3
row = int(input("Enter the number of rows: "))
n = row
while n >= 0:
    x = "*" * n
		# This would store 5 star at start into variable x and keep decrease as -1 at bottom
    y = " " * (row - n)
		# This would store spaces as row - n initially value of both is same of there would 0 spaces and then they would keep increasing
    print(y + x)
		# This would print first space then start for that iteration
    n -= 1

Lists

Lists is similar to arrays in js.

 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
>>> a = [ 1, 342, 223, 'India', 'Fedora']
>>> a
[1, 342, 223, 'India', 'Fedora']
# nth element in list
>>> a[0]
1
>>> a[4]
'Fedora'
# -nth element in list, that is nth element from end of list
>>> a[-1]
'Fedora'
>>> a[-2]
'India'
# n:m would return from n to mth element to list  not including mth element.
# we can either side as blank. which would consider start and end of list
>>> a[0:-1]
[1, 342, 223, 'India']
>>> a[2:-2]
[223]
>>> a[:-2]
[1, 342, 223]
# Below we are using two :: which would work as [n:e:k], from nth to eth element jumping k elements. So if n is 0 and k is 2 then first element would be 0th then 2nd then 4th. and so on.
>>> a[0::2]
[1, 223, 'Fedora']
# to check whether value exists in list we can use 'in' operator
>>> 1 in a
True
>>> 2 in a
False
# len would give length in a
>>> len(a)
3

To check list is empty or not we use following code.

1
2
3
4
if list_name: # This means the list is not empty
    pass
else: # This means the list is empty
    pass

Have continue from for loop tomorrow :)