# code style
print("Hello")
print(7 + 8)
print(7 + 8*2)
print(7/2 - 1)
print(1 + 2 - 3*4/5)
Hello
15
23
2.5
0.6000000000000001
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?
2098000
# 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)
3
7923.371647509579
medan_population, medan_area = 2068000, 261
medan_density = medan_population/medan_area
print(medan_density)
7923.371647509579
# 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)
2069000
# 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)
39.666666666666664
carrots = 24
rabbits = 8
crs_per_rabbit = carrots/rabbits
rabbits = 12
crs_per_rabbit = carrots/rabbits
print(crs_per_rabbit)
2.0
print(1/0) # this will give you an exception /run-time error
ZeroDivisionError: division by zero
print(1/0 # this will give you a syntax error
SyntaxError: unexpected EOF while parsing (<ipython-input-24-0de0e8c4e15c>, line 1)
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")
3729.423433524516
13265.533401849949
False
print(True) # printing a Boolean
print("True") # printing a String
True
True
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
3729.423433524516
NameError: name 'rio_de_janairo_pop_density' is not defined