Python Lists

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What happens if you leave a slicing parameter empty? Which slicing parameter is optional?

*Start* has a default value of 0. *End* has a default value of -1. *Step* is the optional slicing parameter. Step has a default value of 1.

What is the difference between a *function* and a *method*?

A function is simply a block of code that only runs when the function is called. A method is just a function that belongs to a specific object/item.

What are the values in a list called?

A list is a sequence of individual values called "items" or "elements".

All functions contain a placeholder in them. Placeholders do not have a concrete value, but they are there for the programmer to pass arguments into them, which is then processed through the function into a resulting output value. What is this placeholder in the function called?

A parameter.

What is an index value?

A position in an ordered data structure; The location in a sequence.

What is a list comprehension? What is the syntax for a list comprehension?

A short way to create a list based on existing lists. The syntax goes as follows: newList = [transform(element) for element in oldList]

What data types can you include in a list?

Any data type. Numbers, letters, Boolean values, lists, etc.

What are data structures?

Data structures are collections of individual values treated as a single unit.

What are the two ways we can copy a list in a way where both lists *won't* synchronize when one changes?

Either use a for loop and append every item to the new list or use the copy( ) method. The syntax goes as follows: a = [...] b = a.copy( )

What does enumerate mean? What does the enumerate method do and where would you apply it?

Enumerate means to mention (things) one by one. The enumerate method will print the index value of each item in the list, when applied to the <list> portion of the loop header. For ex: for cntr in enumerate(list): print(list)

info = [...] info.insert(2, input("Question: ")) Explain this snippet of code.

In the 1st line, <info> is a list. In the 2nd line, the input( ) function is nested in the second parameter of the insert( ) method so that, when the user enters their response, that input will be inserted into <info> at index position 2.

Is a list ordered or unordered? Mutable or immutable? Duplicate members allowed?

Ordered. Lists are mutable (changeable). In other words, we can edit any item in any location of the sequence at any point in time. Lists allow duplicate members.

numbers = list(range(10)) How can I use slicing parameters to recreate this list, except counting backwards? What number would the list start at if I counted backwards?

Set the step parameter to -1 so that it will count through the list backwards. It will start counting from 9-0: print(numbers[ : :-1])

numbers = list(range(10)) Use slicing parameters to reproduce this list so that ONLY every 2nd index value is printed? What number will the list start counting at?

Set the step parameter to 2. The program will start counting at 0, so it will skip 1, but print 2, skip 3, but print 4, and so on: print(numbers[ : :2])

What are some examples of different data structures in Python?

String, list, tuple, dictionary, set...

list[0] = "name" vs list.insert(0, "name") What is the difference between inserting <"name"> using the 1st vs the 2nd strategy?

The 1st strategy will completely replace the value in index position 0 with <"name">. The 2nd strategy will just add <"name"> to the list at index position 0 and move everything after it one position to the right.

If you want to remove an item from a list based on its index position, or even delete the entire list, what keyword can we use? What is the syntax of this keyword, to delete single items vs entire lists?

The <del> keyword. The syntax goes as follows: del list[index] or del list

What are the 3 functions you could use to remove items from a list?

The <remove( )> method. The <pop( )> method. The <del> keyword.

What method can we use to add new items to the end of a list? What is the syntax for this method?

The append( ) method. The syntax goes as follows: list.append(value)

trans_date = ["07/99", "06/88", "05/77"] input_to_date= input("What end date would you like to see all the transaction amounts from: ") trans_date_to = trans_date.index(input_to_date) What will the index method return if the user input is equal to "06/88"? How should we print this output so that it's useful?

The call to <trans_date.index(...)> will return the index number for the first element it finds that exactly matches the value entered by the user. In this case, <print(int(trans_date_to))> would return a value of 1 since input value <"06/88"> matches the element in index position 1 in the <trans_date> list.

The first item in a list has an index value of what? The last item in the list has an index of what?

The first item in the list has an index value of 0. The last item in a list has an index value of whatever amount of items there are, minus one. This is because we start counting at 0, not 1. The last items index value can also be represented as (-1).

What method can we use to insert items into a list at a specific location? What is the syntax for this method?

The insert( ) method. The syntax goes as follows: list.insert(index, obj) #All on its own line

If you use the pop( ) method on a list without entering an argument, what will the default return value be?

The last item in the list

What function can we use to determine the number of elements in a list?

The len( ) function: print(len(value))

What method do we use when we want to remove an item from a list but store the removed item in another variable? Syntax?

The pop( ) method. The syntax goes as follows: list.pop(index)

What method do we use when we want to remove an item from a list based on its value (not its index)? Syntax?

The remove( ) method. The syntax goes as follows: namesList.remove("Layla")

What will the slice( ) object return after being given a valid argument?

The slice function will return a slice object that can then be used to slice an actual list.

What object can we use to specify how we want a sequence to be sliced without referring to any specific list?

The slice( ) object

When using the sort( ) method on a list of items, what happens to the original list?

The sort( ) method will replace the original list, so the order in which the items originally appear will be lost in the transformation.

What method do we use to organize values in a list alphabetically (for text values) or in ascending order (for numbers)? What is the syntax of this method?

