Find the average highway fuel economy in python
Answers
Answered by
1
In this Python 3 tutorial, we will learn how to import and use CSV files in Python. First of you should be aware of what is CSV file- it means comma separated values.
Let’s import our datafile mpg.csv,(download by clicking on that file) which contains fuel economy data for 234 cars.
mpg : miles per gallon
class : car classification
cty : city mpg
cyl : # of cylinders
displ : engine displacement in liters
drv : f = front-wheel drive, r = rear wheel drive, 4 = 4wd
fl : fuel (e = ethanol E85, d = diesel, r = regular, p = premium, c = CNG)
hwy : highway mpg
manufacturer : automobile manufacturer
model : model of car
trans : type of transmission
year : model year
Program to Import, read and print first 3 dictionaries of the CSV file.
import csv
with open('mpg.csv') as csvfile:
mpg = list(csv.DictReader(csvfile))
mpg[:2] # The first two dictionaries in our list.
OutPut:
[OrderedDict([('', '1'),
('manufacturer', 'audi'),
('model', 'a4'),
('displ', '1.8'),
('year', '1999'),
('cyl', '4'),
('trans', 'auto(l5)'),
('drv', 'f'),
('cty', '18'),
('hwy', '29'),
('fl', 'p'),
('class', 'compact')]),
OrderedDict([('', '2'),
('manufacturer', 'audi'),
('model', 'a4'),
('displ', '1.8'),
('year', '1999'),
('cyl', '4'),
('trans', 'manual(m5)'),
('drv', 'f'),
('cty', '21'),
('hwy', '29'),
('fl', 'p'),
('class', 'compact')])]
Let’s import our datafile mpg.csv,(download by clicking on that file) which contains fuel economy data for 234 cars.
mpg : miles per gallon
class : car classification
cty : city mpg
cyl : # of cylinders
displ : engine displacement in liters
drv : f = front-wheel drive, r = rear wheel drive, 4 = 4wd
fl : fuel (e = ethanol E85, d = diesel, r = regular, p = premium, c = CNG)
hwy : highway mpg
manufacturer : automobile manufacturer
model : model of car
trans : type of transmission
year : model year
Program to Import, read and print first 3 dictionaries of the CSV file.
import csv
with open('mpg.csv') as csvfile:
mpg = list(csv.DictReader(csvfile))
mpg[:2] # The first two dictionaries in our list.
OutPut:
[OrderedDict([('', '1'),
('manufacturer', 'audi'),
('model', 'a4'),
('displ', '1.8'),
('year', '1999'),
('cyl', '4'),
('trans', 'auto(l5)'),
('drv', 'f'),
('cty', '18'),
('hwy', '29'),
('fl', 'p'),
('class', 'compact')]),
OrderedDict([('', '2'),
('manufacturer', 'audi'),
('model', 'a4'),
('displ', '1.8'),
('year', '1999'),
('cyl', '4'),
('trans', 'manual(m5)'),
('drv', 'f'),
('cty', '21'),
('hwy', '29'),
('fl', 'p'),
('class', 'compact')])]
Answered by
0
calculate_miles_per_gallon.py
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
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
# Explanation video: http://youtu.be/JK5ht5_m6Mk
# Calculate Miles Per Gallon
print("This program calculates mpg.")
# Get miles driven from the user
miles_driven = input("Enter miles driven:")
# Convert text entered to a
# floating point number
miles_driven = float(miles_driven)
# Get gallons used from the user
gallons_used = input("Enter gallons used:")
# Convert text entered to a
# floating point number
gallons_used = float(gallons_used)
# Calculate and print the answer
mpg = miles_driven / gallons_used
print("Miles per gallon:", mpg)
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
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
# Explanation video: http://youtu.be/JK5ht5_m6Mk
# Calculate Miles Per Gallon
print("This program calculates mpg.")
# Get miles driven from the user
miles_driven = input("Enter miles driven:")
# Convert text entered to a
# floating point number
miles_driven = float(miles_driven)
# Get gallons used from the user
gallons_used = input("Enter gallons used:")
# Convert text entered to a
# floating point number
gallons_used = float(gallons_used)
# Calculate and print the answer
mpg = miles_driven / gallons_used
print("Miles per gallon:", mpg)
Similar questions