If statements
This or that?
If statements in Python are like decision trees that help the computer make a decision based on certain conditions. You can use if, elif, and else statements to add conditions and provide default actions. If statements will only run the code under them if the boolean it is checking returns true. Else will only run if the if statement(s) above it have returned false. Then finally, Elif will only check if the selection statement above it has returned false.
if (condition):
checks and only runs code if condition is true
elif (condition):
only checks if the if statement above returned false and didn't run
else:
Runs if the if and/or elif statements above it have returned false
For loops
Looping
A for loop is a way to repeat a set of instructions a certain number of times. It's like telling a robot to do the same thing over and over again until you tell to stop. In, you use the "for" keyword to start a loop, and then you specify how many times you want the loop to run. Each time the loop runs it does the same set of instructions until it reaches the end.
for (i) in range:
runs the code i times
​
While Loops
Looping
A while loop is like game where you keep doing something until you win. In Python, you write a while loop to keep doing something until a certain condition is met. For example, you might want to keep asking someone for their name until they give you a valid name. The loop will keep running until the person gives you a valid name, and then it will stop.
While (condition):
Will keep looping until the condition returns false
​
The condition is normally set to false after the intended function and actions have been made by the program.
​
Nesting
Looping
Nesting is when you put one statement inside another statement. It's like putting a toy inside a toy box. You can have a big toy box, and inside that toy box, you can have smaller toy boxes. In Python, you can have a big statement, and inside that statement, you can have smaller statements. It's a way to organize your code and make it easier to read and understand. Statements that are nested won't move on to beyond until the nested statements have completed
If (condition1):
while (condition2):
In this case, the while statement with condition2 will not run unless condition1 is true.
​