Skip to content

Commit 365887a

Browse files
authored
Add files via upload
1 parent a026ab7 commit 365887a

File tree

37 files changed

+451
-0
lines changed

37 files changed

+451
-0
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example, do not modify!
2+
print(5 / 8)
3+
print(7+10)
4+
# Put code below here

1_Python Basic_2_Any comments.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Just testing division
2+
print(5 / 8)
3+
4+
# Addition works too
5+
print(7 + 10)
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Addition and subtraction
2+
print(5 + 5)
3+
print(5 - 5)
4+
5+
# Multiplication and division
6+
print(3 * 5)
7+
print(10 / 2)
8+
9+
# Exponentiation
10+
print(4 ** 2)
11+
12+
# Modulo
13+
print(18 % 7)
14+
15+
# How much is your $100 worth after 7 years?
16+
print(100*(1.1**7))
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Create a variable savings
2+
savings = 100
3+
4+
# Print out savings
5+
print(savings)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create a variable savings
2+
savings = 100
3+
4+
# Create a variable factor
5+
factor=1.10
6+
7+
# Calculate result
8+
result=savings * factor ** 7
9+
10+
# Print out result
11+
print(result)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Create a variable desc
2+
desc="compound interest"
3+
4+
# Create a variable profitable
5+
profitable = True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Several variables to experiment with
2+
savings = 100
3+
factor = 1.1
4+
desc = "compound interest"
5+
6+
# Assign product of factor and savings to year1
7+
year1=savings*factor
8+
9+
# Print the type of year1
10+
print(type(year1))
11+
12+
# Assign sum of desc and desc to doubledesc
13+
doubledesc=desc+desc
14+
15+
# Print out doubledesc
16+
print(doubledesc)

1_Python Basic_8_Type conversion.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Definition of savings and result
2+
savings = 100
3+
result = 100 * 1.10 ** 7
4+
5+
# Fix the printout
6+
print("I started with $" + str(savings) + " and now have $" + str(result) + ". Awesome!")
7+
8+
# Definition of pi_string
9+
pi_string = "3.1415926"
10+
11+
# Convert pi_string into float: pi_float
12+
pi_float=float(pi_string)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create list areas
2+
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
3+
4+
# Create areas_copy
5+
areas_copy = [11.25, 18.0, 20.0, 10.75, 9.50]
6+
7+
# Change areas_copy
8+
areas_copy[0] = 5.0
9+
10+
# Print areas
11+
print(areas)

2_Python List_1_Create a list.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# area variables (in square meters)
2+
hall = 11.25
3+
kit = 18.0
4+
liv = 20.0
5+
bed = 10.75
6+
bath = 9.50
7+
8+
# Create list areas
9+
areas=[11.25,18.0,20.0,10.75,9.50]
10+
11+
# Print areas
12+
print(areas)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# area variables (in square meters)
2+
hall = 11.25
3+
kit = 18.0
4+
liv = 20.0
5+
bed = 10.75
6+
bath = 9.50
7+
8+
# Adapt list areas
9+
areas = ["hallway",hall,"kitchen", kit, "living room", liv,"bedroom", bed, "bathroom", bath]
10+
11+
# Print areas
12+
print(areas)

2_Python List_3_List of lists.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# area variables (in square meters)
2+
hall = 11.25
3+
kit = 18.0
4+
liv = 20.0
5+
bed = 10.75
6+
bath = 9.50
7+
8+
# house information as list of lists
9+
house = [["hallway", hall],
10+
["kitchen", kit],
11+
["living room", liv],["bedroom",bed],["bathroom",bath]]
12+
13+
# Print out house
14+
print(house)
15+
16+
# Print out the type of house
17+
print(type(house))

2_Python List_4_Subset and conquer.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create the areas list
2+
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
3+
4+
# Print out second element from areas
5+
print(areas[1])
6+
7+
# Print out last element from areas
8+
print(areas[-1])
9+
10+
# Print out the area of the living room
11+
print(areas[5])
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Create the areas list
2+
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
3+
4+
# Sum of kitchen and bedroom area: eat_sleep_area
5+
eat_sleep_area = areas[3]+areas[-3]
6+
7+
# Print the variable eat_sleep_area
8+
print(eat_sleep_area)

