Professional Documents
Culture Documents
Adopted from Stanford Uni’s CS106ap course slides by Kylie Jue and Sonja Johnson-Yu
How can we store and organize
Today’s data in our code?
questions
1. Review
4. What’s next?
Review
Console programs
Definition
Console program
A program that solicits input from a user via
an interactive terminal (console) and does
something interesting with that input
Console program summary
● Length
○ The length of a string is the number of characters it contains
○ We can use the Python function len() to evaluate the length of a
string
len(‘banana’) → 6
len(‘’) → 0
len(‘CS106AP rocks my socks’) → 22
Slides courtesy of Sam Redmond, CS41
Slides courtesy of Sam Redmond, CS41
What are the
correct indices?
Slides courtesy of Sam Redmond, CS41
Slides courtesy of Sam Redmond, CS41
Slides courtesy of Sam Redmond, CS41
String functions
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
● str.upper(), str.lower()
String functions
● str.isupper(), str.islower()
Return True or
● str.isalpha(), str.isdigit() False
● str.upper(), str.lower()
String functions
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
● str.isupper(), str.islower()
● str.isalpha(), str.isdigit()
int(‘123’) == 123
float(‘24.7’) == 24.7
str(12345) == ‘12345’
str(20.19) == ‘20.19’
Doctests
Doctests
● PyCharm supports doctests by allowing you to easily run them in the editor
○ Put doctests in function header comments using `>>>`
def add(a, b):
“““
>>> add(2, 4)
6
”””
...
Doctests
● Python has a great testing framework called doctests
● PyCharm supports doctests by allowing you to easily run them in the editor
○ Put doctests in function header comments using `>>>`
def add(a, b):
“““
Call the function
>>> add(2, 4)
6
and specify any
””” arguments if
...
needed.
Doctests
● Python has a great testing framework called doctests
● PyCharm supports doctests by allowing you to easily run them in the editor
○ Put doctests in function header comments using `>>>`
def add(a, b):
“““
>>> add(2, 4)
6 Put the expected
”””
...
output directly
after the test.
Testing strategies
● Write tests that cover a wide variety of use cases for your function!
● Consider:
○ Basic use cases
○ Edge cases
Definition
edge case
Uses of your function/program that
represent extreme situations
EliminationNation.py
[demo]
Takeaways
List
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]
List
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]
List
A data type for storing
Use [ ] to values in a linear collection.
write a list
in code!
What is a list? Definition
[1, 2, 3, 4, 5]
List
A data type for storing
Lists contain values in a linear collection.
elements!
(separate with
commas)
What is a list? Definition
[1, 2, 3, 4, 5]
Lists can
have 1
element!
What is a list? Definition
[1, 2, 3, 4, 5]
[]
What is a list? Definition
[1, 2, 3, 4, 5]
[]
compare to the empty string
(‘’)!
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
‘a’
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
‘a’
>>> letters[3]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
‘a’
>>> letters[3]
‘d’
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
>>> len(letters)
4
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> len(letters)
>>> letters[:2]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[:2]
[‘a’, ‘b’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[:2]
[‘a’, ‘b’]
>>> letters[1:]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[:2]
[‘a’, ‘b’]
>>> letters[1:]
>>> letters[:2]
>>> print(fruits)
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
>>> print(fruits)
>>> numbers
[0, 0, 0, 0, 0]
>>> numbers = [1, 2, 3] * 3
>>> numbers
[1, 2, 3, 1, 2, 3, 1, 2, 3]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
>>> lst += 8
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
>>> lst += 8
you can only use += for
concatenating other lists!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst.append(6)
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst.append(6)
>>> lst.append(6)
>>> lst
append adds a single
element to the end of a
list!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst.append(6)
>>> lst
append adds a single
[1, 2, 3, 4, 5, 6]
element to the end of a
list!
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last
element in a list and
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last
5
element in a list and
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last
5
element in a list and
>>> lst
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last
5
element in a list and
>>> lst
returns it
[1, 2, 3, 4]
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
banana
mango
How can I loop over a list?
>>> find_min([2.3, 7.1, 10.6])
2.3
Think/Pair/Share:
# syntax reminder:
Write a function
for elem in lst: find_min()that returns
the minimum float in a
# do something list.
Think/Pair/Share:
>>> s
Making a list from a string
>>> s = ‘I am comprised of words’
>>> s
‘I am comprised of words’
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’
[0, 1, 2, 3, 4]
>>> numbers = list (range(1, 10, 2))
[0, 3, 4, 7, 9]
Think/Pair/Share:
Write words_starting_with()
Inputs:
sentence (string)
char (string)
Returns:
list of words starting with character
Making a list from a string
>>> lst[::-1]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::-1]
>>> lst[::-1]
>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
>>> lst
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
>>> lst
>>> lst[::2]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::2]
Think/Pair/Share:
What will this return?
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::2]
>>> lst[::2]
(start_index, end_index,
[‘a’, ‘c’, ‘e’] step)
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::2]
(start_index, end_index,
[‘a’, ‘c’, ‘e’] step)