Lecture 1 Flashcards
Controlling exit loops, lists and their methods
what does the break keyword do?
Exits the loop
how do you make an infinite loop?
while True:
what is a list? (3)
1 sequence data type
2 enclosed in [], data separated by commas
3 can contain data of many different types.
how do you access a list item within a list? (syntax)
listname[index of inner list[index of inner list item]]
what is the syntax for using the range object to create a list with numbers 1-10?
a=list(range(1, 11))
how do you create a list using list comprehensioN? (ex: squares of first 3 numbers)
squared_numbers = [x**2 for x in range(1, 4)]
what does append method do and what is syntax?
listname.append(x)
adds a single element x to the end of a list
what does the extend method do and what is the syntax?
listname.extend(x)
merges another list x to the end of the list
what is the insert method and what is the syntax?
listname.insert(index, x)
inserts element x at position index
what is the remove list method and what is the syntax?
listname.remove(value)
removes the first occurrence of element x from listname.
what is the pop method and what is the syntax?
listname.pop(index)
removes the LAST element of a list. if an argument is passed, that index item is removed.
what is the index method and what is the syntax?
listname.index(x)
searches for a given element from the start of the list and returns the position of the first occurrence. Throws error if not found.
what is the count method/syntax?
listname.count(x)
Counts the number of occurrences of an element x in a list listname.
sort method + syntax? (+3 things to remember)
listname.sort(reverse=True/False, key=function to specify sorting criteria by returning it)
**you have to use the keywords by name in the argument eg key=a
**you don’t need the () when naming the function in this syntax
*** does not work when sublists are involved
reverse method + syntax?
listname.reverse()
reverses the list