x = 15
x = x + 5
print(x)20
TODO: Add the Spyder setup.
TODO: Use Anaconda and be prepared for questions.
TODO: Walk through spytertest.py from Canvas with students at the start of the Python session. Retain the original filename spelling; its repository path is not known.
TODO: Clarify the note “use os.chdir() for Spyder.”
TODO: Check whether pandas 3.0 is installed automatically.
TODO: check with PR: no explicit take-home/homework exercises in this session?
The existing notes also identify these materials, but do not establish an exact page mapping:
TODO: Confirm where these materials should be used. The original note referred to the second file as Versicherungsvertreter.xslx; the supplied inventory contains the Word document above instead.
Notes:
Prepare the Python environment before the session and plan to complete as many exercises as time permits.
TODO: include Spyder setup here.
Run these expressions individually in Spyder so that each result is visible.
3*5
16-5
16/4
16/5
16%5 # modulo/remainder after integer division (1)
16//5 # floor division: returns whole-number qotient (3)
3**2 # exponent
"Hello " + "World"Note: operators explained on the following slide.
Refer to a variable as names bound to an object
Python allows a variable to be reassigned to values of different types (dynamic typing), while multiple assignment sets several variables in one statement.
x = 4
x
x = 3.12
x
x = 'hello'
x
a, b = 1, 2
a
b
a + bExplain step by step how Python approaches this statement.
print()These examples contrast comma-separated output and string concatenation, demonstrate repetition, use str() for conversion, and use end to control the line ending.
print('hello')
print('hello', 'there')
print('hello' + ' ' + 'there')
print('haha' * 4)
print(5 * 4)
a = 54
print(a)
print('The number is ' + str(a) + '!')
print('The number is', a, '!')
print('hello ')
print('Peter')
print('hello', end=' ')
print('Peter')Predict the result of each block before running it.
x = 15
x = x + 5
print(x)20
Note: for the x = x + 5, the following slide provides an animated explanation.
x = 4
y = x + 1
x = 2
print(x, y)2 5
a = 4.5
b = 2
print(a // b)2.0
x = 17 / 2 % 2 * 3**3
print(x)13.5
x, y = 2, 6
x, y = y, x + 2
print(x, y)6 4
a, b = 2, 3
c, b = a, c + 1
print(a, b, c)The final block intentionally raises a NameError because c is read before it has been assigned.
Depending on available time, complete this exercise in class or assign it as homework.
In-class exercise
radius = 20
area = 3.14159 * radius**2
print ('The area of the circle with radius', radius, 'is:', area)In-class exercise
investment = 1000
interest_rate = 0.06
investment_period = 5
future_value = investment * (1 + interest_rate)**investment_period
print("The future value is:", future_value)☕ Break — 10 minutes
pandas DataFrames are widely used to load, inspect, clean, and prepare tabular data for machine learning. Models commonly receive DataFrames (or similar formats) depending on the library.
The short alias pd is the conventional name used for pandas (programmers are often lazy and prefer short names).
import pandas as pdTODO: get and link Bikesales.xlsx file
The final expression displays the DataFrame in an interactive environment.
data = pd.read_excel('Bikesales.xlsx')
dataThese expressions show the DataFrame dimensions, column names, and column data types.
data.shape
list(data)
data.dtypesThis path is machine-specific. The raw-string prefix r prevents Windows backslashes from being interpreted as escape sequences.
TODO: select a suitable path here:
import os
os.chdir(r'D:\Dropbox\Lehre\Introduction to Programming\Presentation')Note:
import os
print(os.getcwd())TODO: where is it in spyder?
The relative path is resolved against the active working directory.
data = pd.read_csv('Bikesales.csv')head() previews the first rows; index=False avoids writing the DataFrame index as an extra CSV column.
os.chdir(r'D:\Dropbox\Lehre\Introduction to Programming\Presentation')
exped = pd.read_excel('Exped.xlsx')
exped.head()
exped.to_csv('Exped.csv', index=False)TODO: think: should Transid be an incrementing index? in this case, it does not differ from the default/artificial index… It may be a more reasonable example if the new index differs from the one created by default.
After set_index(), Transid supplies the row labels.
exped1 = exped
exped1 = exped1.set_index('Transid')
exped1.head()In-class exercise ../materials/data/countries.csv
import pandas as pd
countries = pd.read_csv('../materials/data/countries.csv')
print(countries.shape)
print(countries.columns) # or print(list(countries))
countries.head()(20, 5)
Index(['Country', 'Population', 'Area', 'GDP', 'Continent'], dtype='str')
| Country | Population | Area | GDP | Continent | |
|---|---|---|---|---|---|
| 0 | China | 1398.72 | 9596.96 | 12234.78 | Asia |
| 1 | India | 1351.16 | 3287.26 | 2575.67 | Asia |
| 2 | US | 329.74 | 9833.52 | 19485.39 | N.America |
| 3 | Indonesia | 268.07 | 1910.93 | 1015.54 | Asia |
| 4 | Brazil | 210.32 | 8515.77 | 2055.51 | S.America |
In-class exercise ../materials/data/insurance.xlsx
import pandas as pd
insurance = pd.read_excel('../materials/data/insurance.xlsx')
print(insurance.shape)
print(insurance.head())
print(list(insurance))
# insurance.to_csv('insurance.csv', index=False)(500, 10)
Policy Expiry Location State Region InsuredValue Construction \
0 100242 2021-01-02 Urban NY East 1617630 Frame
1 100314 2021-01-02 Urban NY East 8678500 Fire Resist
2 100359 2021-01-02 Rural WI Midwest 2052660 Frame
3 100315 2021-01-03 Urban NY East 17580000 Frame
4 100385 2021-01-03 Urban NY East 1925000 Masonry
BusinessType Earthquake Flood
0 Retail N N
1 Apartment Y Y
2 Farming N N
3 Apartment Y Y
4 Hospitality N N
['Policy', 'Expiry', 'Location', 'State', 'Region', 'InsuredValue', 'Construction', 'BusinessType', 'Earthquake', 'Flood']
Take notes on improvements and common questions during the session and add them to feedback.qmd afterwards.