Variables, expressions, and statements

Values and Types

Values are basic things a program works with. Values come in several different types:

  • A string is any value between quotes ('' single or double "") e.g. 'Hello Coding Circle', "I am very smart", "342"
  • An integer is any whole number, positive or negative e.g. 145, -3, 5
  • A float is any number with a decimal point e.g. 3.14, -2.5

To tell what type a value is, use the built-in function type()


In [ ]:
type('I am amazing!')

In [ ]:
type(145)

In [ ]:
type(2.5)

To print a value to the screen, we use the function print()

e.g. print(1)


In [ ]:
print("Hello World")

Jupyter notebooks will always print the value of the last line so you don't have to. You can suppress this with a semicolon ';'


In [ ]:
"Hello World"

In [ ]:
"Hello World";

TRY IT

Predict and then print the type of 'Orca'


In [ ]:

Variables

A variable is a name that you give a value. You can then use this name anywhere you would use the value that the name refers to.

It has some rules.

  • It must only contain letters, numbers and/or the underscore character.
  • However, it cannot start with a number.
  • It can start with an underscore but this usually means something special so stick to letters for now.

To assign a value to a variable, you use the assignment operator, which is '=' e.g., my_name = 'Charlotte'


In [ ]:
WHALE = 'Orca'
number_of_whales = 10
weight_of_1_whale = 5003.2

Notice that when you ran that, nothing printed out. To print a variable, you use the same statement you would use to print the value. e.g. print(WHALE)


In [ ]:
print(number_of_whales)

TRY IT

Assign the name of a sea creature to the variable sea_creature. Then print the value.


In [ ]:

Reccomendation Name your variables with descriptive names. Naming a variable 'a' is easy to type but won't help you figure out what it is doing when you come back to your code six months later.

Operators and operands

Operators are special symbols that represent computations that the computer performs. We have already learned one operator: the assignment operator '='.

Operands are the values the operator is applied to.

Basic math operators

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ** power (exponentiation)

To use these operators, put a value or variable on either side of them. You can even assign the new value to a variable or print it out. They work with both integers or floats.


In [ ]:
1 + 2

In [ ]:
fish = 15
fish_left = fish - 3
print(fish_left)

In [ ]:
print(3 * 2.1)

In [ ]:
number_of_whales ** 2

In [ ]:
print(5 / 2)

Hint: You can use a variable and assign it to the same variable name in the same statement.


In [ ]:
number_of_whales = 8
number_of_whales = number_of_whales + 2 
print(number_of_whales)

TRY IT

Find the result of 6^18.


In [ ]:

Order of operations

You can combine many operators in a single python statement. The way python evaluates it is the same way you were taught to in elementary school. PEMDAS for Please Excuse My Dear Aunt Sally. Or 1. Parentheses, 2. Exponents, 3. Multiplication, 4. Division, 5. Addition, 6. Subtraction. Left to right, with that precedence. It is good practice to always include parentheses to make your intention clear, even if order of operations is on your side.


In [ ]:
2 * 3 + 4 / 2

In [ ]:
(2 * (3 + 4)) / 2

Modulus operator

The modulus operator is not one you were taught in school. It returns the remainder of integer division. It is useful in a few specific cases, but you could go months without using it.


In [ ]:
5 % 2

TRY IT

Find if 12342872 is divisible by 3


In [ ]:

String operations

The + operator also works on strings. It is the concatenation operator, meaning it joins the two strings together.


In [ ]:
print('Hello ' + 'Coding Circle')

In [ ]:
print("The " + WHALE + " lives in the sea.")

Hint: Be careful with spaces


In [ ]:
print("My name is" + "Charlotte")

TRY IT

Print out Good morning to the sea creature you stored in variable named sea_creature earlier.


In [ ]:

Asking the user for input

To get an input for the user we use the built-in function input() and assign it to a variable.

NOTE: The result is always a string.

WARNING if you leave an input box without ever putting input in, jupyter won't be able to run any code. Ex. you run a cell with input and then re-run that cell before submitting input. To fix this hang the stop button in the menu.


In [ ]:
my_name = input()
print(my_name)

You can pass a string to the input() function to prompt the user for what you are looking for.

e.g. input('How are you feeling?')

Hint, add a new line character "\n" to the end of the prompt to make the user enter it on a new line.


In [ ]:
favorite_ocean_animal = input("What is your favorite sea creature?\n")
print("The " + favorite_ocean_animal + " is so cool!")

If you want the user to enter a number, you will have to convert the string. Here are the conversion commands.

  • To convert a variable to an integer, use int -- e.g., int(variable_name)
  • To convert a variable to a float, use float -- e.g., float(variable_name)
  • To convert a variable to a string, use str -- e.g., str(variable_name)

In [ ]:
number_of_fish = input("How many fish do you want?\n")
number_of_fish_int = int(number_of_fish)
print(number_of_fish_int * 1.05)

TRY IT

Prompt the user for their favorite whale and store the value in a variable called favorite_whale.


In [ ]:

Comments

Comments let you explain your progam to someone who is reading your code. Do you know who that person is? It is almost always you in six months. Don't screw over future you. Comment your code.

To make a comment: you use the # symbol. You can put a comment on its own line or at the end of a statement.


In [ ]:
# Calculate the price of fish that a user wants
number_of_fish = input("How many fish do you want?\n") # Ask user for quantity of fish
number_of_fish_int = int(number_of_fish) # raw_input returns string, so convert to integer
print(number_of_fish_int * 1.05) # multiply by price of fish

TRY IT

Write a comment.


In [ ]:

Project: Milestones

We are going to create an application that prompts the user for their (or their child's) birth year and will calculate and tell them the years of their milestones: Drive a car at 16, Drink alcohol at 21, and Run for president at 35.

  1. Ask the user for the birth year and store in a variable called birth_year.
  2. Convert birth_year to an int and store in variable called birth_year.
  3. Add 16 to the birth_year and store in a variable called drive_car_year.
  4. Add 21 to the birth_year and store in a variable called alcohol_year.
  5. Add 35 to the birth_year and store in a variable called president_year.
  6. Print out the message "You can drive a car in: drive_car_year" and similar messages for the other years. Hint: you will need to use string concatenation, you will also have to cast the integer years to strings using the str() method.

In [ ]: