Exercise 1: Basics

Goals:

  • Manipulate text and number variables
  • Print text and numbers together
  • Control program flow with if, and for
  • Encapsulate related things into functions with def

1. Print variables

Exercise: Print the contents of these variables.


In [ ]:
Name = "John Doe"              # String.
Age = 40                       # Integer.
Height = 180.3                 # Float.
Married = True                 # Boolean (True/False).
Children = ["Emma", "Thomas"]  # List.
Female = False                 # Boolean (True/False).
Interests = []                 # Empty list.

2. Convert celsius to fahrenheit

Exercise: Convert celsius to fahrenheit and store it a new variable. Use this equation: celsius * 1.8 + 32

Present the results in this format: 5 celsius is XXX fahrenheit.


In [ ]:
celsius = 5

In [ ]:
# Write here

3. Calculate percentage

Exercise: Caculate the percentage of people who are employed.

Present the results in this format: 11.4 %


In [ ]:
population = 10000000
employed = 4568600

In [ ]:
# Round to 1 decimal.
round(3.3333333333, 1)

4. Manipulate text

Exercise: Replace Johns last name with your own.


In [ ]:
Name = "John Doe"
text = "The quick brown fox jumps over the lazy dog"

In [ ]:
# Show the first, and fifth character.
print(text[0]) # Note, it starts with 0!
print(text[4])

In [ ]:
# Show the first 3 characters.
print(text[0:3])

In [ ]:
# Replace text.
print(text.replace("fox", "journalist"))

In [ ]:
# Make text lower case, UPPER CASE or Title Case.
print(text.lower())
print(text.upper())
print(text.title())

In [ ]:
# Character length.
len(text)

In [ ]:
# Find the word "quick". Returns position of first character.
print(text.find("quick"))

In [ ]:
# What happens when we don't find the word? It returns -1.
print(text.find("journalism"))

5. If-statements

Exercise: If John is a male and married, print his age. But if John is male and unmarried, print his height.

Use if and elif.


In [ ]:
Name = "John Doe"              # String.
Age = 40                       # Integer.
Height = 180.3                 # Float.
Married = True                 # Boolean (True/False).
Children = ["Emma", "Thomas"]  # List.
Female = False                 # Boolean (True/False).
Interests = []                 # Empty list.

In [ ]:
if Age == 40:
    print(Name + " is 40 years old.")
else:
    print(Name + " is not 40 years old.")

6. For loops

Exercise: Print names of all children that does not have the name "Emma".


In [ ]:
# Print a range of numbers.
for i in range(5):
    print(i)

In [ ]:
# Print each name in the list.
for name in Children:
    print(name)

7. Functions

Exercise: Write a function that check how many names starts with the letter E.

Break the task down in steps:

  1. Create a new variable called e_letters with the value 0. It will store the number of names with E.
  2. Loop through the list of names.
  3. For each name, check if name starts with the letter E.
    • Use name.startswith("E") function.
    • If True, increase e_letters with 1.

In [ ]:
# A function that multiples two numbers together.
def calc(x, y):
    return(x * y)

In [ ]:
calc(10, 5)

In [ ]:
-

In [ ]:
# Number of names in the list.
len(names)

In [ ]:
def countE(names):
    # Write here...