0% found this document useful (0 votes)
19 views

Introduction To Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Introduction To Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

# Multiplication, division, modulo, and exponentiation

INTRODUCTION TO PYTHON
print(3 * 5)
# Example, do not modify!
print(10 / 2)
print(5 / 8)
print(18 % 7)
# Put code below here
print(4 ** 2)
print(7 + 10)
# Calculate two to the power of five
When to use Python? print(2 ** 5)
Python is a pretty versatile language. For which applications can you use
Python? Variable Assignment

 Create a variable savings with the value 100.


You want to do some quick calculations.  Check out this variable by typing print(savings) in the script.

For your new business, you want to develop a database-driven website. # Create a variable savings
savings = 100
Your boss asks you to clean and analyze the results of the latest
# Print out savings
satisfaction survey.
print(savings)
All of the above.
Calculations with variables
Any comments?
 Create a variable growth_multiplier, equal to 1.1.
 Change the values of the numbers shown to see how Python performs  Create a variable, result, equal to the amount of money you saved
addition and subtraction. after 7 years.
 Change the values of the numbers shown to see how multiplication,  Print out the value of result.
division, modulo, and exponentiation works in Python.
 Calculate and print 2 to the power of 5. # Create a variable savings
savings = 100
# Addition, subtraction
# Create a variable growth_multiplier
print(5 + 5)
growth_multiplier = 1.1
print(5 - 5)
# Calculate result
result = savings * growth_multiplier ** 7  What do you think the resulting type will be? Find out by printing out
the type of year1.
# Print out result  Calculate the sum of desc and desc and store the result in a new
print(result) variable doubledesc.
 Print out doubledesc. Did you expect this?
Other variable types
savings = 100
In the previous exercise, you worked with two Python data types:
growth_multiplier = 1.1
 int, or integer: a number without a fractional part. savings, with the desc = "compound interest"
value 100, is an example of an integer.
 float, or floating point: a number that has both an integer and # Assign product of savings and growth_multiplier to year1
fractional part, separated by a point. growth_multiplier, with the year1 = savings * growth_multiplier
value 1.1, is an example of a float.
# Print the type of year1
Next to numerical data types, there are two other very common data types: print(type(year1))
# Assign sum of desc and desc to doubledesc
 str, or string: a type to represent text. You can use single or double
quotes to build a string. doubledesc = desc + desc
 bool, or boolean: a type to represent logical values. Can only # Print out doubledesc
be True or False (the capitalization is important!).
print(doubledesc)
 Create a new string, desc, with the value "compound interest".
 Create a new boolean, profitable, with the value True. Type conversion

# Create a variable desc  Hit Run Code to run the code. Try to understand the error message.
 Fix the code such that the printout runs without errors; use the
desc = "compound interest" function str() to convert the variables to strings.
# Create a variable profitable  Convert the variable pi_string to a float and store this float as a new
variable, pi_float.
profitable = True
# Definition of savings and result
Operations with other types
savings = 100
 Calculate the product of savings and growth_multiplier. Store the result = 100 * 1.10 ** 7
result in year1.
# Fix the printout
print("I started with $" + str(savings) + " and now have $" + str(result) + ". A kit = 18.0
wesome!") liv = 20.0
# Definition of pi_string bed = 10.75
pi_string = "3.1415926" bath = 9.50
# Convert pi_string into float: pi_float # Create list areas
pi_float = float(pi_string) areas = [hall, kit, liv, bed, bath]

Can Python handle everything? # Print areas

Now that you know something more about combining different sources of print(areas)
information, have a look at the four Python expressions below. Which one of
these will throw an error? You can always copy and paste this code in the Create list with different types
IPython Shell to find out!
 Finish the code that creates the areas list. Build the list so that the list
first contains the name of each room as a string and then its area. In
"I can add integers, like " + str(5) + " to strings." other words, add the strings "hallway", "kitchen" and "bedroom" at
the appropriate locations.
"I said " + ("Hey " * 2) + "Hey!"  Print areas again; is the printout more informative this time?

"The correct answer to this multiple choice exercise is answer number " + # area variables (in square meters)
2 hall = 11.25
kit = 18.0
True + False
liv = 20.0
Create a list bed = 10.75

 Create a list, areas, that contains the area of the hallway (hall), kitchen bath = 9.50
(kit), living room (liv), bedroom (bed) and bathroom (bath), in this # Adapt list areas
order. Use the predefined variables.
areas = ["hallway", hall, "kitchen", kit, "living room", liv, "bedroom", bed, "b
 Print areas with the print() function.
athroom", bath]
# Print areas
# Area variables (in square meters)
print(areas)
hall = 11.25
Select the valid list ["kitchen", kit],
my_list = [el1, el2, el3] ["living room", liv],

Can you tell which ones of the following lines of Python code are valid ways ["bedroom", bed],
to build a list? ["bathroom", bath]]
A. [1, 3, 4, 2] B. [[1, 2, 3], [4, 5, 7]] C. [1 + 2, "a" * 5, 3] # Print out house
print(house)
A, B and C
# Print out the type of house
print(type(house))
B
Subset and conquer
B and C
 Print out the second element from the areas list (it has the
C value 11.25).
 Subset and print out the last element of areas, being 9.50. Using a
List of lists negative index makes sense here!
 Select the number representing the area of the living room (20.0) and
 Finish the list of lists so that it also contains the bedroom and print it out.
bathroom data. Make sure you enter these in order!
 Print out house; does this way of structuring your data make more # Create the areas list
sense?
 Print out the type of house. Are you still dealing with a list? areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 1
0.75, "bathroom", 9.50]

