ENGG1810 Flashcards
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
- How do you set up a “while” loop?
ans
- When does a “while” loop end?
ans
- Draw an example flowchart for a “while” loop for a) one with a specified upper limit b) one with no defined end
ans
- What are the 6 different relational operators
ans
- What is the different between “=” and “==”
ans
- What are the 3 different logical operators? How do they work?
ans
- How do you instantly stop an endless loop?
ans
- Why do endless loops happen?
ans
- Compare “break” and “continue” functions. Why would you use either one?
ans
- What does a “for” loop do? Which data collections can you use it with?
ans
- How does a “for” loop turn out when you use a string?
ans
- How does a “for” loop turn out when you use a set?
Using a set - data will come out in random order, as set data is unordered
- What are the 3 different ways you can use a “for” loop with a dictionary?
ans
- Plot a graph!
ans
- How do you change the linestyle and marker on a graph?
ans
- How do you change the font colour and style on a graph?
ans
- Command lines
ans
- How would you end a “while” loop prior to the condition being false?
ans
- How would you skip to the next iteration of a loop?
ans
- Which function would you use if you wanted to make a string into a vertical list of characters?
ans
- What is the different between using a for() with dictionary, and using print() by itself?
ans
- What would stop a while loop from working?
ans
- What function would you use for definite iteration?
ans
- What function would you use for indefinite iteration?
ans
- What is meant by a multi-dimensional collection?
ans
- What is matplotlib.pyplot used for?
ans
- How do you combine graph plotting with loops
ans
- How do you open a text file in Python
ans
- What are the different read modes?
ans
- What does open(file,mode) do?
ans