in Operatordel StatementI have posted homework 11 here.
This is the last homework assignment.
for loop
>>> for team in teams: ... print(team) ... Red Sox Orioles Blue Jays Rays Yankees
>>> for index in range(len(teams)): ... print(teams[index]) ... Red Sox Orioles Blue Jays Rays Yankees
for loop when you want to change the elements in a list >>> new_list = [] >>> new_list []
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX]
>>> days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
>>> weekdays = days[1:6] >>> weekdays ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
>>> days ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> days[:4] ['Sunday', 'Monday', 'Tuesday', 'Wednesday'] >>> digits [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> digits[:5] [1, 2, 3, 4, 5]
>>> days[4:] ['Thursday', 'Friday', 'Saturday'] >>> digits[5:] [6, 7, 8, 9, 0]
>>> days[:] ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> digits[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> daynames = days[:] >>> daynames ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
range function
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX:STEP_VALUE]
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> digits[1:10:2] [2, 4, 6, 8, 0]
digits[1::2] [2, 4, 6, 8, 0]
in Operatorin is an operator that works on objects
that are a collection of values
VALUE in LIST
in operator returns True if the LIST
contains VALUE
False
>>> digits [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> 1 in digits True >>> 11 in digits False
| Method | Description |
|---|---|
| append(item) | Adds item to the end of the list |
| pop(index) | Removes the element at a given index from the list and returns the value. If called with no argument, it returns the last element and deletes it from the list. |
| sort() | Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value) |
| reverse() | Reverses the order of the items in the list |
>>> teams = [] >>> teams []
>>> teams.append('Red Sox')
>>> teams
['Red Sox']
>>> teams.append('Orioles')
>>> teams.append('Blue Jays')
>>> teams.append('Rays')
>>> teams.append('Yankees')
>>> teams ['Red Sox', 'Orioles', 'Blue Jays', 'Rays', 'Yankees']
>>> numbs = [1, 2, 3, 4, 5] >>> n = numbs.pop(0) >>> n 1 >>> numbs [2, 3, 4, 5]
>>> n = numbs.pop() >>> n 5 >>> numbs [2, 3, 4]
>>> l2 = [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>> l2 [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>>l2.sort() >>> l2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l3 = [5, 4, 3, 2, 1] >>> l3.reverse() >>> l3 [1, 2, 3, 4, 5] >>> l3.reverse() >>> l3 [5, 4, 3, 2, 1]
>>> l4 = [2, 5, 9, 1, 8, 6, 3, 7, 4] >>> l4.sort() >>> l4 [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l4.reverse() >>> l4 [9, 8, 7, 6, 5, 4, 3, 2, 1]
del Statementdel statement
del LIST_VARIABLE[INDEX]
del comes the list variable and the index inside square brackets
>>> l5 = [1, 2, 3, 4, 5, 6] >>> del l5[2] >>> l5 [1, 2, 4, 5, 6]
min returns the smallest value in a list
>>> l6 [1, 4, 5, 6] >>> min(l6) 1
max returns the largest
max(l6) 6
file = open('numbs.txt', 'r')
total = 0
count = 0
for line in file:
number = int(line)
total += number
count += 1
average = round(total/count)
file.close()
file = open('numbs.txt', 'r')
above_average = 0
for line in file:
number = int(line)
if number > average:
above_average += 1
create an empty list
for each line in the file:
append the line to the empty list
file = open('numbs.txt', 'r')
numbers = []
for line in file:
numbers.append(int(line))
total = 0
for num in numbers:
total += num
average = round(total/len(numbers))
above_average = 0
for num in numbers:
if num > average:
above_average += 1
print(above_average)
for loops
for loop
file = open('numbs.txt', 'r')
total = 0
numbers = []
for line in file:
num = int(line)
numbers.append(num)
total += num
average = round(total/len(numbers))
above_average = 0
for num in numbers:
if num > average:
above_average += 1
print(above_average)
len on the list object
set a variable to a very low value
for each value in the file:
if the value is greater than the variable:
set the variable to this new value
max functionmin to get the minimum valuefor loop to create a list>>> number_1 = 5 >>> number_2 = number_1 >>> number_2 5
>>> number_1 = 6 >>> number_1 6 >>> number_2 5
>>> list_1 = [1,2,3,4,5,6,7] >>> list_1 [1, 2, 3, 4, 5, 6, 7, 8]
>>> list_2 = list_1 >>> list_2 [1, 2, 3, 4, 5, 6, 7, 8]
>>> list_1[6] = 8 >>> list_1 [1, 2, 3, 4, 5, 6, 8] >>> list_2 [1, 2, 3, 4, 5, 6, 8]
>>> list_1[6] = 8
>>> list_1
[1, 2, 3, 4, 5, 6, 7]
>>> list_2 = [] + list_1
>>> list_2
[1, 2, 3, 4, 5, 6, 7]
>>> list_1[6] = 8 >>> list_1 [1, 2, 3, 4, 5, 6, 8] >>> list_2 [1, 2, 3, 4, 5, 6, 7]
>>> list_1 = [1,2,3,4,5,6,7]
>>> list_2 = list_1[0:len(list_1)]
>>> list_1[6] = 9
>>> list_1
[1, 2, 3, 4, 5, 6, 9]
>>> list_2
[1, 2, 3, 4, 5, 6, 7]
>>> list_1 = [1,2,3,4,5,6,7]
>>> list_2 = list_1[:]
>>> list_1[6] = 10
>>> list_1
[1, 2, 3, 4, 5, 6, 10]
>>> list_2
[1, 2, 3, 4, 5, 6, 7]
use the filename to create a file object
create an empty list
for each line in the file:
convert the line into a number
add the number to the empty list
return a variable pointing to the list
#! /usr/bin/python3
# reads a text file containing integers
# and prints it
# reads a text file of integers
# and stores them in a list which is returned
def read_integers_into_list(filename):
file = open(filename, "r")
new_list = []
for line in file:
number = int(line)
new_list.append(number)
file.close()
return new_list
number_list = read_integers_into_list("temperatures.txt")
print("List:", number_list)
$ ./integers_read.py List: [76, 67, 74, 76, 84, 69, 67, 76, 66, 71]
set an accumulator to zero
loop through the list using the list address:
add each number to the accumulator
return the accumulator divided by the length of the list
def average_list(list):
total = 0
for index in range(len(list)):
total += list[index]
return total/len(list)
def entries_above(list, value):
number_above = 0
for index in range(len(list)):
if list[index] > value:
number_above += 1
return number_above
#! /usr/bin/python3
# this script reads in daily temperatures from a file and
# calculates the numbers of days with above average temperatures
# reads integers into a list
def read_integers_into_list(filename):
file = open(filename, "r")
new_list = []
for line in file:
number = int(line)
new_list.append(number)
file.close()
return new_list
# returns the average of list of numbers
def average_list(list):
total = 0
for index in range(len(list)):
total += list[index]
return total/len(list)
# returns the number of entries in a list
# above a certain value
def entries_above(list, value):
number_above = 0
for index in range(len(list)):
if list[index] > value:
number_above += 1
return number_above
temps = read_integers_into_list("temperatures.txt")
print("Temperature list:", temps)
average = average_list(temps)
print("Average:", average)
print("Days above average:", entries_above(temps, average))
$ ./above_average_2.py Temperature list: [76, 67, 74, 76, 84, 69, 67, 76, 66, 71] Average: 72.6 Days above average: 5
>>> def double_list(list): ... for index in range(len(list)): ... list[index] = 2 * list[index] ... >>> numbers = [1,2,3,4,5] >>> double_list(numbers) >>> numbers [2, 4, 6, 8, 10]
for line in file:
date, temp = line.split()
print(date, temp)
>>> line = '2017-06-01 67' >>> fields = line.split() >>> fields ['2017-06-01', '67'] >>> type(fields) <class 'list'>
dates = []
temps = []
for line in file:
date, temp = line.split()
temp = int(temp)
dates.append(date)
temps.append(temp)
set the variable highest_index to 0
set the variable max_temp to -500
for the index of each entry in the two lists:
get the temperature for the entry
if the temperature is greater than max_temp:
set max_temp to the temperature
set highest_index to the current index
return the date at highest_index
def higest_temp_date(dates, temps):
highest_index = 0
max_temp = -500
for index in range(len(temps)):
temp = temps[index]
if temp > max_temp:
max_temp = temp
highest_index = index
return dates[highest_index]
Respect the work required to produce new ideas, inventions, creative works, and computing artifacts.
Respect privacy