Intro To Lists Flashcards
How do you create a list?
Square brackets [ ]
Every item in a list is a (blank), so each should be enclosed with a (blank)
Python string
Quotation mark
Name 3 things a “[ ]” will make it easier to do.
- Get an item at a specified position (1st, 2nd, 3rd etc)
- Check the number of items
- Add/remove items
How would you count the number of entries in a list?
len()
Referring to any item in the list according to its position in the list is called what?
Indexing
How would you pull the first entry in a list?
[0]
How would you pull the second entry in a list?
[1]
How would you pull the last entry in a list?
Use one less than the length of the list
(e.g if the length of list is 10, index using 9)
How would you print multiple strings in Python with a single command?
Separate them with a comma
Pulling a segment of list is called?
Slicing
To pull the first 3 items in a list what would you use?
[ :3]
If you wanted the last 4 items in a list what would you use?
[-4: ]
How would you remove an item from a list?
.remove()
How would you add an item to a list?
.append()
What does this tell you?
print(“blank:” , sum(hardcover_sales[ :5]) /5)
You want to take the hardcover sales in first five days and divide it by 5
What does split() method do?
Splits a string into a list
How would you quickly turn a string into a list?
.split()
Indexing is used to
Obtain individual elements
Slicing is used to
Obtain a sequence of elements
Lists in Python represent what?
Ordered sequences of values
How would you access an element at the end of the list?
[-1]
How would you express wanting all the letters from index 3 onward?
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’]
Letters[3:]
Strings will be sorted how?
Alphabetically
Numbers are sorted how?
Numerically
A function attached to an object is called a what?
Method
Non-function things attached to an object, such as imag, are called what?
Attributes
What is an imag
An imaginary part of a #
What does list.pop do?
Removes and returns the last element of a list
Returns the index number at which a particular element appears in a list
list.index
To avoid surprises, what will the ‘in’ operator determine?
Whether a list contains a particular value
“Earth” in planets
True
How do you create a tuple?
() parenthesis
Are tuples immutable?
Yes meaning they cannot be modified