Calvin (Deutschbein)
W4Fri: 20 Sep
>>> x = "hello world"
>>> print(x)
hello world
>>> type(x)
<class 'str'>
>>> for letter in "cs":
... print(letter)
...
c
s
for letter in "abcdef":
print(letter)
a
b
c
d
e
f
vowels = "aeiou"
for letter in "abcdef":
print(letter, letter in vowels)
a True
b False
c False
d False
e True
f False
vowels = "aeiou"
for letter in "abcdef":
if letter not in vowels:
print(letter)
b
c
d
f
>>> for letter in "cs":
... print(letter)
...
c
s
>>> for i in range(len('cs')):
... print('cs'[i]) # get it? csi?
...
c
s
word = "lied"
# word = "veil"
for i in range(len(word)):
print(word[i])
l
i
e
d
word = "lied"
# word = "veil"
for index in range(len(word)):
if word[index] == 'i':
# check the letter *before* the 'i'
print(word[index-1])
l
# word = "lied"
word = "veil"
for index in range(len(word)):
if word[index] == 'i':
# check the letter *before* the 'i'
print(word[index-1])
e
def i_before_e(word:str) -> bool:
for index in range(len(word)):
if word[index] == 'i':
# check the letter *before* the 'i'
if word[index-1] == 'e':
return False
>>> i_before_e('veil')
False
>>>
>>> i_before_e('lied')
>>>
def i_before_e(word:str) -> bool:
for index in range(len(word)):
if word[index] == 'i':
# check the letter *before* the 'i'
if word[index-1] == 'e':
return False
return True # this is indented the same as the 'for' loop
>>> i_before_e('icee') # its a disaster
def i_before_e(word:str) -> bool:
for index in range(1, len(word)): # fix is here.
if word[index] == 'i' and word[index-1] == 'e':
return False
return True # this is indented the same as the 'for' loop
h | e | l | l | o | w | o | r | l | d | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
-10 | -9 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
>>> "hello world"[-3]
'r'
>>> "hello world"[8]
'r'
if word[index] == 'i' and word[index-1] == 'e':
abcs = 'abcdefghijklmnopqrstuvwxyz'
for i in range(9,20,3):
print(i, abcs[i])
9 j
12 m
15 p
18 s
abcs = 'abcdefghijklmnopqrstuvwxyz'
new_word = ""
for i in range(9,20,3):
new_word += abcs[i]
>>> print(new_word)
jmps
new_word = ""
for i in range(9,20,3):
new_word += abcs[i]
new_word = abcs[9:20:3]
>>> abcs[9:20:3]
'jmps'
>>>
>>> "hello world"[:5]
'hello'
>>> "hello world"[5:]
' world'
>>> "deeps"[::-1]
'speed'
def remove_space(s:str) -> str: # given string, make new string
for i in range(len(s)): # check each letter
if s[i] == " ": # if letter is space...
return s[:i] + s[i+1:] # before space + after space
>>> remove_space("hello world")
'helloworld'
def space_to_underscore(s:str) -> str:
for i in range(len(s)): # check each letter
if s[i] == " ": # if letter is space...
return s[:i] + "_" + s[i+1:]
>>> space_to_underscore("hello world")
'hello_world'
>>> space_to_underscore("world")
>>>
def space_to_underscore(s:str) -> str:
for i in range(len(s)): # check each letter
if s[i] == " ": # if letter is space...
return s[:i] + "_" + s[i+1:]
return s # return 's' if no spaces
>>> space_to_underscore("hello world")
'hello_world'
>>> space_to_underscore("world")
'world'
def spaces_to_underscores(s:str) -> str:
for i in range(len(s)): # check each letter
if s[i] == " ": # if letter is space...
return s[:i] + "_" + spaces_to_underscores(s[i+1:])
return s # return 's' if no spaces
>>> spaces_to_underscores("thats me espresso")
'thats_me_espresso'
def n_spaces_before(n:int,s:str) -> NoneType: # print -> None
print(n * ' ', s) # test - that is 'n times space plus s'
def spaces_to_underscores(s):
for i in range(len(s)):
if s[i] == " ":
return s[:i] + "_" + spaces_to_underscores(s[i+1:])
return s