Make easy things easy and hard things possible.
- A slogan of Perl (a predecessor language to Python)
While Python can be used to write very complicated programs, one of its strengths is that easy things are still easy.
For example, Python can be a calculator.
1 + 2
3
12 * 4
48
Python allows you to comment your code -- to leave notes for yourself or others about the code.
Comments start with a #
and are ignored by Python when it runs your code.
# The ** operator is exponentiation.
2 ** 3
8
Once you start doing math, you may want to keep the values you calculate for later use.
Python allows you to do this with variables -- words that you choose to represent values you've stored.
# Place the result of "5 * 2" in a variable called "x".
x = 5 * 2
This process of storing something in a variable is often called variable assignment, or simply "assignment" for short. You can assign almost anything to a variable.
# "Assign" the value 42 to the variable "answer".
answer = 42
You can then use the stored values in new calculations.
answer + 5
47
ten = 10
eleven = 11
ten + eleven
21
Python lets you name your variables whatever you want – the only rule is that they must be composed of numbers, letters, and underscores, and they cannot begin with a number.
It's a good idea to take advantage of this flexibility and name your variables with descriptions that help you remember what they contain.
For example, calling your variables x
, y
, and z
is likely to lead to forgetting what you've stored where (unless you're working with coordinates, a domain where those names have meanings).
More descriptive names, like number_of_items
or size_of_container
, are better.
# Perfectly good variable name
my_3rd_favorite_number = 18
# Legal, but undescriptive, variable name
a = 7
# Illegal variable name -- it starts with a number
4_plus_1 = 4 + 1
Cell In[10], line 2 4_plus_1 = 4 + 1 ^ SyntaxError: invalid decimal literal
If you try to name a variable something illegal, Python will gently remind you to follow the rules with a SyntaxError
and an arrow indicating the location of the error.
Caution!
Sometimes Python doesn't pinpoint the error very well, and the error will not be in the same place as the arrow.
width
and height
, and store 3840 and 2160 in them (respectively).pixels = ___ * ___
Fortunately, Python can handle values beyond integers. It's happy to work with decimal numbers.
1 / 3
0.3333333333333333
1.5 * 1.5
2.25
In computer science lingo, decimal numbers are often called floating point numbers, or floats for short.
The name refers to how such numbers are stored by a computer internally, but you don't need to worry about that.
Just be aware that many people on the internet and in data science industry will speak in terms of "floats" and "ints" when they refer to numbers in Python.
Python also can work with text data, like words and sentences.
my_name = 'ethan'
my_hobbies = 'coding, reading, basketball'
In Python, these bits of text are called strings and are enclosed in quotation marks.
Both single quotes ('
) and double quotes ("
) work, and Python treats them identically.
Conveniently, Python lets you "add" strings together to compose longer strings.
'Monty' + ' ' + 'Python'
'Monty Python'
first_name = 'Guido'
last_name = 'van Rossum'
# Remember to add a space between words!
first_name + ' ' + last_name
'Guido van Rossum'
The last kind of value that we'll talk about is a boolean, or a True/False value.
Python recognizes the words True
and False
as "literal" boolean values -- you can use them without quotes to represent logical truth/falsity.
That means you can assign them to variables as you can with other data types.
is_the_moon_made_of_cheese = False
is_this_the_best_python_class = True
first_name
and last_name
variables with your name, and run first_name + ' ' + last_name
again -- make sure it produces what you expect!<
, >
, and ==
can be used to compare strings and numbers in Python3 > 4
False
5 > 5
False
# Greater-than-or-equal-to is expressed with >=
5 >= 5
True
2 <= 10
True
1 == 1
True
1 == 2
False
# Is-not-equal-to is expressed with !=
1 != 2
True
1 != 1
False
With strings, <
and >
compare using alphabetical order.
'abc' > 'bbc'
False
'ghi' == 'ghi'
True
'c' <= 'def'
True
'j' != 'k'
True
Are there any questions before we move on?