for whatever in sequence:
do things here
indent the statements to repeat
Note:
Let's see some examples. Can you see how they work when you run them in Python?
while condition:
do things here
indent the statements to repeat
Note:
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.
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
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.
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.
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'
501 E. High Street
Oxford, OH 45056
1601 University Blvd.
Hamilton, OH 45011
4200 N. University Blvd.
Middletown, OH 45042
7847 VOA Park Dr.
(Corner of VOA Park Dr. and Cox Rd.)
West Chester, OH 45069
Chateau de Differdange
1, Impasse du Chateau, L-4524 Differdange
Grand Duchy of Luxembourg
217-222 MacMillan Hall
501 E. Spring St.
Oxford, OH 45056, USA