The sort( ) method. The syntax goes as follows: list.sort( )

What method should I use to find the index value of a specific item? Syntax? Read the special instructions.

Use the index( ) method. The index( ) method expects one argument that EXACTLY matches the item in the list that you're trying to retrieve the index value for. The syntax goes as follows: variable = list.index(userInput_item)

In cases where a value occurs more than once in a list, what will happen if you try to remove that value from the list using the remove( ) method? Will it delete ALL instances of that value in the list?

When we use the remove( ) method to remove that value from the list, only the first instance is removed and the additional ones remain in the list.

When using the index method, do you need to convert the return value into a numeric data type before performing numeric operations on it?

Yes. After the index value is captured in <variable>, it still needs to be converted into an integer: <int(variable)>, before it can be used in a slicing parameter or have mathematical operations performed on it.

alpha = ["a", "b", "c"] I want to replace the first value in this sequence with "N". How do I do this without using any functions?

You can say <alpha[0] = "N"> and then printing <alpha> should reflect that change.

How do you concatenate multiple lists into a single list?

You use the + operator to concatenate two or more lists in Python. Ex: list1 + list2 + list3

namesList = ["John", "Mike"] namesList.insert(0, "Amy") print(namesList) What will be printed on the output screen?

['Amy', 'John', 'Mike']

What are the 3 slicing parameters? What does each parameter do?

[start:end:step] Start: Specifies the starting integer whose position would note where the slice would begin. End: An integer that denotes the position at which to end the slicing. Step: An integer that indicates the number of positions it will skip between grabbing values. The default is one.

How can we copy a list in a way that keeps both lists synchronized?

a = [1, 2, 3] b = a

⛳️ names = ["James", "Mark", "Curtis", "Lola"] for ctr in names: print(+ctr, sep = ", ") What is ctr? What is +ctr?

ctr is every item in the list. When you have a ctr in a ctr (*ctr), that's every item in every item. So in this case *ctr would be every individual letter in every individual name.

What's the easiest way to create a list consisting of five 0's without typing each individual zero?

list = [0] * 5

names = ["Mary", "David", "Nick"] names_upper = [ ] for name in names: names_upper.append(name.upper( )) print(names_upper) Convert this script into a list comprehension.

names = ["Mary", "David", "Nick"] names_upper = [name.upper( ) for name in names] print(names_upper)

numbers = [1, 2, -1, 6, -5, -2] positive = [ ] negative = [ ] for number in numbers: if number >= 0: positive.append(number) if number < 0: negative.append(number) Convert this script into a list comprehension.

numbers = [1, 2, -1, 6, -5, -2] list1 = [number for number in numbers if number >= 0] list2 = [number for number in numbers if number < 0]

numbers = [1, 2, -1, 6, -5, -2] numbers_EX = [ ] for number in numbers: if number >= 0: numbers_EX.append("Positive") if number < 0: numbers_EX. append("Negative") Convert this script into a list comprehension.

numbers = [1, 2, -1, 6, -5, -2] label = ["positive" if number >= 0 else "negative" for number in numbers]

numbers = [ ] for i in range(0, 11): numbers.append(i) print(numbers) Convert this script into a list comprehension.

numbers = [i for i in range(0, 11)] print(numbers)

Does remove( ), pop( ), and del use a value or an index, to remove items from a list?

remove( ): Value pop( ): Index del: Index

⛳️ What is a sep parameter? For example: for ctr in list1: print(*ctr, sep = ", ")

sep is a value that is placed between each item being printed in each list being displayed. In this case, a comma and a space.

numbers = [1,2,3,4,5,6,7,8,9,10] How do I use the slice( ) object to slice out numbers 3-7 on this list?

sliceNum = slice(2:7) print(numbers[sliceNum])

transList = [#,#,#] dateList = [#,#,#] userInput = input("Enter date here: ") dateTo = dateList.index(userInput) sliceNum = slice(0, int(dateTo)+1) slicelist = transList[sliceNum] #print(slicelist) Explain the syntax and purpose of this snippet.

•In line 1, you have list of dates called <dateList>. •In line 2, the program asks the user to enter a date that corresponds to one of the dates in the list. The response is then stored in a variable called <userInput>. •In line 3, we use the index method <list.index(userInput)> to find the exact index value of the item in <dateList> that corresponds to <userInput>. The index value that is returned as a result is stored in a variable called <dateTo>. •In line 4, we are using the slice function <slice( )> to create a range of index values that we can then store in the variable <sliceNum> and apply it to any data structure we'd like to slice. Notice how <dateTo> had to be converted into an integer because the index function returned a string value. Also notice the <+1> added to the end of <(dateTo)>. We want the slice to end at the index value AFTER that item. •In line 5, we apply the slice variable <[sliceNum]> to another list we created earlier in the script <transList>. Now that we have applied the slice to a data structure, we can then use the print function to produce the resulting output of that combination.


Set pelajaran terkait

physical assessment final exam - ATI questions

View Set

Number Properties, Prime Numbers, Prime Factorization

View Set

Ch 9 AA Partnerships: Formation and Operation: Problems

View Set

5 barriers to consumer satisfaction

View Set