-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.py
More file actions
209 lines (143 loc) · 4.33 KB
/
lists.py
File metadata and controls
209 lines (143 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Lists are created using square brackets[]
l = [1, 4, 5, 12, 45]
print('Lists = ', l)
print(l[0])
print(l[1])
# Sublist
# Sometimes you want just a small portion of a list—a sublist. Sublists are simply subsets of a list
# they can be retrieved using a technique called slicing.
# Given a list l, to create sublist l1 and l2 write:
l = [3, 5, 4, 6, 42, 22]
l1 = l[0:3]
l2 = l[3:5]
print(l1, l2)
# Iterating lists using for
l = [2020, 2030, 2040, 2050]
for i in l:
print("Year: ", i)
# Given a getSublist() function, create a list named l[1, 4, 9, 10, 23]. Using list slicing, get the sublists[1, 4, 9] and [10, 23].
# Input
# A list
# Output
# Two sublists
def getSublist():
l = [1, 4, 9, 10, 23]
list_1 = l[0:3]
list_2 = l[3:]
return[list_1, list_2]
[list_1, list_2] = getSublist()
print("List 1:", list_1)
print("List 2:", list_2)
# Given an AppendtoList() function, create a list named l with the following values:
# [1, 4, 9, 10, 23]
# and appends the number 90 at the end of the list.
# Input
# A list of numbers
# Output
# Append the value 90 to the end of the list l
# Sample Input
# [1, 4, 9, 10, 23]
# Sample Output
# [1, 4, 9, 10, 23, 90]
def AppendtoList():
l = [1, 4, 9, 10, 23]
l.append(90)
return l
print(AppendtoList())
# Given a getAverage() function, create a list named l with the following values:
# [1, 4, 9, 10, 23]
# Calculate the average value of all values in the list.
# Input
# A list of integers
# Output
# An average of values in the list
# Sample Input
# [1, 4, 9, 10, 23]
# Sample Output
# 9.4
def getAverage():
l = [1, 4, 9, 10, 23]
average = sum(l) / len(l)
return average
print('Average = ', getAverage())
# Remove Sublist From List
# Can use remove()
def removeList():
l1 = [1, 4, 9, 10, 23]
l2 = [4, 9]
l1.remove(l2[0])
l1.remove(l2[1])
return l1
l1 = removeList()
print(l1)
def removeFromList():
l1 = [1, 4, 6, 7, 7, 8]
l2 = [4, 6]
l1.remove(l2[0])
l1.remove(l2[1])
return l1
l1 = removeFromList()
print(l1)
# List Comprehensions
# List comprehensions are a concise way to create lists.
# They consist of square brackets containing an expression followed by the for keyword
# the result will be a list whose results match the expression.
print([x*x for x in [1, 2, 3, 4, 3, ]])
# Using Raneg
print(x*x for x in range(4)) # Output - [1, 4, 9, 16, 9]
# The following python code displays all elements from 0 to 9 which are divisible by 2.
print([x*x for x in range(10) if x % 2 == 0])
# Challenge 5: List of Squares
# Given a getSquare() function, create a list with the squares of the first 10 numbers, i.e., in the range from 1-10.
# Input
# An empty list
# Output
# An updated list with the square of each value in the list
def getSquare():
# Write your code here
l1 = [x*x for x in range(11) if x != 0] # Create your list here
return l1
l1 = getSquare()
print(l1)
def getSquareTwo():
l1 = [x*x for x in range(1, 11)]
return l1
print(getSquareTwo())
# Challenge 6: - List of Cubes
def getCube():
l1 = [x**3 for x in range(21) if x != 0] # range(1,21)
return l1
print(getCube())
# Challenge 7: Lists of Even and Odd Numbers
# In this challenge, you are required to create a list containing even and odd numbers.
# Input
# Two empty lists
# Output
# List 1 with even numbers
# List 2 with odd numbers
def ListofEvenOrOdd():
l1 = [x for x in range(0, 20) if x % 2 == 0]
l2 = [x for x in range(1, 21) if x % 2 != 0]
return [l1, l2]
[l1, l2] = ListofEvenOrOdd()
print('Lists even numbers:', l1)
print('List odd numbers:', l2)
# Challenge 8: Sum of Squares of Even Numbers
# In this challenge, your task is to create a list of the squares of even numbers.
# Input
# A list with the square of even numbers from 0-20
# Output
# The sum of the numbers in the list
def evenSquare():
l1 = [x*x for x in range(0,20) if x % 2 == 0]
return sum(l1)
print("Even squared numbres:", evenSquare())
# Even Squares Not Divisible By Three
# Use a list comprehension that iterates over a range of 0-21,
# increments the number in the range by 2 and squares each remaining number.
# Also, use a predicate clause to check if the squared number is not divisible by 3,
# then put the number in the list.
def divisibleByThree():
l1 = [x*x for x in range(0, 21, 2) if x % 3 != 0]
return l1
print(divisibleByThree())