Chapter 4 - For loops, Strings, and Tuples Flashcards
an ordered list of things
a sequence
All sequences are made up of …..
elements
a sequence in which each element is one character
a string
(start, stop, step)
range(), the end point is not included (if it’s 50, 49 is the last number included)
find message length with…
a sequence’s length is equal to the number of elements it has.
len()
differences between elements and characters
a space is an element but not a character
going through a sequence one character at a time
sequential access
direct access
random access
you can specify a position in a sequence and get the element at that position
indexing
[-1] negative index number
last character
high = len(word) low = -len(word)
What index positions will this produce if word = “index”
position = random.randrange(low, high)
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4
In other words: it will produce a random number from and including the low end point, up to, but NOT including the high end point.
the inability of strings to be changed
string immutability
A variable written in ALL CAPS by convention
- more readible
- used for variable meant to be unchaning
constant
a section of elements (part of a sequence)
a slice
start = None
placeholder, evaluates to False
slicing shorthand
word[:]
type of sequence, unlike a string (which can only contain characters), it can contain elements of any type
it’s elements don’t have to be of the same type
whatever you assign to a variable, you can group together and store in a …..
tuple
Can a tuple be treated as a condition
Yes
Is an empty tuple treated as True or False?
False
*Some programming languages offer structures similar to tuples (arrays; vectors). However, those languages usually restrict….
the elements of the sequences to just one type
what does len() return when used on a tuple
the number of tuple elements
Tuples: mutable or immutable?
immutable
What happens during concatenation of tuples
The original tuple is not modified, because it is immutable. Instead, an augmented assignment operator can create a brand-new tuple with the elements from the previous tuples and assign that to (the name of the original variable [ tuple]) e. g. me = ("brain", "brawn") you = ("skills", "pills") me += you >>>print(me) ('brain', 'brawn', 'skills', 'pills')
message = “something”
print message backwards
print(message[::-1])