06-09 list creation from iterator
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)