ENGG1810 Flashcards
(136 cards)
What is a function?
A sequence of re-usable code which executes a function when it is called
- What kind of brackets are used for: lists, tuples, sets, dictionaries
Lists: [ ]
Tuples: ( )
Sets: { }
Dictionaries: { }
- What function can you use to display the group type?
print(type (collection_name ))
- What does len() do? WWYUI
len() is used to show the length of a collection.
Could use it for checking how many items are in a set.
Can use it for calculating the average in a collection.
- What is the index of an item in a list?
An index is the unique identifier of an item in a list.
- Does indexing start from 0 or from 1
0
- What are the qualities of a list v set v tuple v dictionary? When would you use each one?
List - mutable, accepts duplicates, ordered, indexed
Set - mutable, no duplicates, unordered, unindexed
Tuple - immutable, accepts duplicates, ordered, indexed
Dictionary - ordered (key:value sets), no duplicates, semi-mutable?
- How do you slice a range of indexes from a list. Create a list of 5 or more items. Show what the 1st 3 items are.
- Use square brackets and colon.
- Number in front of colon is the lowest and included index, number after is the highest and excluded index
e.g. list=[red,orange,yellow,green,blue]
1st 3 items:
list[0:3] OR list[:3]
- Show everything before the 4th item of a list
print(list[:3])
- Show everything after the 2nd item on the list.
print(list[2:])
- Why would indexing be useful?
- Allows you to differentiate between duplicate values
- Helps you find items within a certain position of a collection vs specific values. So if you have items in order, indexing makes it easier to find items from a certain position in a collection.
- How do you use negative indexing?
- Negative indexing starts from the end - if the list was circular, -1 would be a step in the anti-clockwise direction
Negative indexing could be useful if your list keeps getting longer, and you only want the latest/most recent value
- How do you add items to the end of a list?
list_name.append(“new_item_name”)
- How do you add items to a certain index of a list?
list.insert(index,”item name”)
- How do you remove items from a list a) based on name; b) based on index?
list. remove(“item”)
list. pop(index)
- How do you change the value of an item on a list?
list_name[specific_index] = ‘value to update’
- Ie. I want to change the value of this indexed item on specified list
- How would you change values in a tuple?
Convert to a list first, change values, then convert back to a tuple
- How do you search for items in a set?
ans
- How do you search for a value in a dictionary?
ans
- How do you define a dictionary?
ans
- What is the general structure of an if/else block?
ans
- What is the general structure of an if/elif/else block?
ans
- How do you convert between tuples and lists?
ans
- What does a “while” loop do?
ans