# area variables (in square meters) # Print out second element from areas

hall = 11.25 print(areas[1])

kit = 18.0 # Print out last element from areas

liv = 20.0 print(areas[-1])

bed = 10.75 # Print out the area of the living room

bath = 9.50 print(areas[5])

# house information as list of lists


Subset and calculate
house = [["hallway", hall],
 Using a combination of list subsetting and variable assignment, create print(upstairs)
a new variable, eat_sleep_area, that contains the sum of the area of
the kitchen and the area of the bedroom.
 Print the new variable eat_sleep_area. Slicing and dicing (2)

 Create downstairs again, as the first 6 elements of areas. This time,


# Create the areas list simplify the slicing by omitting the begin index.
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 1  Create upstairs again, as the last 4 elements of areas. This time,
0.75, "bathroom", 9.50] simplify the slicing by omitting the end index.
# Sum of kitchen and bedroom area: eat_sleep_area
# Create the areas list
eat_sleep_area = areas[3] + areas[-3]
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 1
# Print the variable eat_sleep_area 0.75, "bathroom", 9.50]
print(eat_sleep_area) # Alternative slicing to create downstairs
downstairs = areas[:6]
Slicing and dicing
# Alternative slicing to create upstairs
 Use slicing to create a list, downstairs, that contains the first 6 upstairs = areas[6:]
elements of areas.
 Do a similar thing to create a new variable, upstairs, that contains the
last 4 elements of areas. Subsetting lists of lists
 Print both downstairs and upstairs using print(). Try out the commands in the following code sample in the IPython Shell:

x = [["a", "b", "c"],


# Create the areas list
["d", "e", "f"],
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 1
0.75, "bathroom", 9.50] ["g", "h", "i"]]
# Use slicing to create downstairs x[2][0]
downstairs = areas[0:6] x[2][:2]
# Use slicing to create upstairs x[2] results in a list, that you can subset again by adding additional square
upstairs = areas[6:10] brackets.
# Print out downstairs and upstairs What will house[-1][1] return? house, the list of lists that you created before,
is already defined for you in the workspace. You can experiment with it in
print(downstairs)
the IPython Shell.
A float: the kitchen area "bedroom", 10.75, "bathroom", 10.50]
# Add poolhouse data to areas, new list is areas_1
A string: "kitchen" areas_1 = areas + ["poolhouse", 24.5]
# Add garage data to areas_1, new list is areas_2
A float: the bathroom area
areas_2 = areas_1 + ["garage", 15.45]
A string: "bathroom"
Delete list elements
Replace list elements areas = ["hallway", 11.25, "kitchen", 18.0,
 Update the area of the bathroom area to be 10.50 square meters "chill zone", 20.0, "bedroom", 10.75,
instead of 9.50. "bathroom", 10.50, "poolhouse", 24.5,
 Make the areas list more trendy! Change "living room" to "chill zone".
"garage", 15.45]
# Create the areas list Which of the code chunks will do the job for us?
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 1
0.75, "bathroom", 9.50] del(areas[10]); del(areas[11])
# Correct the bathroom area
areas[-1] = 10.50 del(areas[10:11])
# Change "living room" to "chill zone"
del(areas[-4:-2])
areas[4] = "chill zone"

