# code style
print("Hello")
print(7 + 8)
print(7 + 8*2)
print(7/2 - 1)
print(1 + 2 - 3*4/5)
All best practices — https://www.python.org/dev/peps/pep-0008/
0.1 + 0.1 + 0.1 # 0.1 is an approximation to 1/10
# Quiz 1: variable names
medan_population = 2098000
# distinguish between
# 1. variable
# 2. assignment operator
# 3. value
z = 1
y = medan_population
print(y) # what is printed?
# We can assign variables such as
x = 1
y = 2
z = 3
# we can also assign variables in a compact way
x, y, z = 1, 2, 3
print(z)
# when assigning variables, use descriptive variable names
a, b = 2068000, 261
c = a/b
print(c)
medan_population, medan_area = 2068000, 261
medan_density = medan_population/medan_area
print(medan_density)
# Pythonic
my_height = 178
my_weight = 75
# Not Pythonic, but still works
myHeight = 178
myWeight = 75
medan_population = 2068000 # in the year 2015
#medan_population = medan_population + 1000 # verbose way of doing way
medan_population += 1000
print(medan_population)
# My electricity bills for the last three months have been $23, $32 and $64.
# What is the average monthly electricity bill over the three month period?
# Write an expression to calculate the mean, and use print() to view the result.
# Write an expression that calculates the average of 23, 32 and 64
# Step 1: calculate the mean
m1, m2, m3 = 23, 32, 64
avg = (m1+m2+m3)/3
# Step 2: use the print() function to display the result
print(avg)
carrots = 24
rabbits = 8
crs_per_rabbit = carrots/rabbits
rabbits = 12
crs_per_rabbit = carrots/rabbits
print(crs_per_rabbit)
print(1/0) # this will give you an exception /run-time error
print(1/0 # this will give you a syntax error
rio_population, rio_area = 6453682, 486.5
# Write code to compare the densities of these two cities
# Find out if the population of San Fransisco is denser than that of Rio de Janeiro
# (print either True or False)
# TODO: compute the densities of the two cities
san_francisco_population, sf_area = 864816, 231.89
san_francisco_pop_density = san_francisco_population / sf_area
print(san_francisco_pop_density)
rio_population, rio_area = 6453682, 486.5
rio_de_janairo_pop_density = rio_population / rio_area
print(rio_de_janairo_pop_density)
# TODO: write code that prints True if San Fransisco is denser than Rio de Janeiro, and False otherwise
if san_francisco_pop_density > rio_de_janairo_pop_density:
print("True")
else:
print("False")
print(True) # printing a Boolean
print("True") # printing a String
san_francisco_population, sf_area = 864816, 231.89
san_francisco_pop_density = san_francisco_population / sf_area
print(san_francisco_pop_density)
san_francisco_pop_density > rio_de_janairo_pop_density