# Lecture 3, Part 1 Exercises¶

## Before starting, please run the following cell¶

In [1]:
from __future__ import division, print_function


## Question 1¶

### 1.1¶

Print the integers from 0 - 10 using a for loop

### 1.2¶

Print the integers from 0 - 10 using a while loop

### 1.3¶

Write a function that takes x as input and prints it

### 1.4¶

Write a function that takes x and prints every number between 0 and x-1 using a for loop

### 1.5¶

Write a function that takes x and prints every number between 0 and x-1 using a while loop

## Question 2¶

### 2.1¶

Use code to verify that 4 is even

### 2.2¶

Write a function that takes x and returns True if x is even

### 2.3¶

Write a function that takes x and prints every number between 0 and x-1 using a for loop

### 2.4¶

Write a function that takes x and prints every EVEN number between 0 and x-1 using a for loop

## 2.5¶

Write a function that takes x and prints every EVEN number between 0 and x-1 using a while loop

### 2.6¶

Using while True:, print the integers from 0 - 10

### 2.7¶

Write a function that takes x and prints every EVEN number between 0 and x-1 using while True:

### 2.8¶

Write a function that takes x and prints every EVEN number between 0 and x-1 using a for loop, without control flow (i.e. no if statements)

## Question 3¶

### 3.1¶

Create a list called my_list with 3 integers

### 3.2¶

Print the first element in my_list

### 3.3¶

Print the length of my_list

### 3.4¶

Print every number in my_list using a for x in y loop

### 3.5¶

Write a function that takes a list and prints all items in it using a for x in y loop

### 3.6¶

Print the sum of all the elements in my_list using a for x in y loop

### 3.7¶

Write a function that takes a list and prints the sum of all items in it using a for x in y loop

### 3.8¶

Write a function that takes a list and prints the sum of all items in it using a while loop

### 3.9¶

Write a function that takes a list and prints the sum of all items in it that does NOT use a for x in y loop or a while loop

## Question 4¶

### 4.1¶

Create a string called my_string with the value "racecar"

### 4.2¶

Print the first character of my_string

### 4.3¶

Print the last character of my_string using len

### 4.4¶

Print True if the first and last characters of my_string are the same

### 4.5¶

Write a function that takes a string and returns True if the first and last characters of the string are the same and False otherwise

### 4.6¶

Write a function that takes a string and returns True if the second and second to last characters of the string are the same and False otherwise

Note: A palindrome is a word that is spelt the same forward as it is backwards.

• Palindromes:
• racecar
• 12321

### 4.7¶

For each of the following strings, say whether they are palindromes (in words):

1. kayak
2. 12323
3. hello
4. noon

### 4.8¶

Write a function that takes a string and returns True if the string is a palindrome and False otherwise

# Question 5¶

### 5.1¶

Run the code below to print the numbers from 0 - 20

In [ ]:
for i in range(20):
print(i)


### 5.2¶

Add to the code in the block above (without deleting anything) to break out of the loop after printing the numbers 0 - 5

### 5.3¶

Now, use the code from 5.1 to print all the numbers between 0 and 20 that are not multiples of 3.

### 5.4¶

Write code that will print all the numbers between 0 and 20 that are not multiples of 3 using continue

### 5.5¶

Try running the code below and see what happens (hint: you might need to use the stop button at the top of the page).

In [ ]:
i = 0
while True:
i += 1
print(i)


### 5.6¶

Now, edit the code above to only print the numbers up to 20.

### 5.7¶

Now, using continue, use the same code from 5.6 to print the numbers up to 20 that are not multiples of 3.

## Question 6¶

### 6.1¶

What is the output of the following code?

i = 0
def func(x):
i = 10
print(i, x)

func(5)


What is the value of i after the above code is executed?

### 6.2¶

What is the output of the following code?

i = 8
def func2():
i += 10
print(i)

func2()


What is the value of i after the above code is executed?

### 6.3¶

What will j be when printed in the code below?

def func1(x):
j = 10
print(j, x)

def func2(x):
j = 5
func1(x)
print(x)

func2(3)
print(j)


### 6.4¶

Now, we are going to create a FortuneTeller function that will print out a fortune depending on the value of the input given. Use the code comments below to help you complete this task.

In [ ]:
# Let's pretend we are taking in user input. Make a variable input with the value 1.

# Function header that creates a function called FortuneTeller that takes no arguments.
def FortuneTeller():
# Add a local variable to FortuneTeller called input that has the value 3.

# Add a conditional that checks when input is equal to 1. Print out "You will be lucky" if input equals 1.

# Add a conditional that checks when input is equal to 2. Print out "You will be unlucky" if input equals 2.

# Add a conditional that checks when input is equal to 3. Print out "You will get money" if input equals 3.

# Add a conditional that checks when input is equal to 4. Print out "You will lose money" if input equals 4.

# Add a conditional that checks when input is equal to 5. Print out your own statement if input equals 5.

# Call the FortuneTeller function and observe the output value



### 6.5¶

What value did you get when you ran the code above? Was it the value you expected?

### 6.6¶

How can you change the code in 6.4 so that the output given is "You will lose money"?

### 6.7¶

In the code block below, what will be the output? Was it what you expected?

i = 100
for i in range(3):
print(i)


### 6.8¶

Will (a) and (b) output the same thing? If not, explain why.

(a)

k = 0
for i in range(3):
print(k)


(b)

k = 0
for i in range(3):
k = 6
print(k)