2_Python List_6_Slicing and dicing.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Create the areas list
2+
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
3+
4+
# Use slicing to create downstairs
5+
downstairs =areas[:6]
6+
7+
# Use slicing to create upstairs
8+
upstairs=areas[6:]
9+
10+
# Print out downstairs and upstairs
11+
print(downstairs)
12+
print(upstairs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Create the areas list
2+
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
3+
4+
# Alternative slicing to create downstairs
5+
downstairs=areas[:6]
6+
7+
# Alternative slicing to create upstairs
8+
upstairs=areas[6:]
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Create the areas list
2+
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
3+
4+
# Correct the bathroom area
5+
areas[9] =10.50
6+
7+
# Change "living room" to "chill zone"
8+
areas[4]="chill zone"

2_Python List_9_Extend a list.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Create the areas list and make some changes
2+
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
3+
"bedroom", 10.75, "bathroom", 10.50]
4+
5+
# Add poolhouse data to areas, new list is areas_1
6+
areas_1 = areas + ["poolhouse", 24.5]
7+
8+
# Add garage data to areas_1, new list is areas_2
9+
areas_2 = areas_1 + ["garage",15.45]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Create variables var1 and var2
2+
var1 = [1, 2, 3, 4]
3+
var2 = True
4+
5+
# Print out type of var1
6+
print(type(var1))
7+
8+
# Print out length of var1
9+
print(len(var1))
10+
11+
# Convert var2 to an integer: out2
12+
out2=int(var2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Create lists first and second
2+
first = [11.25, 18.0, 20.0]
3+
second = [10.75, 9.50]
4+
5+
# Paste together first and second: full
6+
full = first+second
7+
8+
# Sort full in descending order: full_sorted
9+
full_sorted = sorted(full,reverse=True)
10+
11+
# Print out full_sorted
12+
print(full_sorted)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# string to experiment with: room
2+
room = "poolhouse"
3+
4+
# Use upper() on room: room_up
5+
room_up=room.upper()
6+
7+
# Print out room and room_up
8+
print(room)
9+
print(room_up)
10+
# Print out the number of o's in room
11+
print(room.count('o'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Create list areas
2+
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
3+
4+
# Print out the index of the element 20.0
5+
print(areas.index(20.0))
6+
7+
# Print out how often 14.5 appears in areas
8+
print(areas.count(14.5))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Create list areas
2+
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
3+
4+
# Use append twice to add poolhouse and garage size
5+
areas.append(24.5)
6+
areas.append(15.45)
7+
8+
# Print out areas
9+
print(areas)
10+
11+
# Reverse the orders of the elements in areas
12+
areas.reverse()
13+
14+
# Print out areas
15+
print(areas)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition of radius
2+
r = 0.43
3+
# Import the math package
4+
import math
5+
6+
# Calculate C
7+
C = 2*math.pi*r
8+
9+
# Calculate A
10+
A = math.pi*r*r
11+
12+
# Build printout
13+
print("Circumference: " + str(C))
14+
print("Area: " + str(A))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Definition of radius
2+
r = 192500
3+
4+
# Import radians function of math package
5+
from math import radians
6+
7+
# Travel distance of Moon over 12 degrees. Store in dist.
8+
phi=radians(12)
9+
dist=r*phi
10+
11+
# Print out dist
12+
print(dist)

4_NumPy_10_Average versus median.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# np_baseball is available
2+
3+
# Import numpy
4+
import numpy as np
5+
6+
# Create np_height from np_baseball
7+
np_height=np_baseball[:,0]
8+
9+
# Print out the mean of np_height
10+
print(np.mean(np_height))
11+
12+
# Print out the median of np_height
13+
print(np.median(np_height))
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# np_baseball is available
2+
3+
# Import numpy
4+
import numpy as np
5+
6+
# Print mean height (first column)
7+
avg = np.mean(np_baseball[:,0])
8+
print("Average: " + str(avg))
9+
10+
# Print median height. Replace 'None'
11+
med = np.median(np_baseball[:,0])
12+
print("Median: " + str(med))
13+
14+
# Print out the standard deviation on height. Replace 'None'
15+
stddev = np.std(np_baseball[:,0])
16+
print("Standard Deviation: " + str(stddev))
17+
18+
# Print out correlation between first and second column. Replace 'None'
19+
corr = np.corrcoef(np_baseball[:,0], np_baseball[:,1])
20+
print("Correlation: " + str(corr))

4_NumPy_12_Blend it all together.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# heights and positions are available as lists
2+
3+
# Import numpy
4+
import numpy as np
5+
6+
# Convert positions and heights to numpy arrays: np_positions, np_heights
7+
np_positions=np.array(positions)
8+
np_heights=np.array(heights)
9+
10+
11+
# Heights of the goalkeepers: gk_heights
12+
gk_heights = np_heights[np_positions=='GK']
13+
14+
# Heights of the other players: other_heights
15+
np_positions !='GK'
16+
other_heights=np_heights[np_positions !='GK']
17+
18+
# Print out the median height of goalkeepers. Replace 'None'
19+
print("Median height of goalkeepers: " + str(np.median(gk_heights)))
20+
21+
# Print out the median height of other players. Replace 'None'
22+
print("Median height of other players: " + str(np.median(other_heights)))

4_NumPy_1_First NumPy Array.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create list baseball
2+
baseball = [180, 215, 210, 210, 188, 176, 209, 200]
3+
4+
# Import the numpy package as np
5+
import numpy as np
6+
7+
# Create a numpy array from baseball: np_baseball
8+
np_baseball=np.array(baseball)
9+
10+
# Print out type of np_baseball
11+
print(type(np_baseball))

0 commit comments

Comments
 (0)