Skip to content

Introduction to Python

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

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Exponential ** of the number on the right can be used to power the number by the left. eg 4**2 = 16, 100 * 1.1 ** 7 = 198 (here 7 is used to power 1.1 7times then mul by 100)

print("I started with " + str(result) + ". Awesome!") I started with 194.87171000000012. Awesome!

sublist in list

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

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

# Print areas
print(areas)
# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
         "bedroom", 10.75, "bathroom", 10.50]

# Add poolhouse data to areas, new list is areas_1
areas_1 = areas + ["poolhouse", 24.5]

# Add garage data to areas_1, new list is areas_2
areas_2 = areas_1 + ["garage", 15.45]

The ; sign is used to place commands on the same line. The following two code chunks are equivalent:

Same line

command1; command2

Separate lines

command1 command2

The best way to assign a list to another variable -> areas_copy = areas[:] and not areas_copy = areas

As if you do it the first way any changes you make in the copy part reflect in the main list (areas)

Add your notes here

# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Create areas_copy
areas_copy = areas[:]

# Change areas_copy
areas_copy[0] = 5.0

# Print areas
print(areas)

To get help on the max() function documentation, for example, you can use one of these calls: help(max) ?max

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

sorted() -> key=None means that if you don't specify the key argument, it will be None. reverse=False means that if you don't specify the reverse argument, it will be False, by default.

# Create lists first and second
first = [11.25, 18.0, 20.0]
second = [10.75, 9.50]
# Paste together first and second: full
full = first + second
# Sort full in descending order: full_sorted
full_sorted = sorted(full, reverse= True)
print(full_sorted)

List Methods: Most list methods will change the list they're called on. Examples are:

append(), that adds an element to the list it is called on, remove(), that removes the first element of a list that matches the input, and reverse(), that reverses the order of the elements in the list it is called on.

# Definition of radius
r = 0.43

# Import the math package
import math

# Calculate C
C = 2*math.pi* r

# Calculate A
A = math.pi*r**2

# Build printout
print("Circumference: " + str(C))
print("Area: " + str(A))

Calculate the distance travelled by the Moon over 12 degrees of its orbit. Assign the result to dist. You can calculate this as r * pi, where r is the radius and pi is the angle in radians. To convert an angle in degrees to an angle in radians, use the radians() function, which you just imported.