Python Basics I Flashcards

1
Q

What are the six basic types of data structures in Python?

A
  1. Numbers
  2. Strings
  3. Lists
  4. Sets
  5. Tuples
  6. Dictionaries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a list look like?

A

It is a list of objects separated by commas and enclosed with square brackets.
[1,2,3] [1,2,’cow’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create a sublist (A list from an existing list)

A

Use slice notation, for example,
[1,2,3,4][1:3]
The interval specified is closed on the left, and open on the right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Python Variable?

A

It is a container for storing data values. For example, x = [1,2,3,4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How could you extract one element of a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you change the value of one element of a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you form a set? (i.e. what does a set look like?)

A

Collection of objects in braces.
{1,2,’squirrel’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the fundamental difference between a list and a set?

A

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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a nested list?

A

A list, one of whose elements is a list.
For example [1,2,[3,4]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you embed a variable inside of a string?

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly