input
Functionif
Statementsif
-else
Statementsif
-else
Statementif
Statementsif
-elif
-else
Statementsfor
Loopsrange
FunctionI have posted homework 7 here.
It is not due this coming Sunday.
The mid-term exam will be given on Tuesday, March 23rd.
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 Ungraded Class Quizzes.
The other 40% will come from four questions that ask you to write a short segment of code.
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.
The Mid-term is a closed book exam.
To prevent cheating, certain rules will be enforced during the exam.
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"
input
Functioninput
function is used to ask the user for a valueinput
in a script it stops runninginput
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 string int
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
>>> 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
bool
True
False
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 expression
if BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
if
-else
Statementsif
statement will only run the code block o if the boolean expression is trueif
-else
statement
if BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
else:
STATEMENT
STATEMENT
...
if
clause are executedelse
clause are executedif
-else
Statementif
statement and and if
-else
statement must be indented properlyif
-else
statement both the if
and else
clauses must be alignedif
and else
are not exactly aligned, you will get an error
>>> if 4 > 3:
... print("4 is ")
... print("greater than 3")
File "<stdin>", line 3
print("greater than 3")
^
if
Statementsif
statement inside another if
statement if
statement
$ cat it443.py # determines whether a student can take IT 443 # demonstrates nested if statements print("This program will tell you if you can take IT 443") answer = input("Have you taken IT 244 (y/n)? ") if answer == "y": answer = input("Have you taken IT 341 (y/n)? ") if answer == "y": print("You can take IT 443") else: print("You must take IT 341 before you can take IT 443") else: print("You must take IT 244 before you can take IT 443") $ python3 it443.py This program will tell you if you can take IT 443 Have you taken IT 244 (y/n)? y Have you taken IT 341 (y/n)? y You can take IT 443
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 optionalelif
is short for "else if"and
operator returns true if both its operands are true
>>> 5 > 4 and 4 > 2 True
and
returns false
>>> 5 > 4 and 4 < 2 False
>>> 5 < 4 and 4 < 2 False
or
operator returns true if either of its operands are true
>>> 5 > 4 or 4 < 2 True
>>> 5 < 4 or 4 < 2 False
not
operator takes only one operand and reverses its value>>> not 5 > 4 False
>>> 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 |
and or not | Logical per Operators |
while
loop has the following format
while BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
while
loop is evaluated before entering the loopwhile
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 |
for
loops are the most commonfor
loop that creates a times table
$ cat times_table.py # This script creates a times table using nested for loops for row in range(1, 11): for column in range(1, 11): entry = row * column print(entry, end="\t") print() $ python3 times_table.py 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
import
statement to bring these functions into any Python scriptstr
, int
and float
return a valueprint
does not return a value
def FUNCTION_NAME([PARAMETER, ...]):
STATEMENT
STATEMENT
...
def