del(areas[-3]); del(areas[-4])
Extend a list
Inner workings of lists
 Use the + operator to paste the list ["poolhouse", 24.5] to the end of
the areas list. Store the resulting list as areas_1. # Create list areas
 Further extend areas_1 by adding data on your garage. Add the
string "garage" and float 15.45. Name the resulting list areas_2. areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Create areas_copy
# Create the areas list (updated version) areas_copy = list(areas)
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0, # Change areas_copy
areas_copy[0] = 5.0 pow() takes three arguments: base, exp, and None. All of these arguments
# Print areas are required.

print(areas)
pow() takes three arguments: base, exp, and mod. base and exp are
required arguments, mod is an optional argument.
Familiar functions

 Use print() in combination with type() to print out the type of var1. pow() takes two arguments: exp and mod. If you don’t specify exp, the
 Use len() to get the length of the list var1. Wrap it in a print() call to function will return an error.
directly print it out.
 Use int() to convert var2 to an integer. Store the output as out2. Multiple arguments

 Use + to merge the contents of first and second into a new list: full.
# Create variables var1 and var2
 Call sorted() on full and specify the reverse argument to be True. Save
var1 = [1, 2, 3, 4] the sorted list as full_sorted.
var2 = True  Finish off by printing out full_sorted.

# Print out type of var1


# Create lists first and second
print(type(var1))
first = [11.25, 18.0, 20.0]
# Print out length of var1
second = [10.75, 9.50]
print(len(var1))
# Paste together first and second: full
# Convert var2 to an integer: out2
full = first + second
out2 = int(var2)
# Sort full in descending order: full_sorted
Help! full_sorted = sorted(full, reverse=True)
# Print out full_sorted
help(max)
print(full_sorted)
?max

Use the IPython Shell to open up the documentation on pow(). Which of the String Methods
following statements is true?
 Use the upper() method on place and store the result in place_up. Use
the syntax for calling methods that you learned in the previous video.
pow() takes three arguments: base, exp, and mod. If you don’t  Print out place and place_up. Did both change?
specify mod, the function will return an error.
 Print out the number of o’s on the variable place by  Use append() twice to add the size of the poolhouse and the garage
calling count() on place and passing the letter 'o' as an input to the again: 24.5 and 15.45, respectively. Make sure to add them in this
method. We’re talking about the variable place, not the word "place"! order.
 Print out areas
# string to experiment with: place  Use the reverse() method to reverse the order of the elements in areas.
 Print out areas once more.
place = "poolhouse"
# Use upper() on place: place_up # Create list areas
place_up = place.upper() areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Print out place and place_up # Use append twice to add poolhouse and garage size
print(place) areas.append(24.5)
print(place_up) areas.append(15.45)
# Print out the number of o's in place # Print out areas
print(place.count('o')) print(areas)
# Reverse the orders of the elements in areas
List Methods
areas.reverse()
 Use the index() method to get the index of the element in areas that is # Print out areas
equal to 20.0. Print out this index.
 Call count() on areas to find out how many times 9.50 appears in the print(areas)
list. Again, simply print out this number.
Import package
# Create list areas script. Fill in the code to calculate C and A and see how the print() functions
create some nice printouts.
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Print out the index of the element 20.0  Import the math package. Now you can access the
print(areas.index(20.0)) constant pi with math.pi.
 Calculate the circumference of the circle and store it in C.
# Print out how often 9.50 appears in areas  Calculate the area of the circle and store it in A.
print(areas.count(9.50))
# Definition of radius
List Methods (2) r = 0.43
# Import the math packageimport math
# Calculate C  Import the numpy package as np, so that you can refer
to numpy with np.
C = 2 * r * math.pi  Use np.array() to create a numpy array from baseball. Name this
# Calculate A array np_baseball.
 Print out the type of np_baseball to check that you got it right.
A = math.pi * r ** 2
# Build printout # Create list baseball
print("Circumference: " + str(C)) baseball = [180, 215, 210, 210, 188, 176, 209, 200]
print("Area: " + str(A)) # Import the numpy package as npimport numpy as np
# Create a NumPy array from baseball: np_baseball
Selective import
np_baseball = np.array(baseball)
 Perform a selective import from the math package where you only # Print out type of np_baseball
import the radians function.
 Calculate the distance travelled by the Moon over 12 degrees of its print(type(np_baseball))
