Module 6: Flashcards

1
Q

list

A

a container that stores a sequence of values

square brackets indicate that we are creating a list.

values = example: [1,2,3,4,5,6]

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

How can you get the length of a list?

A

len()

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

How can you get the number of a specific number of the list?

A

get the index
example:
values[n]

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

How can you set a specific element of a list to another integer?

A

Example:
values[2] = 15

sets the 3rd integer to a value of 15

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

Does a integer list start at 0 or 1 for a list?

A

0

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

How do negative values react with lists?

A

negative subscripts provide access to the list elements in reverse order

example:
#sets the last element to 20

scores[len(scores) -1] = 20

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

How can a new element be appended to the end of a list?

A

with the append method

example:
friends.append(“Harry”)

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

How can you insert OR change an element in a list with a specific position?

A

with the insert method OR using brakets ~

example:
friends.insert(1, “Cindy”)

OR
friends[1] = “Cindy”

“Cindy” will be inserted in position 1 HOWEVER, brackets will change the variable entirely not inserting it

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

How can you test if an element is present in a list?

A

Use an in operator:

if “Cindy” in friends:
print(“She’s a friend”)

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

How do you find the position at which an element occurs?

A

Will set n to the position at which Emily is at

the index method:

n = friends.index(“Emily”)

HOWEVER index only finds the first occurrence if it is in the list multiple times.

If a second argument is passed to the index method, its value indicates the starting position in the list where the search begins

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

Whats the main difference between lists and strings?

A

They are both sequences, but lists can hold values of any type, whereas strings are sequences of characters. Strings are also immutable (meaning you cannot change the characters in the sequence), whereas, lists are mutable.

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

Concatenation of lists and example

A

you can do this with +

example:
myNumbers = [6,7,8]
yourNumbers = [3, 4 5]
ourNumbers = myNumbers+yourNumbers
#Sets ourNumbers to [6,7,8,3,4,5]

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

replication

A

replication of two list uses the “*” operator

numbers = [1,2,3]*3
#Sets numbers to [1,2,3,1,2,3,1,2,3]

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

equality and inequality testing

A

You can use the == operator to compare whether two lists have the
same elements, in the same order.

[1, 4, 9] == [1, 4, 9] # True
[1, 4, 9 ] == [4, 1, 9] # False.

The opposite of == is !=

[1, 4, 9] != [4, 9] # True.

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

Sum, Maximum, Minimum

A

If you have a list of numbers, the sum() function yields the sum of all values in the list.

sum([1, 4, 9, 16]) # Yields 30

For a list of numbers or strings, the max() and min() functions return the largest and smallest value:

max([1, 16, 9, 4]) # Yields 16
min(“Fred”, “Ann”, “Sue”) # Yields “Ann”

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

sorting

A

The sort() method sorts a list of numbers or strings from smallest to largest:

values = [1, 16, 9, 4]
values.sort() # Now values is [1, 4 , 9, 16]

17
Q

Copying lists

A

using prices= values is not duplicating the list, but rather you are simply getting another reference to the same list

use the list() function to duplicate the list (that is, a new list that has the same elements in the same order as a given list)

Example: prices = list(values)

18
Q

slices of a list

A

Sometimes you want to look at a part of a list. Suppose you are given a list of temperatures, one per month:
temperatures = [18, 21, 24, 33, 39, 40, 39, 36, 30, 22, 18]

  • You are only interested in the temperatures for the third quarter, with index values 6, 7, and 8
  • You can use the slice operator to obtain them:
    thirdQuarter = temperatures[6 : 9]
  • The arguments are the first element to include, and the first to exclude
  • So in our example we get elements 6, 7, and 8
  • Both indexes used with the slice operator are optional
  • If the first index is omitted, all elements from the first are included
  • The slice temperatures[ : 6]
  • Includes all elements up to, but not including, position 6
  • The slice temperatures[6 : ]
  • Includes all elements starting at position 6 to the end of the list

