Python Crash Course - Chapter 4: Working with Lists

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What Python loop should you use to perform the same function with every item in a list?

When you want to perform the same function with every item in a list, you can use Python's for loop. magicians = ['alice', 'david', 'carolina'] for magician in magicians: print(magician) A name is stored in 'magician'. Then printed. Then, Python loops the action.

What function will determine the sum of a list of numbers?

Use the sum() function to find the sum of a number list. >>> digits = ['1','2'] >>> print(sum(digits)) 3

How would you use a negative index to outpoint a specific number of items at the end of a list.

A negative index in the first position of a slice will return all results from the position specified to the end of the list. For example players[-3:] will return the last three items from the players list.

What is the purpose of tuples in Python and what is the syntax?

A tuple is a sequence of immutable Python objects. In other words, tuples allow you to create a list that cannot be changed. A tuple uses parentheses to define a list as opposed to a regular list which uses square brackets. When compared with lists, tuples are simple data structures. Use them when you want to store a set of values that should not be changed throughout the life of a program.

What is a list comprehension?

Comprehensions in Python provide us with a short and concise way to construct new sequences (such as lists, set, dictionary etc.) using sequences which have been already defined.

How would you add numbers to a list?

Create a for loop and use the append function to add a range of numbers to a list numbers = [1, 2, 3, 4 ] for values in range(5,11): <<tab>>numbers.append(values) print(numbers) >>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

What is a good naming practise for temporary for loop variables?

For clarity and ease, it is a good practise to name the list as a plural and the variable in the singular. Using singular and plural names can help you identify whether a section of code is working with a single element from the list or the entire list. For example: * cars = [] * loop/variable = car

How would you use a slice to include all items from a list?

Leave out all indexes. ([:]) This tells python to make a slice that starts that the first item and ends with the last item.

What does the range() function do?

The range() function allows you to generate a series of numbers. When using the range() function, Python will start with the first value and stop on the second. >>> for value in range(1,5): >>> print(value) 1 2 3 4

What is looping in Python?

Looping allows you to take the same action, or set of actions, with every item in a list.

How does Python respond if you omit the first index in a slice?

Python automatically starts your slice at the beginning of the list.

How does Python treat indentations?

Python uses indentations to one line of code to indicate it is connected to the line above it. Longer Python programs can contain several levels of indintations to help communicate the overall program's organization.

How does Python respond if you omit the second index in a slice?

Python will return the items from the list starting with index inputted to the end of the list.

What is a slice in Python?

Slices allow you to work with a specific group of items in a list. Example: print(players[0:3]) would return items from a list at index 0 through the third position (ie index 0, 1, 2).

What does the colon do in a for loop?

The colon in a for loop tells Python the next line is the start of a loop.

What function will determine the minimum in a list of numbers?

Use the min() function to find the minimum of a number list. >>> digits = ['1','2'] >>> print(min(digits)) 1

How does Python represent exponents?

To denote something as an exponent in Python, use the double asterisk (**). >> print(4**2) 16

How would you make a list out of a range of numbers?

Use the list() function to make a list out of a range of numbers by wrapping the range with the list. numbers = list(range(1,6))

What function will determine the maximum in a list of numbers?

Use the max() function to find the maximum of a number list. >>> digits = ['1','2'] >>> print(max(digits)) 2


Kaugnay na mga set ng pag-aaral

Review Intro to Edu Test 2 3 & 4

View Set

English Comp 101 Chapter 6: Narration

View Set

HS&F: Chapter 8 - Nervous System

View Set

Fundamental of Nursing Chapter 27_Safety

View Set

Quiz: General Insurance Concepts

View Set

Chapter 10: Inheritance (True or False)

View Set

Unit 1 - Comparing and Contrasting

View Set

Targeted Medical-Surgical: Endocrine

View Set

advanced nursing final Keiser University

View Set

ACG 2071 EXAM 3 PRACTICE QUESTIONS

View Set