# Loops, Conditional Statements and Function

• for loop - (0:20)
• while loop - (2:12)

## For Loop Statement

### Basic Structure:

for whatever in sequence:

do things here

indent the statements to repeat

Note:

1. whatever is a dummy variable that you can replace with other variables.
2. Be sure to indent the statements to repeat in the loop.

Let's see some examples. Can you see how they work when you run them in Python?

## While Loop Statement

### Basic Structure:

while condition:

do things here

indent the statements to repeat

Note:

1. We need to have at least one condition to implement the loop and make sure we don't get an infinite (or endless) loop
2. Be sure to indent the statements to repeat in the loop

Let's see examples.

``````#1 print all even numbers from 1 to 20
number = 2  # initialize the number we want to print
while number <= 20:
print number
number = number + 2

#2 find 1+2+???+10
total = 0  # initialize total
value = 1  # we start the calculation from 1
while value <= 10:
total = total + value
value = value + 1
print total  # check answer ``````

Note: We use the same examples to demonstrate how to use for loops and while loops. However, for some situations, using while loops may be more appropriate than using for loops or vice versa. It is better to write pseudocode (a simple outline of what you plan to do) first and get a clear idea of what you need and how to accomplish that task.

• In general, for loops are used when you know exactly how many times you plan to run a loop; a while loop is used when the number of iterations is uncertain.

## Conditional Statement

• if...elif...else.. - (0:07)
• Example - (0:44)
• Another example with list - (1:45)

### Basic Structure:

if condition:

do things here

indent the statements to repeat

Note: The if statement is like a single iteration of a loop; it is used to trigger a reaction to certain conditions, or respond to an event or another object.

Here are two examples.

``````#1 input an integer and check if the number is 0 or 1
a = int(input("Please input an integer: "))  # you need to execute this command first or save all commands in a file and execute the file
if (a == 0 or a == 1):
print "This number is "+str(a)+"."  # if the number is 0 or 1, print it
else:
print "The number is neither 0 nor 1."

#2 create two lists to store all even and odd numbers from 1 to 20 individually
even = []  # create a list for even numbers
odd = []  # create a list for odd numbers
for k in range(1, 21):
if k%2 == 0:  # % is the mod operator; this line checks if the remainder of k/2 equals 0
even.append(k)  # append the number to the list even
else:  # don't forget to indent the statement
odd.append(k)
print even  # check the list
print odd  # check the list ``````

## Creating a function

• Function - (0:07)
• Parameters - (0:39)
• Creating a function example - (0:50)
• Calling a function example - (1:25)

A function is a block of code that performs one desired action. Common examples are the “print()” function that allows output code to be shown in the console “math.sqrt()”, which returns the square root of a number. Creating functions allows code to be reused and repurposed.

Every function is created with the word “def”, which stands for define, followed by the name for the function, a set of parentheses, and a colon. The actual code is then written on the lines below.

### Basic Structure:

def functionName(parameter1, parameter2, ...):

do things here

return(output)

The parentheses contain what are called parameters. These are the variables that are input into the function that contribute to the final output. Parameters are not always required; depending on the function, they can include anything from integers and strings to other functions. Let’s create a simple area calculating function to get used to the syntax.

``````def getArea(width, height):
return(width * height)``````

Note: getArea is the name of the function that asks the user to input two values: the width and height of some object. The function then returns the width and height multiplied by each other.

## Calling a Function

When you utilize a previously created function it is referred to as “calling” that function. Let’s call the getArea() function that we just created to calculate the area of an object. We start by typing the function name followed by parentheses and then entering the required data in the parentheses in the order specified at the creation of the function.

``print 'Area = ', getArea(2,4), 'feet'``

Note: We wrap the function in a print statement so the user will know what the number printed to the console means.

Similarly, we could define variables which have integer values and return the same result by plugging those in as the function parameters.

``````rectangle_width = 2
rectangle_height = 4

print 'Area = ', getArea(rectangle_width, rectangle_height), 'feet'``````