-
Sven Graupner authoredSven Graupner authored
20 Pts)
Assignment B: Explore Python (This assignment demonstrates Python's basic data structures.
Challenges
- Challenge 1: Indexing Fruits
- Challenge 2: Packaging Fruits
- Challenge 3: Sorting Fruits
- Challenge 4: Income Analysis
- Challenge 5: Code Income Analysis
- Challenge 6: Explore Python built-in functions
1.) Challenge 1: Indexing Fruits
Explore Python. Review Python's basic data structures.
# Python is known for advanced list processing.
>>> fruits = ['apple', 'pear', 'orange', 'banana']
>>> print(fruits)
>>> fruits
['apple', 'pear', 'orange', 'banana']
>>> print(len(fruits))
4
>>> print(f"the third fruit is: {fruits[2]}")
the third fruit is: orange
>>> print(f"the second and third fruits are: {fruits[1:3]}")
the second and third fruits are: ['pear', 'orange']
>>> print(f"the last fruit is: {fruits[-1]}")
the last fruit is: banana
>>> print(f"the last two fruits are: {fruits[-2:]}")
the last two fruits are: ['orange', 'banana']
Perform examples on your laptop. (1 Pt)
2.) Challenge 2: Packaging Fruits
Review Python's built-in data structures. Perform examples and answer questions on a piece of paper.
-
What are data types for
fruits
,fruitbag
andfruitbox
called? (1 Pt) -
Name three properties that characterize each data type. (1 Pts)
-
Why does output for
fruitbag
differ from input? (1 Pt)>>> fruits = ['apple', 'pear', 'orange', 'banana'] >>> fruitbag = {'apple', 'pear', 'orange', 'banana'} >>> fruitbox = ('apple', 'pear', 'orange', 'banana') >>> print(fruits) ['apple', 'pear', 'orange', 'banana'] >>> print(fruitbox) ('apple', 'pear', 'orange', 'banana') >>> print(fruitbag) {'orange', 'banana', 'apple', 'pear'} >>> print(fruits[1]) pear >>> print(fruitbox[1]) pear >>> print(fruitbag[1]) TypeError: object is not subscriptable >>>
-
How is the structure for Eric called? (1 Pt)
eric = {"name": "Eric", "salary": 5000, "birthday": "Sep 25 2001"} >>> print(eric) {'name': 'Eric', 'salary': 5000, 'birthday': 'Sep 25 2001'} >>> print(eric["salary"]) 5000
3.) Challenge 3: Sorting Fruits
-
What is the difference between sort() and built-in function sorted(), link (2 Pts)?
>>> fruits = ['apple', 'pear', 'orange', 'banana'] >>> f1 = sorted(fruits) >>> print(f"{f1},\n{fruits}") ['apple', 'banana', 'orange', 'pear'], ['apple', 'pear', 'orange', 'banana'] >>> f2 = fruits.sort() >>> print(f"{f2},\n{fruits}") None, ['apple', 'banana', 'orange', 'pear']
-
Some people say that Arrays in other languages are Lists in Python. Other people argue that Tuples are Arrays.
- a) Which statement is (more) correct? (1 Pt)
- b) Name two differences between Arrays and Lists? (1 Pt)
-
Draw sketches to visualize Python data structures: List, Set, Tuple, Dictionary and Array (from other languages like C, C++). (1 Pt)
4.) Challenge 4: Income Analysis
The US tax Income Revenue Service (IRS) annually publishes income statistics by ZIP codes (reports).
For example, California ZIP Code 93636 is for Madera county, an agricultural region north of Fresno in the Central Valley. Income distribution for the tax year 2020 was:
income bracket: number of tax returns
filed in bracket
[$1 to under $25,000] 1,800
[$25,000 to under $50,000] 1,380
[$50,000 to under $75,000] 980
[$75,000 to under $100,000] 830
[$100,000 to under $200,000] 1,660
[$200,000 or more, up to $10M] 550
Numbers mean that 980 tax returns were filed in the bracket [$50,000 to under $75,000] taxable income, which is [50,000 .. 74,999].
A common statistical analysis is to compute:
-
the mean (average) income per tax filer and the
-
the median income per tax filer.
Assume $10 million ($10M) as upper limit for "or more" in the highest bracket.
For calculating the mean income, use the mean income within each bracket.
For calculating the median income, consider a linear rising income from the lower bound to the upper bound in each bracket.
Answer questions:
-
What is the difference between mean (average) and median calculations? (1 Pt)
- Why are both indicators relevant?
-
Calculate manually the average income for Madera county. (1 Pt)
-
Calculate manually the median income for Madera county. (1 Pt)
5.) Challenge 5: Code Income Analysis
Write Python code to perform this income analysis.
Use pure Python, no libraries such as Pandas or Numpy or library functions for mean and median.
Think about following steps:
-
Chose a suitable Python structure to represent tax data for a ZIP code. (1 Pt)
- Which data is relevant for the analysis?
- How can data be structured?
- Use only use Python structures: list, set, tuple, dictionary.
-
Code data for one ZIP code into your structure (no need to read
.xlsx
-files). (1 Pt) -
Define two functions
mean_income(...)
andmedian_income(...)
that take data for one ZIP code as input and return respective numbers. -
Define function
number_of_returns(...)
. -
Implement functions and demonstrate they return correct values.
-
Demonstrate analysis for other ZIP codes:
Results:
mean_income in Mountain View, CA is: 1,740,371 - median_income is: 114,820
mean_income in Palo Alto, CA is: 2,077,038 - median_income is: 153,658
mean_income in Atherton, CA is: 2,623,881 - median_income is: 354,087
mean_income in Redding, IA is: 33,333 - median_income is: 31,249
mean_income in New York City, NY U West is: 1,544,990 - median_income is: 104,774
(4 Pts)
6.) Challenge 6: Explore Python built-in functions
Learn about Python's built-in functions. Test the globals() function.
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_
importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module
'builtins' (built-in)>, 'fruits': ['apple', 'pear', 'orange', 'banana']}
Test the input() function.
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
exit()
(1 Pt)