** You can assign values to a slice: temperatures[6 : 9] = [45, 44, 40]
* Replaces the values in elements 6, 7, and 8

19
Q

filling a list

A

This loop creates and fills a list with squares (0, 1, 4, 9, 16, …):

values = []
for i in range(n) :
values.append(i * i)

20
Q
A
21
Q

how to compute a sum of numbers example

A

result = 0.0
for element in values:
result = result + element

22
Q

concatenate strings example

A

you only need to change the initial value compared to a sum of numbers list:

example:
result = “”
for element in names :
result = result + element

23
Q

element list separators

A

Add the separator before each element (there’s one fewer separator
than there are numbers) in the sequence except the initial one (with
index 0), like this:

for i in range(len(names)) :
if i > 0 :
result = result + “, “
result = result + names[i]

OR

If you want to print values without adding them to a string:

for i in range(len(values)) :
if i > 0 :
print(“ | “, end=””)
print(values[i], end=””)
print()

24
Q

Here is the implementation of the max and min algorithm for a list

A

largest = values[0]
for i in range(1, len(values)) :
if values[i] > largest :
largest = values[i]

smallest = values[0]
for i in range(1, len(values)) :
if values[i] < smallest :
smallest = values[i]

25
Q

What is a linear search?

A

searching a container (such as a set or list) for an object inspecting each element in turn (aka searching sequentially).

26
Q

Examples of a linear search

A

limit = 100
pos = 0
found = False
while pos < len(values) and not found :
if values[pos] > limit :
found = True
else :
pos = pos + 1
if found :
print(“Found at position:”, pos)
else :
print(“Not found”)

27
Q

Collecting vs counting certain elements/matches of a list.

A

For collecting you can append the results
Example: collecting all values that are > 100:

limit = 100
result = []
for element in values :
if (element > limit) :
result.append(element)

For counting you can use a counter
Example: counting the number of all values that are > 100

limit = 100
counter = 0
for element in values :
if (element > limit) :
counter = counter + 1

28
Q

Removing elements/matches to a certain condition you are looking for in a list example and why you should not use a for loop.

A

for in range loops will remove the element and then skip analyzing the next element

i = 0
while i < len(words) :
word = words[i]
if len(word) < 4 :
words.pop(i)
else :
i = i + 1

29
Q

Steps of swapping elements good example vs a bad example

A

list of values has a sequence of [13, 4, 20, 5] , and you are trying to swap value[i] and value[j]. values[i] is [0], values [j] is [3]

i = 1
j = 3
values = [13, 4, 20, 5]
first = values[i]
values[i] = values[j]
values[j] = first
#Good Example

values[i]=values[j]
values[j]=values[i]
#Bad Example

30
Q

Give an example of turning user input into a list :)

A

Start with an empty list and, as each value is read, append the value to the end of the list:

values = []
print(“Please enter values, Q to quit:”)
userInput = input(“”)
while userInput.upper() != “Q”
values.append(float(userInput))
userInput = input(“”)

31
Q

sum function that does not modify them

A

def sum(values) :
total = 0
for element in values :
total = total + element
return total

32
Q

function that multiplies all elements of a list (AKA modifying all list elements) by a given factor:

A

def multiply(values, factor) :
for i in range(len(values)) :
values[i] = values[i] * factor

33
Q

example of a function that squares and returns a list

A

def squares(n) :
result = []
for i in range(n) :
result.append(i * i)
return result

34
Q

tuple

A

an immutable sequence (unlike a list that is a mutable sequence)

35
Q

call by value vs call by reference

A
  • Call by value: When the contents of a variable that was passed to a function can never be changed by that function
    -Call by reference: Function can change the arguments of a method call. A Python method can mutate the contents of a list when it receives
    an reference to
36
Q

What does the asterisk indicate in a function when it is in the arguments section

A

it indicates that the function can receive any number of arguments

example:
def sum(*values):

37
Q
A