Values are basic things a program works with. Values come in several different types:
string
is any value between quotes ('' single or double "") e.g. 'Hello Coding Circle', "I am very smart", "342"integer
is any whole number, positive or negative e.g. 145, -3, 5float
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";
In [ ]:
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.
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)
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 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
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)
In [ ]:
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
In [ ]:
5 % 2
In [ ]:
In [ ]:
print('Hello ' + 'Coding Circle')
In [ ]:
print("The " + WHALE + " lives in the sea.")
Hint: Be careful with spaces
In [ ]:
print("My name is" + "Charlotte")
In [ ]:
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.
int
-- e.g., int(variable_name)
float
-- e.g., float(variable_name)
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)
In [ ]:
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
In [ ]:
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.
birth_year
.birth_year
to an int and store in variable called birth_year
.birth_year
and store in a variable called drive_car_year
.birth_year
and store in a variable called alcohol_year
.birth_year
and store in a variable called president_year
.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 [ ]: