1 of 13

CSE 163

Introduction to Python II

Strings and Lists

Suh Young Choi

🎶 Listening to: Across the Spider-Verse Soundtrack�💬 Before Class: Read any good books lately?

2 of 13

This Time

  • Python Crash Course - Day 2
    • Strings and String Functions
    • Slicing
    • Lists
    • Documentation
    • None

Last Time

  • Python Crash Course - Day 1
    • Hello world
    • Variables
    • Expressions
    • Types + Casting
    • Loops
    • Conditionals
    • Functions (parameters + returns)

2

3 of 13

Quick Announcements!

  • Still waiting on course website ☹
    • CSE Support is helping out ☺

  • Office hours have begun!—schedule will be posted on Ed/course website.

  • Reminders:
    • Checkpoints are shorter coding and conceptual problems
    • Learning Reflections are guided study notes/reflections on the weekly material
    • Take-Home assessments are longer programming exercises

3

4 of 13

Recap: Lesson

Strings

  • Text data that has indices for characters
  • Has functions to transform strings (returns new ones!)
  • Slicing semantics

Lists

  • A lot like strings (indexed-sequence), but can hold any value!
  • For Java people—a lot like an array

Documentation

  • Doc-string (“”” documentation “””)
  • Type annotations

None

4

5 of 13

String Methods vs. len

Call functions on strings using syntax:

This is not true for len (or other built-in Python functions):

This asymmetry is annoying, but you have to get used to it!

5

s.function(params)

len(s)

6 of 13

Strings

vs.

Lists

6

s = 'hello world'

# Length

len(s) # 11

# Indexing

s[1] # 'e'

s[len(s) - 1] # 'd'

# Looping

for i in range(len(s)):

print(s[i])

for c in s:

print(c)

l = ['dog', 'says', 'woof']

# Length

len(l) # 3

# Indexing

l[1] # 'says'

l[len(l) - 1] # 'woof'

# Looping

for i in range(len(l)):

print(l[i])

for word in l:

print(word)

7 of 13

Function comments

Function comments are the best way for others to know what your code does!

  • Remember grammar?
  • Focus on what your function does, rather than the how

7

def sum_what(nums):

“””

Takes in a list of

numbers and returns the

sum of those numbers.

“””

total = 0

for i in nums:

total += i

return total

def sum_how(nums):

“””

Takes in a list of numbers,

then calculates the sum of

all numbers using a for loop

to iterate through all numbers.

“””

total = 0

for i in nums:

total += i

return total

8 of 13

Anatomy of a Function

8

function �header

parameters

function �body

function call

return statement

9 of 13

Anatomy of a Function (updated!)

function �header

9

parameters

function �body

return statement

function call

type annotations

doc-string comment

10 of 13

None

  • None is a special value in Python that represents the absence of a value
  • You don’t have to know a lot about None except:
    • It can cause your program to crash if you ask a None value to do something
    • You might have cases where you return None
    • Check for None using the is keyword, instead of ==

10

def increment(x):

if x < 0:

return None

else:

return x + 1

if increment(-1) is None:

print('Failed')

11 of 13

What are you still curious about?

11

12 of 13

Group Work:

Best Practices

When you first working with this group:

  • Introduce yourself!
  • If possible, angle one of your screens so that everyone can discuss together

Tips:

  • Starts with making sure everyone agrees to work on the same problem
  • Make sure everyone gets a chance to contribute!
  • Ask if everyone agrees and periodically ask each other questions!
  • Call TAs over for help if you need any!

12

13 of 13

Next Time

Quiz Section

  • Course Infrastructure
  • Testing & Debugging Process
  • Style

Lesson

  • More list functions
  • File I/O

Before Next Time

  • Go to section tomorrow!
  • Complete the Lesson 3 reading before Friday!

13