orbit. Assign the result to dist. You can calculate this as \r * phi,
where r is the radius and phi is the angle in radians. To convert an Baseball players’ height
angle in degrees to an angle in radians, use the radians() function,
which you just imported.  Create a numpy array from height_in. Name this new
 Print out dist. array np_height_in.
 Print np_height_in.
# Definition of radius  Multiply np_height_in with 0.0254 to convert all height
measurements from inches to meters. Store the new values in a new
r = 192500 array, np_height_m.
# Import radians function of math packagefrom math import radians  Print out np_height_m and check if the output makes sense.
# Travel distance of Moon over 12 degrees. Store in dist.
# edited/addedimport pandas as pd
dist = r * radians(12)
mlb = pd.read_csv('baseball.csv')
# Print out dist
# height_in is available as a regular list
print(dist)
height_in = mlb['Height'].tolist()
Different ways of importing # height_in is available as a regular list
# Import numpyimport numpy as np
my_inv([[1,2], [3,4]])
# Create a numpy array from height_in: np_height_in
np_height_in = np.array(height_in) print(bmi)
# Print out np_height_in
Lightweight baseball players
print(np_height_in)
# Convert np_height_in to m: np_height_m  Create a boolean numpy array: the element of the array should
be True if the corresponding baseball player’s BMI is below 21. You
np_height_m = np_height_in * 0.0254
can use the < operator for this. Name the array light.
# Print np_height_m  Print the array light.
print(np_height_m)  Print out a numpy array with the BMIs of all baseball players whose
BMI is below 21. Use light inside square brackets to do a selection on
the bmi array.
Baseball player’s BMI

 Create a numpy array from the weight_lb list with the correct units. # height_in and weight_lb are available as a regular lists
Multiply by 0.453592 to go from pounds to kilograms. Store the # Import numpyimport numpy as np
resulting numpy array as np_weight_kg.
 Use np_height_m and np_weight_kg to calculate the BMI of each # Calculate the BMI: bmi
player. Use the following equation: $ = $ Save the np_height_m = np.array(height_in) * 0.0254
resulting numpy array as bmi.
np_weight_kg = np.array(weight_lb) * 0.453592
 Print out bmi.
bmi = np_weight_kg / np_height_m ** 2
# edited/added # Create the light array
weight_lb = mlb['Weight'].tolist() light = bmi < 21
# height_in and weight_lb are available as regular lists # Print out light
# Import numpyimport numpy as np print(light)
# Create array from height_in with metric units: np_height_m # Print out BMIs of all baseball players whose BMI is below 21
np_height_m = np.array(height_in) * 0.0254 print(bmi[light])
# Create array from weight_lb with metric units: np_weight_kg
NumPy Side Effects
np_weight_kg = np.array(weight_lb) * 0.453592
# Calculate the BMI: bmi np.array([True, 1, 2]) + np.array([3, 4, False])
bmi = np_weight_kg / np_height_m ** 2
# Print out bmi np.array([True, 1, 2, 3, 4, False])
np.array([4, 3, 0]) + np.array([0, 2, 2]) baseball = [[180, 78.4],
[215, 102.7],
np.array([1, 1, 2]) + np.array([3, 4, -1]) [210, 98.5],
[188, 75.2]]
np.array([0, 1, 2, 3, 4, 5])
# Import numpyimport numpy as np
Subsetting NumPy Arrays # Create a 2D numpy array from baseball: np_baseball

 Subset np_weight_lb by printing out the element at index 50. np_baseball = np.array(baseball)
 Print out a sub-array of np_height_in that contains the elements at # Print out the type of np_baseball
index 100 up to and including index 110.
print(type(np_baseball))
# Print out the shape of np_baseball
# height_in and weight_lb are available as a regular lists
print(np_baseball.shape)
# Import numpyimport numpy as np
# Store weight and height lists as numpy arrays Baseball data in 2D form
np_weight_lb = np.array(weight_lb)
 Use np.array() to create a 2D numpy array from baseball. Name
np_height_in = np.array(height_in)
it np_baseball.
# Print out the weight at index 50  Print out the shape attribute of np_baseball.
print(np_weight_lb[50])
# Print out sub-array of np_height_in: index 100 up to and including index 1 # baseball is available as a regular list of lists
10 # Import numpy packageimport numpy as np
print(np_height_in[100:111]) # Create a 2D numpy array from baseball: np_baseball
np_baseball = np.array(baseball)
Your First 2D NumPy Array
# Print out the shape of np_baseball
 Use np.array() to create a 2D numpy array from baseball. Name print(np_baseball.shape)
