CS 105: TOPIC 6 – LOOPS TOPIC 7 – MORE EXCEL
Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/
JULY 5, 2020
TOPIC 7 MORE EXCEL Max Fowler (Computer Science) - - PowerPoint PPT Presentation
CS 105: TOPIC 6 LOOPS TOPIC 7 MORE EXCEL Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ JULY 5, 2020 Week 4 Video Series Topics for loops (definite loops) and range while loops (indefinite
Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/
JULY 5, 2020
for loops (definite loops) and range while loops (indefinite loops) break and continue in loops Excel – SUM, COUNTIF/IFS, Excel - INDEX/MATCH, *LOOKUP
Loops are the third basic coding structure
a) Execute body b) Loop back to step 1
For loops do some action for each element of a
Prints… Iteration 1: Holden is an awesome student! Iteration 2: Melissa is an awesome student! Iteration 3: Quinn is an awesome student!
For when you want indices as well as elements of a collection
Most languages let a for loop run over a sequence of numbers – from i
= 0 to 10, for example
Range let's use generate a finite sequence of numbers
range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(3, 8) # [3, 4, 5, 6, 7] range(-3, 11, 3) # [-3, 0, 3, 6, 9]
How many lines will be printed?
To a degree, easier "Indefinite" loops – indefinite as in we don't
Uses sentinel values – stopping symbols
How many lines are printed?
break ends a loop continue skips an iteration, but continues the loop!
How many items are printed?
mixed_list = [ 'hi', 3, math.pi, 'there', ['CS', 105]] for item in mixed_list: if type(item) != str: continue print(item)
We've seen some functions already, either in the
Functions generate values for assignments Example functions
SUM, COUNTIF, COUNTIFS
SUM(range) Sum the values in a range – pretty
COUNT values with a criteria Criteria can be
A single value – 7 or "Illinois" A comparison – ">7" or ">" & B2 A string with wildcards – "CS *"
COUNTIFS(range1, criteria1, range2,
Ranges much have equal row/column count Counts each time the values of the ranges
Don't want to bloat compass' gradebook
1-dimensional lookup:
=INDEX(cellrange, index)
cellrange is 1-dimensional range index is 1-based, not 0-based like in Python
=INDEX(B3:B11, 4) ---> B6
2-dimensional lookup:
=INDEX(cellrange, index1, index2)
cellrange is 2-dimensional range index1 specifies the row, index2 the column
=INDEX(B3:D11, 4, 2) ---> C6
=MATCH(value, cell-range, match-type)
value is the value we're looking for cell-range is a row or a column (not 2D) match-type is:
0 = Exact match 1 = Approximate (largest less than or equal, values must be sorted in increasing order) -1 = Approximate (smallest value greater than or equal, values must be sorted in
descending order)
returns 1-based index into the cell range for a cell containing the value Return #N/A if no match
=INDEX(B1:B20, MATCH("Max", A1:A20, 0))
Search A1:A20 for a cell holding 'Max' Read the corresponding element of the B column
Handles a common case of INDEX + MATCH
Less flexible, but only 1 function
=VLOOKUP (value, table, col_index, [range_lookup])
value - value to look for in the first column of table. table - table from which to retrieve a value. col_index - The column in the table from which to retrieve a value. range_lookup - [optional] TRUE = approximate match (default). FALSE =
exact match.
Value searched for must be to the left of value to return HLOOKUP does for rows, what VLOOKUP does for cols
Which cell does =INDEX(C10:F20, 3, 2) read from?
A) D12 B) E12 C) E13 D) F12