06-09 list creation from iterator

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

The factors of an integer are those numbers that evenly divide into the integer. Given the integer n, create a list of all the factors of n, excluding 1 and n itself. The list should be ordered from smallest to largest. Assign the new list to the variable factors.

Solution 1 factors= [i for i in range(2, n) if n % i == 0] Solution 2 factors = [] for i in range(2, n): [Tab Key]if n % i == 0: [Tab Key][Tab Key]factors.append(i)

The factors of an integer are those numbers that evenly divide into the integer. Given the integer n, create a list of all the factors of n, excluding 1 and n itself. Assign the new list to the variable factors. The list should be ordered from largest to smallest.

Solution 1 factors= [i for i in range(n-1, 1, -1) if n % i == 0] Solution 2 factors = [] for i in range(n-1, 1, -1): [Tab Key]if n % i == 0: [Tab Key][Tab Key]factors.append(i)

Create a list consisting of the negative values in the list numbers. Assign the new list to the variable negatives.

Solution 1 negatives = [i for i in numbers if i < 0] Solution 2 negatives = [] for i in numbers: [Tab Key]if i < 0: negatives.append(i)

Given the list my_list containing integers, create a list consisting of all the even elements of my_list. Assign the new list to the variable new_list.

Solution 1 new_list = [i for i in my_list if i % 2 == 0] Solution 2 new_list = [] for i in my_list: [Tab Key]if i % 2 == 0: new_list.append(i)


Conjuntos de estudio relacionados

econ chpt 9 Saving, investment, and the financial system

View Set

Orpheus and Eurydice commonlit answers

View Set

BUSI - Ch. 12 (Distributing and Promoting Products and Services)

View Set

Business Analytics 251- Introduction to Concepts of Business Analytics

View Set

Exam 1 Multiple Choice Questions

View Set

Clinical Medicine: Ocular Manifestations Associated with Dermatological Conditions I

View Set

algebra 2b - unit 4: theoretically speaking lesson 16-20

View Set