We use square brackets to index into strings and lists:
text = "this is a string"
for i in range(len(text)):
print(text[i])
We use parentheses when we define and call functions. Notice that print
is also a function.
def area(length, width):
return length*width
print("Area is:", area(4, 5))
A for
loop is used when we know exactly how many times we want to repeat a set of statements. It is known as a definite loop. The syntax is:
for <variable> in <sequence>:
<stmts>
The semantics is that the variable will be set to the first item in the sequence, then the statements are executed. Then the variable is set to the second item in the sequence, then the statements are executed. Repeat for all items in the sequence.
A while
loop is used when we aren't sure how many times we will need to repeat a set of statements. It is known as an indefinite loop. The syntax is:
while <condition>:
<stmts>
The semantics is that the condition will be evaluated, if it is True, then the statements will be executed. Repeat until the condition evaluates to False.
The syntax for calling a function is <functionName>(<arg1>, <arg2>, ...)
.
You've been calling functions inside functions all of the time, but maybe didn't realize it. For example, both input
and print
are functions that you've been calling from within main
.
Parameters are variables that are given in a function definition. Arguments are expressions within a function call that get evaluated, and then their values are copied into the parameters in order.
For example in the code snippet below, the parameters are first
and last
, and the arguments are "Valerie" and "Smith". When the greeting
function is called, these arguments are copied into the parameters in order.
def greeting(first, last):
print("Hello", first)
print("Or do you prefer a more formal greeting?")
print("Salutations", first, last)
greeting("Valerie", "Smith")
This would print:
Hello Valerie
Or do you prefer a more formal greeting?
Salutations Valerie Smith
This is called recursion and is a topic for later in the semester (week 11).
As our programs get larger (the rock-paper-scissors game is a good example of this), our main program will serve as a nice outline of the entire program. Our goal is to hide details away inside functions, so that the main program is clear and easy to understand. For the rock-paper-scissors game, we've helped you by telling you which functions you should write (getChoice, calculateWinner, and printScores). Later in the semester we will let you design your own functions.
The semantics of loops are the same, regardless of whether they are inside a function or not.
When you are trying to understand a program just follow the flow of execution, starting in main and then working through each statement one at a time. If a statement contains a function call, then evaluate the arguments and associate those values with the function's parameters, and jump to the function's definition working your way through those statements.
When we define functions, we can choose any valid variable name that we'd like to represent the data that needs to be processed. We encourage you to always choose names that are meaningful. So the name text is an arbitrary choice. We could have instead called it string or phrase. The goal is to choose a variable name that reflects something about what the variable is storing.
def hasLetter(letter, text):
"""
Returns True if the letter occurs within the text.
Otherwise returns False.
"""
for i in range(len(text)):
if text[i] == letter:
return True
return False
We can call this function as shown below. The variable char
will get evaluated to "z" and passed into the parameter letter
, and the variable word
will get evaluated to "pizza" and get passed into the parameter text
. Then the hasLetter
function will be executed with those values associated with the parameters.
char = "z"
word = "pizza"
if hasLetter(char, word):
print(word, "has a", char)
If your program does NOT output: "Your phrase contains all vowels!" even though your string does contain at least one of every vowel, then make sure that the return False
statement is outside of the for loop (as shown above).