Skip to content

Introduction to Python

Run the hidden code cell below to import the data used in this cours

Python Commands

Variables float - real numbers int - integer number str - string, text bool - True, False

Adapt list areas areas = ["hallway" , hall, "kitchen" , kit, "living room", liv, "bedroom" , bed, "bathroom", bath]

areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

Use slicing to create downstairs downstairs = areas[0:6] start is 0 (first character)

Use slicing to create upstairs upstairs = areas[6:10] 6 is 7th character

Create variables var1 and var2 var1 = [1, 2, 3, 4] var2 = True

Print out type of var1 print(type(var1))

Print out length of var1 print(len(var1))

Convert var2 to an integer: out2 out2 = int(var2)

Subsetting np_2d[0, 2] (first is row, second is column) (: => selects the entire row/col)

np_height_in = np_baseball[:,0] #created only first column print(np.mean(np_height_in)) #calculates the average of height print(np.median(np_height_in)) #calculates the median of height corr = np.corrcoef(np_baseball[: , 0] , np_baseball[: , 1]) #first and second columns)

gk_heights = np_heights[np_positions == 'GK'] #height of the first position = GK

other_heights = np_heights[np_positions != 'GK'] #other heights, not GK

**Matplotlib + Histogramms

plt.plot(X , Y) -> makes the line plot on X and Y axis plt.scatter(X , Y) -> makes dotted plot plt.xscale('log') -> logarithmic scale ?? plt.show() -> displayes the plot

help(plt.hist) plt.hist() -> makes the histogram

plt.xlabel('Year') plt.ylabel('Population') plt.title('World Population Projection') plt.yticks ([0, 2, 4, 6, 8, 10], -> adding extra labels on X or Y-axis plt.xticks ['0', '2B', '4B', '6B', '8B', '10B'])

#add more data year = [1950, 1951, 1952, ..., 2100] pop = [2.538, 2.57, 2.62, ..., 10.85]

year = [1800, 1850, 1900] + year pop = [1.0, 1.262, 1.650] + pop

# Create variables var1 and var2
var1 = [1, 2, 3, 4]
var2 = True

# Print out type of var1


# Print out length of var1


# Convert var2 to an integer: out2

1 hidden cell