it np_baseball.
 Print out the type of np_baseball. Subsetting 2D NumPy Arrays
 Print out the shape attribute of np_baseball. Use np_baseball.shape.
 Print out the 50th row of np_baseball.
# Create baseball, a list of lists  Make a new variable, np_weight_lb, containing the entire second
column of np_baseball.
 Select the height (first column) of the 124th baseball player updated = np.array(pd.read_csv('update.csv', header = None))
in np_baseball and print it out.
# baseball is available as a regular list of lists# updated is available as 2D n
umpy array
# edited/added
# Import numpy packageimport numpy as np
baseball = pd.read_csv('baseball.csv')[['Height', 'Weight']]
# Create np_baseball (3 cols)
# baseball is available as a regular list of lists
np_baseball = np.array(baseball)
# Import numpy packageimport numpy as np
# Print out addition of np_baseball and updated
# Create np_baseball (2 cols)
print(np_baseball + updated)
np_baseball = np.array(baseball)
# Create numpy array: conversion
# Print out the 50th row of np_baseball
conversion = np.array([0.0254, 0.453592, 1])
print(np_baseball[49,:])
# Print out product of np_baseball and conversion
# Select the entire second column of np_baseball: np_weight_lb
print(np_baseball * conversion)
np_weight_lb = np_baseball[:,1]
# Print out height of 124th player Average versus median
print(np_baseball[123, 0])
 Create numpy array np_height_in that is equal to first column
2D Arithmetic of np_baseball.
 Print out the mean of np_height_in.
 Print out the median of np_height_in.
 You managed to get hold of the changes in height, weight and age of
all baseball players. It is available as a 2D numpy array, updated.
Add np_baseball and updated and print out the result. # np_baseball is available
 You want to convert the units of height and weight to metric (meters # Import numpyimport numpy as np
and kilograms, respectively). As a first step, create a numpy array
with three values: 0.0254, 0.453592 and 1. Name this # Create np_height_in from np_baseball
array conversion. np_height_in = np_baseball[:,0]
 Multiply np_baseball with conversion and print out the result.
# Print out the mean of np_height_in
# edited/added print(np.mean(np_height_in))
baseball = pd.read_csv('baseball.csv')[['Height', 'Weight', 'Age']] # Print out the median of np_height_in
n = len(baseball) print(np.median(np_height_in))
Explore the baseball data  Extract all the heights of the goalkeepers. You can use a little trick
here: use np_positions == 'GK' as an index for np_heights. Assign the
 The code to print out the mean height is already included. Complete result to gk_heights.
the code for the median height. Replace None with the correct code.  Extract all the heights of all the other players. This time
 Use np.std() on the first column of np_baseball to calculate stddev. use np_positions != 'GK' as an index for np_heights. Assign the result
Replace None with the correct code. to other_heights.
 Do big players tend to be heavier? Use np.corrcoef() to store the  Print out the median height of the goalkeepers using np.median().
correlation between the first and second column Replace None with the correct code.
of np_baseball in corr. Replace None with the correct code.  Do the same for the other players. Print out their median height.
Replace None with the correct code.
# np_baseball is available
# edited/added
# Import numpyimport numpy as np
fifa = pd.read_csv('fifa.csv', skipinitialspace=True, usecols=['position', 'heig
# Print mean height (first column)
ht'])
avg = np.mean(np_baseball[:,0])
positions = list(fifa.position)
print("Average: " + str(avg))
heights = list(fifa.height)
# Print median height. Replace 'None'
# heights and positions are available as lists
med = np.median(np_baseball[:,0])
# Import numpyimport numpy as np
print("Median: " + str(med))
# Convert positions and heights to numpy arrays: np_positions, np_heights
# Print out the standard deviation on height. Replace 'None'
np_positions = np.array(positions)
stddev = np.std(np_baseball[:,0])
np_heights = np.array(heights)
print("Standard Deviation: " + str(stddev))
# Heights of the goalkeepers: gk_heights
# Print out correlation between first and second column. Replace 'None'
gk_heights = np_heights[np_positions == 'GK']
corr = np.corrcoef(np_baseball[:,0], np_baseball[:,1])
# Heights of the other players: other_heights
print("Correlation: " + str(corr))
other_heights = np_heights[np_positions != 'GK']
Blend it all together # Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))
 Convert heights and positions, which are regular lists, to numpy
arrays. Call them np_heights and np_positions. # Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))

You might also like