
Chapter 4 (For loops, strings and tuples)
For loops
word = 'hello'
for letter in word:
code
- Here for each iteration letter will equal a char out of for reading from left to right
Functions
range(start,end,step)
what would range(0,10,2) return?
0,2,4,6,8
it doesn't return 10
len(sequence)
What would len('hello') return?
5
it is not zero based
element in sequence
You can use the 'in' operator to check if a element is in a sequence, what would you call an element that is apart of the sequence?
You would call it a member of the sequence
Indexing
What is sequential access?
This is when you look at each element of a sequence to see if it is the one you are looking for
What is random access?
This is when you know the location of the element in a sequence, and can go straight to it
Is random access and indexing the same thing?
YES
word = 'nerd'
what char would be returned from the code above?
word[1]
e
word[4]
out of range
word[-4]
n
word[-1]
d
Strings
Define mutable
The variables value can change or mutate
Define immutable
The variables value can not change
Give two examples of immutable data types
string
tuple
Using all caps for a variable name is common practice for what?
Constants
What is slicing a string?
It is copying a range of elements from a string ranging from 1 to all of them
What value is assigned as a place holder value?
None
text = 'pizza'
What will be returned?
text[1:3]
iz
text[1:]
izza
text[-4:4]
izz
help guide
p i z z a
0 1 2 3 4
-1 -2 -3 -4 -5
Tuple
tuple = ('hello', 'wow', 'yo', 78, 7.89)
What is a tuple?
It is a immutable sequence of elements that can me of any data type
Can you concatenate tuples?
Yes, but if you declare a tuple with a single element, python might not interpret this as a tuple and then you cannot concatenate