1
2
3
4
interest= 1+0.6*2 interest2= 1+0.9*4 interest3= 1+2.1*3
def interest(rate, years): return 1 + rate * years
✂
5
✂
round(number[, ndigits])
None
round
n
n
6
square(n) n
def sum_squares(x, y): “””
>>> sum_squares(3, 9)
90
“””
return square(x) + square(y)
7
8
def square(x): return pow(x, 2)
def square(x): return x ** 2
from operator import mul def square(x):
return mul(x, x) square = lambda x: x * x
8
from operator import mul
def square(x):
return mul(x, x-1) + x
9
10
true_false
d
helper
my_int
rolled_one
dice
take_turn
num_rolls
print triple
abs
11
def summation(n, f):
“””Sums the result of applying the function F to each term in the sequence from 1 to N.
N can be any integer > 1, F must take a single integer argument and return a number.
“””
total = 0
k=1
while k <= n:
total = total + f(k)
k=k+1 return total
12
if sqrt(square(a) + square(b)) > 1:
x = x + sqrt(square(a) + square(b))
⟱
hypotenuse = sqrt(square(a) + square(b)) if hypotenuse > 1:
x = x + hypotenuse
13
if sqrt(square(a) + square(b)) > 1:
x = x + sqrt(square(a) + square(b))
⟱
hypotenuse = sqrt(square(a) + square(b)) if hypotenuse > 1:
x = x + hypotenuse
x1 = (-b + sqrt(square(b) – 4 * a * c)) / (2 * a)
⟱
discriminant = square(b) – 4 * a * c
x1 = (-b + sqrt(discriminant)) / (2 * a)
13
nki xyz fgh
14
nki xyz fgh
average_age = average(age, students)
# Compute average age of students
aa = avg(a, st)
14
15
16
17
# Sum up the numbers from 1 to 10
sum = 0 x=1
while x < 10:
sum += x x += 1
18
19
if x > 5 x += 1
sum = 0 x=0
while x < 10:
sum + = x x+=1
20
if x > 5 # Missing colon x += 1
sum = 0 x=0
while x < 10:
sum + = x x+=1
20
if x > 5 # Missing colon x += 1
sum = 0 x=0
while x < 10:
sum + = x # No space needed between + and = x+=1
20
SyntaxError
print("just testing here"))
title = 'Hello, ' + name ', how are you?'
21
IndentationError TabError
cat -A filename.py
def sum(a, b): total = a + b
return total
22
23
def div_numbers(dividend, divisor): return dividend/divisor
quot1 = div_numbers(10, 2)
quot2 = div_numbers(10, 1)
quot3 = div_numbers(10, 0)
quot4 = div_numbers(10, -1)
24
def div_numbers(dividend, divisor): return dividend/divisor
quot1 = div_numbers(10, 2)
quot2 = div_numbers(10, 1)
quot3 = div_numbers(10, 0) # Cannot divide by 0!
quot4 = div_numbers(10, -1)
24
TypeError:'X' object is not callable
sum = 2 + 2
sum(3, 5)
25
...NoneType...
def sum(a, b): print(a + b)
total = sum( sum(30, 45), sum(10, 15) )
26
NameError
fav_nut = 'pistachio'
best_chip = 'chocolate'
trail_mix = Fav_Nut + best__chip
27
UnboundLocalError
sum = 0
def sum_nums(x, y): sum += x + y
return sum sum_nums(4, 5)
28
29
def div_numbers(dividend, divisor): return dividend/divisor
quot1 = div_numbers(10, 2)
quot2 = div_numbers(10, 1)
quot3 = div_numbers(10, 0)
quot4 = div_numbers(10, -1)
Traceback (most recent call last):
File "main.py", line 14, in
quot3 = div_numbers(10, 0)
File “main.py”, line 10, in div_numbers
return dividend/divisor
ZeroDivisionError: division by zero
30
Traceback (most recent call last):
File “main.py”, line 14, in
quot3 = div_numbers(10, 0)
File “main.py”, line 10, in div_numbers
return dividend/divisor
ZeroDivisionError: division by zero
31
Traceback (most recent call last):
File “main.py”, line 14, in
quot3 = div_numbers(10, 0)
File “main.py”, line 10, in div_numbers
return dividend/divisor
ZeroDivisionError: division by zero
32
def f(x):
return g(x – 1)
def g(y):
return abs(h(y) – h(1 /& y)
def h(z): z*z
print(f(12))
33