Python HW Help
All scripts should consist of a main function that includes a call to a function that includes the code. The optional scripts are simply for your learning and enjoyment, not for extra credit.
First Script (Required) – Reading a data file:
In this problem you will use a file which is named states.txt The states.txt file is located in Canvas (be sure you get the file with .txt extnsion). You will need to copy this file to your local working directory.
Log in to Canvas.
Select CIS 41A.
Select files.
Select states.txt
Move your cursor to the right of the green checkmark icon.
Click the down arrow next to the gear.
Click Download
Click Save File and OK
The file should be on your desktop or download directory.
Move the file to the directory constining your Python program.
The file has 50 lines of data, one for each state in the Unites States. Each line of data contains three pieces of data separated by a space: the two letter abbreviation of the state’s name, the region that the state is in, and the 2016 population of the state.
You need to find the state with the highest population in the Midwest region.
Sample Execution Results:
Highest population state in the Midwest is: IL 1280200_x000D_ _x000D_ **Here is what I have so far and need help:**_x000D_
import re
def states():
myFile = open(“States.txt”, “r”)
myFile.readline()
for lines in myFile:
if re.search(r”Midwest”, lines):
print(“Highest population state in the Midwest is:”, lines)
myFile.close()
In-class assignment
Dictionaries
This assignment consists of three parts.
Part 1 of 3 – Dictionary Basics:
- Create a dictionary of fruit and desserts made from the fruit. The fruit should be the key and the dessert should be the value. Use these key value pairs: apple:sauce, peach:cobbler, carrot:cake, strawberry: sorbet, banana:cream pie.
- Add the mango fruit to the dictionary. Its dessert is sticky rice.
- Change the strawberry dessert to shortcake.
- Carrot is not a fruit, so remove carrot from the dictionary.
- Print the apple dessert.
- See if a banana dessert exists.
- See if a pear dessert does not exist.
- Iterate through the dictionary using the items method and print the results (key and value).
- Iterate through the dictionary using the keys and print the sorted results (key and value).
Sample Execution Results:
e)_x000D_ apple dessert is: sauce_x000D_ f)_x000D_ A banana dessert exists._x000D_ g)_x000D_ A pear dessert does not exist._x000D_ h)_x000D_ strawberry shortcake_x000D_ apple sauce_x000D_ banana cream pie_x000D_ peach cobbler_x000D_ mango sticky rice_x000D_ i)_x000D_ apple sauce_x000D_ banana cream pie_x000D_ mango sticky rice_x000D_ peach cobbler_x000D_ strawberry shortcake_x000D_
Part 2 of 3 – Zipping Lists to make a Dictionary:
Create a list of states that contains the following states (in this order): Alabama, Alaska, Arizona, Arkansas, California
Create a list of capitals that contains the following capitals (in this order): Montgomery, Juneau, Phoenix, Little Rock, Sacramento
You now have a pair of ordered lists that correspond to each other – Montgomery is the capitol of Alabama, Juneau is the capital of Alaska, and so on.
Create a dictionary that combines these lists into a dictionary with state as the key and capitol as the value. Use the built-in-functions dict and zip – see: dict function and zip function . You should be able to do this with one line of code.
Iterate through the dictionary and print the sorted results (key and value).
Sample Execution Results:
Alabama Montgomery_x000D_ Alaska Juneau_x000D_ Arizona Phoenix_x000D_ Arkansas Little Rock_x000D_ California Sacramento_x000D_
Part 3 of 3 – Combining Dictionaries:
Create a new dictionary of state capitols and populate it with these key value pairs: California: Sac., Colorado: Denver, Connecticut: Hartford. Be sure that the California capitol is Sac. and not Sacramento.
Using the dictionary update method, (see: update method ) update the dictionary from Part 2 above with your newly created dictionary.
Iterate through the updated dictionary and print the sorted results (key and value). Note the updated value of California’s capitol.
Sample Execution Results:
Alabama Montgomery_x000D_ Alaska Juneau_x000D_ Arizona Phoenix_x000D_ Arkansas Little Rock_x000D_ California Sac._x000D_ Colorado Denver_x000D_ Connecticut Hartford_x000D_
Unit I
In-class assignment
Objects and Classes
This assignment consists of three parts.
Part 1 of 3 – A Basic Class – State Data:
Create a StateData class with the following methods: __init__, __str__, displayState.
Note: __ is two underline characters.
The __init__ method should have the parameters self, name, abbreviation, region, and population and should store the name, abbreviation, region, and population as attributes.
The __str__ method has the parameter self and should return the state’s name.
The displayState method has the parameter self and should print formatted state data as shown below.
Test the class by creating an instance of the class (instantiating) called s1 with the following data: “California”, “CA”, “West”, 39250000. Print your state object (this will call the __str__ method). Then call displayState. This test code should be after your class code – don’t worry about calling from main.
Sample Execution Results:
California_x000D_ California (CA) is in the West region and has population of 39250000_x000D_
Part 2 of 3 – Different ways of working with Attributes:
Here we explore different ways to work with Python attributes. Note that, while one of the approaches we are using is set/get, this approach is generally deprecated in favor of the simpler dot notation. Perhaps the only reason you might want to use set/get methods is when you need to run some extra code within the class whenever you are setting or getting an attribute. See the introduction to properties
Create a StateData2 class with the following methods: __init__, setRegion, getRegion.
The __init__ method should have the parameters self, name and should store the name as an attribute.
The setRegion should have the parameters self, region and should store the region as an attribute.
The getRegion should have the the parameter self and should return the the value of the region data variable.
Test the class by creating an instance of the class called s2 with the following data: “California”. Then call setRegion with the argument “West”. Then set the population attribute with the following line of code: s2.pop = 39250000
Then print four lines: s2.name, s2.getRegion(), s2.region, s2.pop
Again, this test code should be after your class code.
Sample Execution Results:
California_x000D_ West_x000D_ West_x000D_ 39250000_x000D_
Part 3 of 3 – Data Hiding:
Data hiding within Python is achieved with the use of special naming conventions: beginning an attribute name with either a single underscore (protected) or a double underscore (private). See:object oriented programming (scroll down to the Public- Protected- and Private Attributes section).
Create a StateData2 class with the following method: __init__.
The __init__ method should have the parameter self. It should store the value “Public” in an attribute called public, the value ” Protected” in an attribute called _protected (use a single underscore), and the value ” Private” in an attribute called __private (use a double underscore).
Test the class by creating an instance of the class called test.
Try to print three lines: test.public, test._protected, test.__private
Sample Execution Results:
PublicProtectedTraceback error
Unit J
In-class assignment
Chapter 9 contined
Chapter 10: Inheritance
Part 1 of 1 – Basic Inheritance – Circle & Cylinder:
You will be creating a Circle base (parent) class and a Cylinder class that inherits from it (child). Both classes and the code to test the classes will be contained within a single script.
The Circle class has the following methods: __init__, getArea.
Circle’s __init__ method should have the parameters self and radius, and should store the radius as an attribute.
The getArea method has the parameter self and should return the circle’s area (use the pi constant from the math module when calculating the area).
The Cylinder class inherits from the Circle class and has the following methods: __init__, getVolume.
Cylinder’s __init__ method should have the parameters self, radius and height. From within Cylinder’s __init__, call Circle’s __init__ to store the radius. The height should be stored as an attribute of the Cylinder.
The getVolume method has the parameter self and should return the cylinder’s volume. See: volume of a cylinder
Test by creating an instance of the Circle class with a radius of 4. Print the area of the circle, rounded to 2 places.
Then, create an instance of the Cylinder class with a radius of 2 and a height of 5. Print the volume of the cylinder, rounded to 2 places.
Sample Execution Results:
Circle area is: 50.27_x000D_ Cylinder volume is: 62.83
"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.
Discount Code: CIPD30
Click ORDER NOW..


