1 of 9

CSE 163

Introduction to Python II

Strings and Lists

Hunter Schafer

🎢 Listening to: Carly Rae Jepsen

πŸ’¬ In Zoom Chat: Tea or coffee? Other?

2 of 9

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 9

Questions during Class

Feel free to ask questions in Zoom chat, someone can respond in chat or Hunter might answer the question while talking. Don’t feel pressured to read messages, we will point out anything important for everyone to hear.

TAs and I will be hanging out in chat before/after class and can answer questions or just chat if you want to hang out!

If we miss your question in chat, feel free to ask after class or post on EdStem so we can answer later!

During breakouts, please use chat if you don’t have a mic!

3

4 of 9

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 β€œβ€β€)

None

4

5 of 9

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 9

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 9

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 commonly will return None in bad cases

7

def increment(x):

if x < 0:

return None

else:

return x + 1

if increment(-1) is None:

print('Failed')

8 of 9

Group Work: Mechanics

Group work will be done in Zoom breakouts

  • Should be about 6 people per group (might move around)

Will automatically move you to breakout rooms and bring you back when we want to debrief.

  • Will send broadcast messages for quick announcements
  • You can ask for help and a TA will come over when they are available!
  • You can also post on the Ed Megathread any quick questions!

8

9 of 9

Group Work:

Best Practices

When you first working with this group:

  • Turn on your mic / camera or introduce yourself in chat
    • We prefer mic/camera if available to encourage sense of human interaction :)
  • Share your name + where in the world you’re calling in from!
  • Elect one person to β€œdrive” and share their screen for reference

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!

9