Python Basics I Flashcards
What are the six basic types of data structures in Python?
- Numbers
- Strings
- Lists
- Sets
- Tuples
- Dictionaries
What does a list look like?
It is a list of objects separated by commas and enclosed with square brackets.
[1,2,3] [1,2,’cow’]
How do you create a sublist (A list from an existing list)
Use slice notation, for example,
[1,2,3,4][1:3]
The interval specified is closed on the left, and open on the right
What is a Python Variable?
It is a container for storing data values. For example, x = [1,2,3,4]
How could you extract one element of a list?
Use square brackets to index the list, for example [1,2,3,4][0] would extract the zeroth element, (The indexing is zero based) you could also have done x[0]
How can you change the value of one element of a list?
Identify it with indexing, then use an assignment statement to set the value,
x[0] = 10
x is now equal to [10, 2, 3,4]
How do you form a set? (i.e. what does a set look like?)
Collection of objects in braces.
{1,2,’squirrel’}
What is the fundamental difference between a list and a set?
The elements in a list do not need to be distinct, but they are distinct in a set.
For example, these two lists are not equal: [1,1] and [1], but these two sets are equal {1} and {1,1}
What is a nested list?
A list, one of whose elements is a list.
For example [1,2,[3,4]]
How do you embed a variable inside of a string?
By using braces {} and the .format method. For example
‘This is var1 {} and this is var2 {}’.format(‘Hello’, ‘World’)
returns,
‘This is var1 Hello and this is var2 World’