input Functionif Statementsif-else Statementsif-else Statementif-elif-else Statementsfor Loopsrange FunctionI have posted homework 7 here.
It is NOT due this coming Sunday.
Instead it is due Sunday, March 30th.
This is to give you time to study for the midterm.
And to give me time to score it.
March 25th.
That is the first Tuesday after the Spring Break
The exam will be given in this room.
It will consist of questions like those on the quizzes along with questions asking you to write short segments of Python code.
60% of the points on this exam will consist of questions from the Weekly Graded Quizzes.
There is a link to the answers to the graded quizze on the class web page.
There will be 15 of these questions, worth 4 points each.
The other 40% of points will come from four questions that ask you to write a short segment of code.
There will be 4 of these questions worth 10 points each.
To study for the code questions you should be able to
if/elif/else
statement
while loopfor loop using rangeA good way to study for the code questions is to review the Class Exercises and homework solutions.
Today's class will be a review session.
You will only be responsible for the material in the Class Notes for today's class on the exam.
If for some reason you cannot take the exam on the date mentioned above you must contact me to make alternate arrangements.
The Midterm is given on paper.
I scan each exam paper and upload the scans to Gradescope.
I score the exam on Gradescope.
You will get an email from Gradescope with your score when I am done.
The Midterm is a closed book exam.
You are not allowed to use any resource other than what is in your head, while taking the exam.
Cheating on the exam will result in a score of 0 and will be reported to the Administration.
Remember your Oath of Honesty.
To prevent cheating, certain rules will be enforced during the exam.
Remember, the Midterm and Final determine 50% of your grade.
Let's look at the answers to Quiz 5.
Today is a review session.
There will no Class Exercise or Class Quiz today.
Are there any questions before I begin?
5 6.47 True
print("Hello world!")
>>> team = "Red Sox" >>> name = "Glenn Hoffman"
>>> print("Hello") # but ignore this text
Hello
VARIABLE_NAME = EXPRESSION
name = "Glenn Hoffman"
int)float)input Functioninput function gets a value from the userinput in a script it
stops running ...
input waits for the user to type some textinput returns the string to
the
function call
>>> name = input("Name: ")
Name: Glenn
>>> name
'Glenn'
input is always a stringint function
>>> number = int(number) >>> type(number) <class 'int'>
| Character | Operation | Description |
|---|---|---|
| + | Addition | Adds two numbers |
| − | Subtraction | Subtracts one number from another |
| * | Multiplication | Multiplies one number by another |
| / | Division | Divides one number by another and gives the result as a decimal number |
| // | Integer division | Divides one number by another and gives the result as an integer |
| % | Remainder | Divides one number by another and gives the remainder |
| ** | Exponent | Raises a number to a power |
>>> 4 / 2 2.0 >>> 4 / 5 0.8
floatint)
>>> 10 // 3 3
>>> 3 ** 2 9 >>> 3 ** 3 27 >>> 3 ** 4 81
>>> 17 // 5 3
>>> 17 % 5 2
| ** | Exponentiation |
| * / // % | Multiplication, division and remainder |
| + - | Addition and subtraction |
2 + 3 * 5
>>> 2 + 3 * 5 17 >>> (2 + 3) * 5 25
| Escape Character | Effect |
|---|---|
| \n | Causes output to be advanced to the next line |
| \t | Causes output to skip over to the next horizontal tab position |
| \' | Causes a single quote mark to be printed |
| \" | Causes a double quote mark to be printed |
| \\ | Causes a backslash character to be printed |
>>> team = "Red Sox" >>> cheer = "Let's go " + team >>> print(cheer) Let's go Red Sox
True or False is called a
boolean
boolTrue and
False
TrueFalseTrue or False depending on their
relationship
| Operator | Meaning |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | Equal to |
| != | Not equal to |
| ** | Exponentiation |
| * / // % | Multiplication, division and remainder |
| + - | Addition and subtraction |
| > < >= <= == != | Relational Operators |
if Statementsif statement evaluates a boolean expressionTrue ...
if BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
Trueif-else Statementsif statement will only run the statements in the
code block ...
TrueTrue ...True ...
False ...if-else statement
if BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
else:
STATEMENT
STATEMENT
...
Trueif clause are
executed
else clause
are executed
if-else Statementif statement and and if-else
statement must be indented properly
if-else statement has two clauses
if clauseelse clauseif and else must line up vertically
>>> if 4 > 3:
... print("4 is ")
... print("greater than 3")
File "<stdin>", line 3
print("greater than 3")
^
if-elif-else Statementsif-elif-else statement lets you
test for many conditions
if BOOLEAN_EXPRESSION_1:
STATEMENT
...
elif BOOLEAN_EXPRESSION_2:
STATEMENT
...
elif BOOLEAN_EXPRESSION_3
STATEMENT
...
...
[else:
STATEMENT
...]
else clause means
that it is optional
elif is short for "else if"and operator returns True if both its
operands are True
>>> 5 > 4 and 4 > 2 True
False, and returns
False
>>> 5 > 4 and 4 < 2 False
False if both its operands are False
>>> 5 < 4 and 4 < 2 False
or operator returns True if either of its
operands are True
>>> 5 > 4 or 4 < 2 True
False if both of its operands are
False
>>> 5 < 4 or 4 < 2 False
not operator takes only one operand ...True becomes False
>>> not 5 > 4 False
False becomes True
>>> not 5 < 4 True
and
| p | q | p and q |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
| p | q | p or q |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
not
| p | not p |
|---|---|
True |
False |
False |
True |
| ** | Exponentiation |
| * / // % | Multiplication, division and remainder |
| + - | Addition and subtraction |
| > < >= <= == != | Relational Operators |
| not | Logical NOT |
| and | Logical AND |
| or | Logical OR |
True
while loop has the following format
while BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
True
while loop does not become
False
for Loopsfor loop has the following format
for VARIABLE_NAME in LIST:
STATEMENT
STATEMENT
...
range Functionrange function is used to create a list of integers in a
for loop
>>> for number in range(5): ... print(number) ... 0 1 2 3 4
>>> for number in range(1, 5): ... print(number) ... 1 2 3 4
>>> for number in range(2, 11, 2): ... print(number) ... 2 4 6 8 10
$ cat average.py
# this program asks the user how many numbers
# they have to enter, then performs a running
# total and computes the average
entries = int(input("How many entries? "))
total = 0
for entry_no in range(entries):
number = int(input("number: "))
total += number
average = total / entries
print("Average", average)
$ python3 average.py
How many entries? 10
number: 10
number: 9
number: 8
number: 7
number: 6
number: 5
number: 4
number: 3
number: 2
number: 1
Average 5.5
number = number + 5
number += 5
| Operator | Example | Equivalent To |
|---|---|---|
| += | numb += 5 | numb = num + 5 |
| -= | num -= 5 | num = num - 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| //= | x //= 5 | x = x // 5 |
| %= | x %= 5 | x = x % 5 |
| **= | x **= 5 | x = x ** 5 |
import statement to bring these functions
into any Python script
str, int and
float return a value
print does not return a value
def FUNCTION_NAME([PARAMETER, ...]):
STATEMENT
STATEMENT
...
defaverage = round(total/entries)