2017-02-27

posted Feb 27, 2017, 7:06 AM by Samuel Konstantinovich   [ updated Feb 27, 2017, 7:11 AM ]
Reminder: Quiz Wed. No reference sheet.

Goal: Python String Slices 

Do Now: (On paper, do not use the computer!)
Evaluate each expression:
a) 3 / 4
b) 3 / 4.0
c) 16 % 5
d) 5 % 16
e) "1" + "2"
f) 1 + "2" 


Trace Through the loop: (On paper, do not use the computer!)
sum = 0
i = 3
while i < 20:
  sum = sum + i + 1
  i+=3

g) What is the value of sum when the loop ends?
h) How many times did i get incremented? 

Slices:
s[start:end]

Shortcuts:
s[start:]  is the same a s[start:len(s)]
s[:end] is the same as s[0:end]

Increments: 

s[start:end:stepSize]
Compare the results:
s="HappySmileHello!"
s[1:10]
s[1:10:2]
s[1:10:3]
What does the 3rd parameter do?

Negative Indices:
Which is easier:
s="HappySmileHello!"
s[len(s)-1]
s[-1]
s[len(s)-2]
s[-2]

It is nice to know negative indices, but I will not try to trick you with them (it is easy to be careless)
s[3:8]
s[8:3]
s[-4:-10]
s[-10:-4]



Comments