-
week 1
- variables
-
built-in functions
- dir()
- help()
-
function definition
- def function_name(parameters):
body
-
return expression
- What is passed back to the caller?
-That memory address is passed back to the caller.
What is displayed?
-Nothing!
-
week 2
-
type STR
immutable
cannot be modified
- single/double quote. triple-quote multi-line strings
- escape sequence - '\'
-
String operators
- +, *
-
print
- What is passed back to the caller?
-Nothing!
What is displayed?
-The values at those memory address(es) are displayed on the screen.
-
input method
- var = input("value returned from this function is always a string")
- docstring
Built-in function help() displays the docstring from a function definition
-
Function Design Recipe
-
1. Examples
What should your function do?
Type a couple of example calls.
Pick a name (often a verb or verb phrase): What is a short answer to "What does your function do"?
2. Type Contract
What are the parameter types?
What type of value is returned?
3. Header
Pick meaningful parameter names.
4. Description
Mention every parameter in your description.
Describe the return value.
5. Body
Write the body of your function.
6. Test
Run the examples.
- def convert_to_celsius(fahrenheit):
''' (number) -> number
Return the number of Celsius degrees equivalent to fahrenheit degrees.
>>> convert_to_ccelsius(32)
0
>>> convert_to_celsius(212)
100
'''
return (fahrenheit - 32) * 5 / 9
- Function Reuse
- Function calls
-
week3
- Functions, Variables, and the Call Stack, frames, namespace
-
Type: bool
True or False
-
Comparison operators
- <, >, ==, >=, <=, !=
-
Logical Operators
-
AND
- True True -> True
True False -> False
False True -> False
False False -> False
-
OR
- True True -> True
True False -> True
False True -> True
False False -> False
-
NOT
- True False
False True
-
type conversion
- str(int/float)
- int(str, float)
If called with a string that can't be converted, a ValueError happens
- float(int/str)
If called with a string that can't be converted, a ValueError happens
-
import module
- import module
- from module import */name
-
IF statement
- if expression1:
body1
[elif expression2: 0 or more clauses
body2]
[else: 0 or 1 clause
bodyN]
- Check code and reduce IF statements if possible
-
Remember if-elif if-if usage options
- if-elif is single block. runs only if is false. if IF is true elif is not ran
- in IF-IF case every if is separate statement and runing in sequence
- Nested IFs
- None - value, is NoneType
-
week 4
-
More STR operators
- Comparison
-
if substring is IN another one
if contains
- >>> 'c' in 'aeiou'
False
>>> 'cad' in 'abracadabra'
True
>>> 'zoo' in 'ooze'
False
- len(str)
-
STR indexing and slicing
-
Indexing
- str[0], str[1],... str[-2], str[-1]
-
slicing
- str[0:1], str[0,len(str)],str[0:], str[:-1]
indexes higher then actual indexes return whole word
not crossing indexes returning empty string
- Modifying strings
TypeError: 'str' object does not support item assignment
-
String Methods
- lower(), upper(),endswith(), isnumerical(), count()
-
FOR loop
-
for variable in str:
body
- Accumulator pattern
- outside counter
- runs while true and definied number of loops
-
week 5
-
WHILE loop
- while expression:
statements
- runs infinite number of loops untill expression is FALSE
- Lazy Evaluation
Condition stops as soon as first element is true or false not checking further conditions
- comments #
-
type LIST
mutable
can be modified
- [expr1, expr2, ..., exprN]
-
list operations
-
indexing
- grades = [80, 90, 70]
>>> grades[0]
80
>>> grades[1]
90
>>> grades[2]
70
-
slicing
- >>> grades[0:2]
[80, 90]
-
IN operator
- >>> 90 in grades
True
>>> 60 in grades
False
-
looping over list elements with FOR loop
- >>> for grade in grades:
print(grade)
80
90
70
-
built-in funcs
- len(list): return the length of list.
min(list): return the smallest element in list.
max(list): return the largest element in list.
sum(list): return the sum of elements of list (where list items must be numeric).
-
list methods
- help(list)
- Lists elements may be of any type
-
Mutability
-
mutable: they can be modified
- list, dict, set
-
immutable: they cannot be modified
- str, int, float and bool, tuple, frozen set
-
Aliasing
- A side affect. Hard to find bugs
-
RANGE
- range([start,] stop[, step]):
return a virtual sequence of numbers from start to stop by step
-
week 6
-
FOR loops over indices
- # Iterating over the indices of a list
for i in range(len(lst)):
print(lst[i])
- Parallel Lists and Strings
Corresponding Elements
-
Nested Lists
- Lists can contain items of any type, including other lists
-
Nested Loops
- The bodies of loops can contain any statements, including other loops.
-
Reading Files
-
open(filename, mode)
- 'r' - to open for reading
'w' - to open for writing
'a' - open for appending to what is already in the file
-
File read methods
-
readline()
- When you want to process only part of a file.
-
for line in file
- process every line in the file one at a time
-
readlines()
- return all lines in a file in a list
-
read()
- read the whole file at once and use it as a single string
- flanders_file.close()
-
Writing Files
- file.write(str)
-
File dialogs
- import tkinter.filedialog
- from_filename = tkinter.filedialog.askopenfilename()
- to_filename = tkinter.filedialog.asksaveasfilename()
- from_file.close()
-
week 7
-
type DICT
- {key1: value1, key2: value2, ..., keyN: valueN}
- >>> dict['key']
corresponding value
- IN operator to check KEY existance
- len(dict) - number of key:value pairs
- because dict is mutable elements can be changed
- del dict[(key)] #remove key:value pair
- for looping over keys
- key:value are unordered in dict
- empty dict = {}
- key MUST be immutable
-
tuples
- indexing
- slicing
- for looping
- fewer methods then lists
- len(tuple)