Values are basic things a program works with. Values come in several different types:
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")
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 [ ]:
apples = 15
apples_left = apples - 3
print(apples_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 [ ]:
In [ ]:
my_name = eval(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 = eval(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.
In [ ]:
number_of_apples = eval(input("How many apples do you want?\n"))
number_of_apples_int = int(number_of_apples)
print((number_of_apples_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 apples that a user wants
number_of_apples = eval(input("How many apples do you want?\n")) # Ask user for quantity of apples
number_of_apples_int = int(number_of_apples) # raw_input returns string, so convert to integer
print((number_of_apples_int * 1.05)) # multiply by price of apples
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.
In [ ]: