Python Programming DiSSS Phase 1 - Lists/Tuples

¡Supera tus tareas y exámenes ahora con Quizwiz!

What are 3 ways to improve the readability of the code?

1. Use 4 spaces per indentation level. 2. Use 79 characters per level or fewer. 3. Use single blank lines to group parts of your program.

When would you use a list instead of a dictionary?

A list is used when values are going to disorganized or rearranged within a group, but the value itself will not be changed. A dictionary is used when the values are going to changed thus a key is used in order to refer the value in question that is going to be changed.

How do you use a loop to generate a list of square numbers?

Create a blank, use range to create integer numbers and then square the numbers based off of that, and then append onto the list. Example: squares = [] for x in range(1, 11): square = x**2 squares.append(square)

How do you use a loop to convert a list of names to upper case? Or an action to change the format of names in a list?

Define the list, and resultant list, setup a function that will act out the changes made to list and have the names in the resultant list be equal to the function resultant. Example: names = ['kai', 'abe', 'ada', 'gus', 'zoe'] upper_names = [] for name in names: upper_names.append(name.upper())

How can you make a list of numbers from 1 to 1 million?

Functions on the same principle as before and then integrate the list preset command. numbers = list(range(1, 1000001))

What does str do to any single lined object?

It converts the item into a string

What is the difference between a tuple and a list+ syntax? What exception is there to the rule?

Lists can be changed and use [] square brackets. Tuple can't be changed and use () parentheses. Example: dimensions = (800, 600) The only exception is that you can overwrite an entire tuple.

What are list comprehensions?

Lists comprehensions are when lists are created based off of a range of numbers or another list.

How do you a comprehension to convert a list of names to uppercase?

Set the parameters of the second list = the function acted on the first list. Example: names = ['kai', 'abe', 'ada', 'gus', 'zoe'] upper_names = [name.upper() for name in names]

How do you generate a list of square numbers?

Set up the definition of the list within the square brackets Example: squares = [x**2 for x in range(1, 11)]

How do you loop through a tuple?

Setup the loop as the parameter name Example: dimension = (1, 2, 3, 4, 5, 6, 7, 8, 9) for dimension in dimensions: print(dimension) *Note: dimension must the variable used in the setup for the loop and the print parameter*

How do you pop the last item in a list?

Setup the variable with the pop string method making it default the last item in the list most_recent_user = users.pop() print(most_recent_user)

How do you get the first/second item in the list?

Since the first item of the list is using the index with the first item being 0, thus the second item being 1 first_user = users[0] second_user = users[1]

What are lists?

Square bracket information containers that have organized items with a specific organizational value attached to each item. This is for items that do not have changes in positions.

How do you add items starting with an empty list?

The last item = The first item. We will show it below users = [] users.append('val') users.append('bob') users.append('mia') users = [ 'val', 'bob', 'mia' ]

How do you get the last item in the list?

The last item of the normal index is going to be based on the length of the list, too much work. Use reverse indexing where the last item will be -1 last_user = users[-1]

How can you print the numbers from 0 to 1000?

The range() function starts at 0 by default, and stops one number below the number passed to it. Example: for number in range(1001): print(number)

How do you make a copy of a list?

To copy a list make a slice that starts at the first item and ends at the last item. If you try to copy a list without using this approach, whatever you do to the copied list will affect the original list as well Example: finishers = ['kai', 'abe', 'ada', 'gus', 'zoe'] copy_of_finishers = finishers[:]

How do you create a list?

To write a comprehension, define an expression for the values you want to store in the list. Then write a for loop to generate input values needed to make the list.

How do you make a list?

Use a plural name for the name of the list, square brackets to define the beginning and the end of the list, and commas in order to divide components. Example: users = ['val', 'bob', 'mia', 'ron', 'ned']

How do you find the last 3 items in the list?

Use reverse indexing is a similar fashion Example: last_three = finishers[-3:]

How do you add an item to the end of a list?

Use the .append string method, it will lead to the item added to being at the end of a list. Example: users.append('amy')

How do you remove the last item in a list?

