Chapter 7 Flashcards

1
Q

What is a sequence?

A

An Object that contains multiple items of data

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

What is a list?

A

An object that contains multiple data items
EX: even_numbers = [2, 4, 6, 8, 10]
info = [‘Alicia’, 27, 1550.87}

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

What is an element?

A

Each object that is stored in a list

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

How do you display an entire list?

A

Use the print function:
numbers = [5, 10, 15, 20]
print(numbers)

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

How would you convert certain objects to lists using range:

A

numbers = list(range(5))

  • The range function is called with 5 passed as an argument. The function returns an iterable containing the values 0, 1, 2, 3, 4.
  • The iterable is passed as an argument to the list() function. – -The list() function returns the list [0, 1, 2, 3, 4].
  • The list [0, 1, 2, 3, 4] is assigned to the numbers variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a repetition operator?

A
when the operand on the left side of the * symbol is a sequence (such as a list) and the operand on the right side is an integer,
EX: 
1  >>> numbers = [1, 2, 3] * 3 [Enter]
 2  >>> print(numbers) [Enter]
 3  [1, 2, 3, 1, 2, 3, 1, 2, 3]
 4  >>>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you access a list with a for loop?

A

numbers = [1, 2, 3, 4]
for num in numbers:
print(num)
The numbers variable references a list with four elements, so this loop will iterate four times. The first time the loop iterates, the num variable will reference the value 1, the second time the loop iterates, the num variable will reference the value 2, and so forth.

-num variable value has no effect on the output

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

What is an index ?

A

Each element in a list has an index that specifies its position in the list. Indexing starts at 0, sothe index of the first element is 0
print(my_list[0], my_list[1], my_list[2], my_list[3])
EX #2:
index = 0
while index < 4:
print(my_list[index])
index += 1

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

What is an indexError exception?

A

raised if you use an invalid index with a list.

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

What is the len function in python?

A

returns the length of a sequence, such as a list.
EX: my_list = [10, 20, 30, 40]
size = len(my_list)

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

How do you use a len and range function to get the indexes in a list?

A
1  names = ['Jenny', 'Kelly', 'Chloe', 'Aubrey']
 2  for index in range(len(names)):
 3      print(names[index])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does mutable mean?

A
Lists in python can be changed
EX: Line 3 in this code rewrites an index:
1  numbers = [1, 2, 3, 4, 5]
 2  print(numbers)
 3  numbers[0] = 99
 4  print(numbers)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you concatenate a list?

A
Use the + operator:
list1 = [1, 2, 3, 4]
 list2 = [5, 6, 7, 8]
 list3 = list1 + list2
OR use += to concatenate one list to the other
list1 = [1, 2, 3, 4]
 list2 = [5, 6, 7, 8]
 list1 += list2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a slice?

A

A span of items that are taken from a sequence
EX: list_name[start : end]
days = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’,
‘Thursday’, ‘Friday’, ‘Saturday’]
mid_days = days[2:5]
[‘Tuesday’, ‘Wednesday’, ‘Thursday’]
(Does not include end but includes start)

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

How do you slice a list with a step value?

A

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
&raquo_space;> print(numbers[1:8:2]) [Enter]
[2, 4, 6, 8]
-Step value of 2 to get every other item form the list

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

What does the in operator do in Python?

A

-Used to determine whether an item is contained in a list
EXAMPLE:
9 search = input(‘Enter a product number: ‘)
10
11 # Determine whether the product number is in the list.
12 if search in prod_nums:
13 print(f’{search} was found in the list.’)
14 else:
15 print(f’{search} was not found in the list.’)

17
Q

What does the not in operator do in Python?

A

Determines if an item is not inside of a list
EXAMPLE:
if search not in prod_nums:
print(f’{search} was not found in the list.’)
else:
print(f’{search} was found in the list.’)

18
Q

What does the append method do?

A

Adds items onto a list:

name_list.append(name)

19
Q

What does the index method do?

A

Shows the index of where an item is located in a list
EXAMPLE:
Item_index = food.index(item)

20
Q

What does the insert method do?

A

Allows you to insert an item into a list at a specific position.
-Two arguments get passed into the insert method: index specifying where the item should be inserted and the item you want to insert
EXAMPLE:
name.insert(0, ‘Joe’)

21
Q

What does the sort method do?

A

Rearranges the elements of a list so they appear in ascending order (from the lowest value to the highest value)
EXAMPLE: my_list.sort()

22
Q

What does the remove method do?

A

Removes an item from the list. You pass an item to the method as an argument and the first element containing that item is removed.
EXAMPLE:
food.remove(item)

23
Q

What does the reverse method do?

A

Reverses the order of the items in the list
EXAMPLE:
my_list.reverse()

24
Q

What does the del statement do?

A

Allows you to remove an element from a specific index REGARDLESS of the item stored in that index
EXAMPLE:
del my_list[2]

25
Q

What is the min and max functions?

A

Min function: Accepts a list as an argument and returns the item that has the lowest value
Max function: Accepts a list as an argument and returns the item that has the highest value

EXAMPLE:
print(min(my_list))

26
Q

How do you create a copy of a list that is a separate entity from that list?

A

Use the append item as shown here:

list2 = [] + list1

27
Q

How do you use the random module with a list?

A

import random

random.choices(numbers, k=3)

28
Q

What does the writelines method do?

A
8      # Open a file for writing.
  9      outfile = open('cities.txt', 'w')
 10
 11      # Write the list to the file.
 12      outfile.writelines(cities)
 13
 14      # Close the file.
 15      outfile.close()

-saves the contents of a list to a file

29
Q

What is a list comprehension?

A

-an expression that reads an input list, using the values of the input list to produce an output list
EXAMPLE:
list1 = [1, 2, 3, 4]
list2 = [item for item in list1]

EX 2: shows length of strings in first list

str_list = [‘Winken’, ‘Blinken’, ‘Nod’]
len_list = [len(s) for s in str_list]

EX 3: Makes a new list of integers less than 10:

list1 = [1, 12, 2, 20, 3, 15, 4]
list2 = [item for item in list1 if item < 10]

30
Q

What are nested, or two dimensional lists?

A

Lists inside of lists

1 &raquo_space;> students = [[‘Joe’, ‘Kim’], [‘Sam’, ‘Sue’], [‘Kelly’, ‘Chris’]] [Enter]

31
Q

How would you write a for loop for nested loop?

A

10 for row in values:
11 for element in row:
12 print(element)
13

32
Q

What is a tuple?

A

A tuple is a sequence like a list except it cannot be changed

  • A tuple is written with parenthesis instead of brackets
  • Tuples supports all the same operations as a list except those that change the list

> > > my_tuple = (1, 2, 3, 4, 5) [Enter]
print(my_tuple) [Enter]
(1, 2, 3, 4, 5)

33
Q

What is the reason for tuples?

A

Processing tuples is faster than processing a list

It is also safer because it definitely can’t be changed

34
Q

What does the built-in list() function do? What does a built-in tuple() function do?

A

-Converts a tuple to a list
-Converts a list to a tuple
EXAMPLE

2 &raquo_space;> number_list = list(number_tuple) [Enter]
3 &raquo_space;> print(number_list) [Enter]
4 [1, 2, 3]
5 &raquo_space;> str_list = [‘one’, ‘two’, ‘three’] [Enter]
6 &raquo_space;> str_tuple = tuple(str_list) [Enter]

35
Q

What does the matplotlib package do?

A

library for creating two-dimensional charts and graphs
Not part of python library so must be installed separately
Useful for different graphical calculations

36
Q

How do you import pyplot from mathplotlib?

A

import matplotlib.pyplot