# SHAD UBC 2018: Answers to Python Exercises

## Lesson 1: Python as a Calculator

``````Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
>>> 17 + 89
106
>>> 100 - 23
77
>>> 12 * 45
540
>>> 100 / 4
25.0``````

## Lesson 2: Variables

``````Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
>>> age = 39
>>> age ** 2
1521
>>> x = 4
>>> y = 5
>>> x * y
20
>>> x = 7
>>> x * y
35``````

## Lesson 3: Simple Data Types

``````Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
>>> name = "Kyle"
>>> duplicated_name = name * 7
>>> duplicated_name
'KyleKyleKyleKyleKyleKyleKyle'``````

## Lesson 4: Lists

``````Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
>>> animals = ["dog", "cat", "squirrel"]
>>> animals
'dog'
>>> animals
'cat'
>>> animals
'squirrel'
>>> animals
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
animals
IndexError: list index out of range``````

## Lesson 5: Script Mode

``````age = 39
square_of_age = age ** 2
print(square_of_age)``````

## Lesson 6: User Input & Type Conversion

``````user_age = int(input("What is your age? "))
print(user_age ** 2)``````

## Lesson 7: Conditional Execution

``````user_grade = int(input("What grade are you in? "))
if user_grade == 12:
print("I hope you graduate!")
else:
print("I hope you do well in high school!")``````

## Lesson 8: Repetition with While Loops

``````num = int(input("Enter a positive number: "))
while num > 0:
print(num)
num = num - 1
print("Blast off!")``````

## Lesson 9: Repetition with For Loops

``````for i in range(8):
print(i, i ** 2)``````

## Lesson 10: Functions without Return Values

``````def print_number_and_square(n):
print(n, n ** 2)

user_num = int(input("Enter a number: "))
while user_num != 0:
print_number_and_square(user_num)
user_num = int(input("Enter a number: "))``````

## Lesson 11: Functions with Return Values

``````def is_even(n):
return (n % 2) == 0

def print_number_and_evenness(n):
if is_even(n):
print(n, "is even")
else:
print(n, "is odd")

user_num = int(input("Enter a number: "))
while user_num != 0:
print_number_and_evenness(user_num)
user_num = int(input("Enter a number: "))``````

## Lesson 12: Modules

``````import math

def print_number_and_square_root(n):
print(n, math.sqrt(n))

user_num = int(input("Enter a number: "))
while user_num != 0:
print_number_and_square_root(user_num)
user_num = int(input("Enter a number: "))``````