Values & expressions
Values
A computer program manipulates values. The earliest programs were number munchers, calculating mathematical formulas more efficiently than a calculator. These days, programs deal with user data, natural language text, BIG data sets, and all sorts of interesting values.
In Python and most languages, each value has a certain data type. Some basic types:
An integer is a number with nothing after the decimal. Examples:
2
,44
,-3
A float is a number that does have something after the decimal, even if that something is a 0. Floating-point numbers are stored differently in computers and are treated differently in arithmetic calculations, so it's important to know whether you want a float or an integer. Examples:
3.14159
,4.5
,-2.0
A Boolean is a value that can only store two possible values, either
True
orFalse
. They’ll become very important when we want to be able to make decisions in programs and take different code paths based on whether something is true or false.A string is a string of text, where that text could be anything allowed by the character encoding. We can get into that later, but for now, you should be able to use a wide range of characters, including non-English characters and even emojis! For example,
'¡hola!'
and'python party 🐍'
are both valid strings. Strings can either be surrounded by single quotes' or "double quotes", as long as it’s the same on both sides.
There’s a lot we can do with those four types, so we’ll start with those and introduce fancier types like lists and dictionaries in later units.
Expressions
Computer programs use expressions to manipulate values and come up with new values. Here are some Python expressions that use arithmetic operators to do calculations:
18 + 69 # Results in 87
2021 - 37 # Results in 1984
2 * 100 # Results in 200
100/2 # Results in 50.0
100//2 # Results in 50
2 ** 100 # Results in 1267650600228229401496703205376
Most of those arithmetic operators likely look familiar to you, so let’s just break down the not so familiar ones.
The /
operator is the “true division” operator; it treats both numbers as floating point numbers and always results in a floating point number (even if the first number evenly divided the second). Contrasted to that, //
is the “floor division” operator; it calculates the result and then takes the “floor” of it, rounding down so that there is never anything after the decimal. It always results in an integer.
The **
operator is the exponentiation operator; it raises the first number to the power of the second number and returns the result. In math class, this is often written like 2^100, but Python decided to use **
instead. The ^
symbol is actually an operator in Python, but it's used for a very different operation (“bitwise exclusive or”) that we won’t dive into here.
You can put together multiple operators to make longer expressions, and use parentheses to group parts of the expression you want evaluated first. Parentheses work pretty much the same in programming as they do in math.
24 + ((60 * 6) / 12)
Exercise: Magic birthday math
There's a neat math trick that we used to do back in the days of calculators. After a long sequence of calculations, your birthday will come out at the end as a floating point number! Try to use Python to pull off the math trick instead, using the editor below.
The instructions:
Start with the number 7
Multiply by the month of your birth
Subtract 1
Multiply by 13
Add the day of your birth
Add 3
Multiply by 11
Subtract the month of your birth
Subtract the day of your birth
Divide by 10
Add 11
Divide by 100
String expressions
The most basic way to manipulate string values is concatenation: combining two strings together. If you've never heard that term, it's common in programming languages and comes from Latin "con" (with) + "catena" (chain). I always just think of "cadena", the Spanish word for necklace.
"red" + "blue" # Results in "redblue"
If you want a whitespace between the strings you're concatenating, you need to explicitly put a space inside one of the strings:
"hola " + "mundo" # Results in "hola mundo"
Or concatenate a string that is simply whitespace:
"oi" + " " + "galera" # Results in "oi galera!"
String concatenation will soon become very useful, once we learn how to use variables and functions.
Exercise: Say my name
Use Python to write an expression that results in your full name, by concatenating different parts of your name. Different cultures vary in the number of parts that names have (some have multiple middle names, some have no middle names at all), so you may end up concatenating additional strings or removing a string from the starter expression.
Call expressions
Many expressions use function calls that return values. For example, instead of writing 2 ** 100
, we could opt to write pow(2, 100)
and get the same result. The pow()
function is one of Python’s many built-in functions, which means it can be used inside any Python program anywhere.
A few more handy built-ins:
max(50, 300) # Results in 300
min(-1, -300) # Results in -300
abs(-100) # Results in 100
In fact, every arithmetic expression from above can also be expressed using function calls, but not all of the functions are built-in. Instead, we must import the functions from the Python standard library.
So, to make a function call that adds two numbers, our program starts off with a line that imports the add
function from the operator
module:
from operator import add
add(18, 69)
Or similarly, for subtraction:
from operator import sub
sub(2021, 37)
Why would we go through the effort of using the function when we could just use the arithmetic operator? In most programs, we wouldn’t. But it’s helpful to realize that every operator in Python can actually be expressed as a function call, since we’ll encounter some situations in the future where we can only use function calls and not operators.