Python Fundamentals¶

Variables and Basic Operators¶

Make easy things easy and hard things possible.

- A slogan of Perl (a predecessor language to Python)

Applied Review¶

Python and Notebooks¶

  • We're working with Python in Notebooks, a common workflow in data science.
  • Notebooks are composed of cells, which can contain either code or markdown text.

Python at Its Simplest: Basic Math¶

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.

In [1]:
1 + 2
Out[1]:
3
In [2]:
12 * 4
Out[2]:
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.

In [3]:
# The ** operator is exponentiation.
2 ** 3
Out[3]:
8

Variables¶

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.

In [4]:
# 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.

In [5]:
# "Assign" the value 42 to the variable "answer".
answer = 42

You can then use the stored values in new calculations.

In [6]:
answer + 5
Out[6]:
47
In [7]:
ten = 10
eleven = 11
ten + eleven
Out[7]:
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.

In [8]:
# Perfectly good variable name
my_3rd_favorite_number = 18
In [9]:
# Legal, but undescriptive, variable name
a = 7
In [10]:
# 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.

Your Turn¶

  1. 4k monitors, counterintuitively, typically have a resolution of 3840x2160. Create two variables, width and height, and store 3840 and 2160 in them (respectively).
  2. How many total pixels are in a display with this resolution? Hint: fill in the blanks with variable names: pixels = ___ * ___

Data Types: Beyond Integers¶

Fortunately, Python can handle values beyond integers. It's happy to work with decimal numbers.

In [11]:
1 / 3
Out[11]:
0.3333333333333333
In [12]:
1.5 * 1.5
Out[12]:
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.

In [13]:
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.

In [14]:
'Monty' + ' ' + 'Python'
Out[14]:
'Monty Python'
In [15]:
first_name = 'Guido'
last_name = 'van Rossum'
# Remember to add a space between words!
first_name + ' ' + last_name
Out[15]:
'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.

In [16]:
is_the_moon_made_of_cheese = False
is_this_the_best_python_class = True

Your Turn¶

  1. Overwrite the first_name and last_name variables with your name, and run first_name + ' ' + last_name again -- make sure it produces what you expect!
  2. What happens when you try to add together two different kinds of values, like an integer and a string? Does this behavior make sense?

Boolean Logic¶

  • Operators like <, >, and == can be used to compare strings and numbers in Python
  • Results of these "comparison" operators are booleans (true or false)
In [17]:
3 > 4
Out[17]:
False
In [18]:
5 > 5
Out[18]:
False
In [19]:
# Greater-than-or-equal-to is expressed with >=
5 >= 5
Out[19]:
True
In [20]:
2 <= 10
Out[20]:
True
In [21]:
1 == 1
Out[21]:
True
In [22]:
1 == 2
Out[22]:
False
In [23]:
# Is-not-equal-to is expressed with !=
1 != 2
Out[23]:
True
In [24]:
1 != 1
Out[24]:
False

With strings, < and > compare using alphabetical order.

In [25]:
'abc' > 'bbc'
Out[25]:
False
In [26]:
'ghi' == 'ghi'
Out[26]:
True
In [27]:
'c' <= 'def'
Out[27]:
True
In [28]:
'j' != 'k'
Out[28]:
True

Questions¶

Are there any questions before we move on?