From xkcd.
def function_1(...,...):...def function_2(...,...):......def function_k(...,...):...#Comments and so on.statement_1statement_2...statement_n
def function_1(...,...):...def function_2(...,...):......def function_k(...,...):...#Comments and so on.statement_1statement_2...statement_n
The Python interpreter executes statements from top to bottom.
The function definitions and comments (lines starting with the # character) are parsed but not executed.
def function_1(...,...):...def function_2(...,...):......def function_k(...,...):...#Comments and so on.statement_1statement_2...statement_n
The Python interpreter executes statements from top to bottom.
The function definitions and comments (lines starting with the # character) are parsed but not executed.
Actual computation starts from statement_1
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Conditional Execution
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Conditional Execution
Repeated Execution or Loops
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Conditional Execution
Repeated Execution or Loops
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Conditional Execution
Repeated Execution or Loops
Function definitions
Need to vary computation sequence as values evolve.
Control flow determines the order in which statements are executed.
Conditional Execution
Repeated Execution or Loops
Function definitions
These blocks don't even execute until you call them.
Keeps your codebase organized 😎
We use conditional executions all the time.
We use conditional executions all the time.
if (sleepy == True): snooze_alarm()else: wakeup()
We use conditional executions all the time.
if (sleepy == True): snooze_alarm()else: wakeup()
We use conditional executions all the time.
if (sleepy == True): snooze_alarm()else: wakeup()
The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.
The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!
We use conditional executions all the time.
if (sleepy == True): snooze_alarm()else: wakeup()
The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.
The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!
The else block is not mandatory.
We use conditional executions all the time.
if (sleepy == True): snooze_alarm()else: wakeup()
The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.
The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!
The else block is not mandatory.
You can handle multiple scenario cases by using if-elif-else statements.
We use conditional executions all the time.
if (sleepy == True): snooze_alarm()else: wakeup()
The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.
The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!
The else block is not mandatory.
You can handle multiple scenario cases by using if-elif-else statements.
if (sleepiness == 10): snooze()elif (sleepiness == 5): coffee()else: wakeup()
n = 10for i in range(n): print(i, end=" ")print() # Prints a newline
n = 10for i in range(n): print(i, end=" ")print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
n = 10for i in range(n): print(i, end=" ")print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
n = 10for i in range(n): print(i, end=" ")print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
x = 5y = 10for i in range(x,y): print(i, end=" ")print() #Prints a newline
n = 10for i in range(n): print(i, end=" ")print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
x = 5y = 10for i in range(x,y): print(i, end=" ")print() #Prints a newline
5 6 7 8 9
List all the factors of a number n.
List all the factors of a number n.
n = 50for i in range(1,n+1): if n%i == 0: print(i)
List all the factors of a number n.
n = 50for i in range(1,n+1): if n%i == 0: print(i)
Cutting down the number of iterations:
List all the factors of a number n.
n = 50for i in range(1,n+1): if n%i == 0: print(i)
Cutting down the number of iterations:
n = 50from math import sqrtfor i in range(1,int(sqrt(n))+1): if n%i == 0: print(i) print(int(n/i))
Often we don't know the number of repetitions in advance.
Often we don't know the number of repetitions in advance.
sleepy = 10while(sleepy): snooze() sleepy = sleepy - 1
Often we don't know the number of repetitions in advance.
sleepy = 10while(sleepy): snooze() sleepy = sleepy - 1
Executes the body if condition evaluates to True.
Often we don't know the number of repetitions in advance.
sleepy = 10while(sleepy): snooze() sleepy = sleepy - 1
Executes the body if condition evaluates to True.
After each iteration, check if condition is True again.
Often we don't know the number of repetitions in advance.
sleepy = 10while(sleepy): snooze() sleepy = sleepy - 1
Executes the body if condition evaluates to True.
After each iteration, check if condition is True again.
☠️ Note that this execution will not terminate if the condition never evaluates to False!
From xkcd.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |