Learning python through pym 13-08-2019
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
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
while loop we execute until condition is meet.
generating fibonacci using while
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.
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
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:
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.
|
|
Lists
Lists is similar to arrays in js.
|
|
To check list is empty or not we use following code.
Have continue from for loop tomorrow :)