Use the .pop string method with the items in the parentheses. Example: users.remove('mia')

How do you remove an element by its value?

Use the .remove string method Example: users.remove('mia')

How do you reverse the order of the list?

Use the .reverse string method before the parentheses Example users.reverse()

How do you overwrite a tuple?

Use the dynamic formatting of python in order to change the entire tuple Example: dimensions = (800, 600) print(dimensions) dimensions = (1200, 900)

How do you add items at a position in the list?

Use the index and add item string method in order. The number that you use is the exact position that it will be placed. users.insert(0, 'joe') users.insert(3, 'bea')

How do you change individual items in a list?

Use the index reference to then rearrange the list object value be treating it as a variable(valerie instead of val, ronald instead of ron) Example: users[0] = 'valerie' users[-2] = 'ronald'

How do you remove an element by its position?

Use the index value and the pop string method Example: del users[-1]

How do you find the length of the list?

Use the len string method command num_users = len(users) print("We have " + str(num_users) + " users.")

(Basic Statistics) How do you find the maximum value in a list?

Use the max present command Example: ages = [93, 99, 66, 17, 85, 1, 35, 82, 2, 77] youngest = max(ages)

(Basic Statistics) How do you find the minimum value in a list?

Use the min present command Example: ages = [93, 99, 66, 17, 85, 1, 35, 82, 2, 77] youngest = min(ages)

Give an example of visualizing code?

Use the print command in succession after each change in a program in order to observe successful change dogs = [] dogs.append('willie') dogs.append('hootz') dogs.append('peso') dogs.append('goblin') for dog in dogs: print("Hello " + dog + "!") print("I love these dogs!") print("\nThese were my first two dogs:") old_dogs = dogs[:2] for old_dog in old_dogs: print(old_dog) del dogs[0] dogs.remove('peso') print(dogs)

How do you get the first 3 items of the list?

Use the same slicing protocol that would be used for slicing strings Example: finishers = ['kai', 'abe', 'ada', 'gus', 'zoe'] first_three = finishers[:3]

How do you get the middle 3 items of list?

Use the slicing index in order to First number refers to starting number Second number refer to number you start at, but not include Example: middle_three = finishers[1:4]

How do you temporarily rearrange a list?

Use the sorted tag before applying the string method. When the normal tag is used, the list is back to normal. This works for both alphabetized, reversed and otherwise. print(sorted(users)) print(sorted(users, reverse=True))

(Basic Statistics) How do you find the sum of all the data in a list?

Use the sum preset command ages = [93, 99, 66, 17, 85, 1, 35, 82, 2, 77] total_years = sum(ages)

How do you rearrange a list in reverse alphabetical order?

Within the .sort string method, place reverse = True within the parentheses Example: users.sort(reverse=True)

What is the use of the range function in regards to lists?

You can use the range() function to work with a set of numbers efficiently. The range() function starts at 0 by default, and stops one number below the number passed to it. You can use the list() function to efficiently generate a large list of numbers.

How do you pop the first item in the list?

You have to assign the index value of the item you want to pop in the parentheses, and plug in 0 Example: first_user = users.pop(0) print(first_user)

How can you print the numbers from 1 to 1000.

You must account for the offset due to the index range offset. for number in range(1, 1001): print(number)

How do you sort a list?Is it permanent?

You sort a list using the .sort string method Which permanently rearranges the list Example: users.sort()

How do you sort a list? Is it permanent?

You use the .sort string method, which permanently rearranges the list into alphabetical order. Example: users.sort()

How do you print all of the items in a list?

You use the for conditional with the colon at the end ( As the first user is the same as the other user, you can use rickies and jerries in corresponding order) Example: for user in users: print("Welcome, " + user + "!") print("Welcome, we're glad to see you all!")


Conjuntos de estudio relacionados

Human Growth and Development Midterm

View Set

Penny Chapter 27 Fetal Heart and Chest

View Set

Prep U Chapter 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set

Geological Time Chapter 4 Section 3 / 4 Radioactive Dating / Geologic Time Scale

View Set