Ch03 Getting Started with Python

Data Types

Variables

a = 3
b = 4
a + b
7
a = 3
print(a)
a = "three"
print(a)
3
three

Numeric Types

type(4)
int
type(4.4)
float
type(4.)
float
float(4)
4.0
int(4.9)
4
1.125 - 1.1
0.02499999999999991
3 + 4  # Sum
7
3 - 4  # Subtraction
-1
3 / 4  # Division
0.75
3 * 4  # Multiplication
12
3**4  # The power operator (Excel uses 3^4)
81
3 * (3 + 4)  # Use of parentheses
21

Comments

# This is a sample we've seen before.
# Every comment line has to start with a #
3 + 4
7
3 + 4  # This is an inline comment
7

Booleans

3 == 4  # Equality (Excel uses 3 = 4)
False
3 != 4  # Inequality (Excel uses 3 <> 4)
True
3 < 4  # Smaller than. Use > for bigger than.
True
3 <= 4  # Smaller or equal. Use >= for bigger or equal.
True
# You can chain logical expressions
# In VBA, this would be: 10 < 12 And 12 < 17
# In Excel formulas, this would be: =AND(10 < 12, 12 < 17)
10 < 12 < 17
True
not True  # "not" operator
False
False and True  # "and" operator
False
False or True  # "or" operator
True
bool(2)
True
bool(0)
False
bool("some text")  # We'll get to strings in a moment
True
bool("")
False
bool(None)
False

Strings

"A double quote string. " + 'A single quote string.'
'A double quote string. A single quote string.'
print("Don't wait! " + 'Learn how to "speak" Python.')
Don't wait! Learn how to "speak" Python.
print("It's easy to \"escape\" characters with a leading \\.")
It's easy to "escape" characters with a leading \.
# Note how Python allows you to conveniently assign multiple
# values to multiple variables in a single line
first_adjective, second_adjective = "free", "open source"
f"Python is {first_adjective} and {second_adjective}."
'Python is free and open source.'
"PYTHON".lower()
'python'
"python".upper()
'PYTHON'

Indexing and Slicing

language = "PYTHON"
language[0]
'P'
language[1]
'Y'
language[-1]
'N'
language[-2]
'O'
language[:3]  # Same as language[0:3]
'PYT'
language[1:3]
'YT'
language[-3:]  # Same as language[-3:6]
'HON'
language[-3:-1]
'HO'
language[::2]  # Every second element
'PTO'
language[-1:-4:-1]  # Negative step goes from right to left
'NOH'
language[-3:][1]
'O'

Data Structures

Lists

file_names = ["one.xlsx", "two.xlsx", "three.xlsx"]
numbers = [1, 2, 3]
file_names + numbers
['one.xlsx', 'two.xlsx', 'three.xlsx', 1, 2, 3]
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
cells = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]
cells[1]  # Second row
[4, 5, 6]
cells[1][1:]  # Second row, second and third column
[5, 6]

Line Continuation

a = (1 + 2
     + 3)
a = 1 + 2 \
    + 3
users = ["Linda", "Brian"]
users.append("Jennifer")  # Most commonly you add to the end
users
['Linda', 'Brian', 'Jennifer']
users.insert(0, "Kim")  # Insert "Kim" at index 0 
users
['Kim', 'Linda', 'Brian', 'Jennifer']
users.pop()  # Removes and returns the last element by default
'Jennifer'
users
['Kim', 'Linda', 'Brian']
del users[0]  # del removes an element at the given index
len(users)  # Length
2
"Linda" in users  # Check if users contains "Linda"
True
print(sorted(users))  # Returns a new sorted list
print(users)  # The original list is unchanged
['Brian', 'Linda']
['Linda', 'Brian']
users.sort()  # Sorts the original list
users
['Brian', 'Linda']
len("Python")
6
"free" in "Python is free and open source."
True

Dictionaries

exchange_rates = {"EURUSD": 1.1152,
                  "GBPUSD": 1.2454,
                  "AUDUSD": 0.6161}
exchange_rates["EURUSD"]  # Access the EURUSD exchange rate
1.1152
exchange_rates["EURUSD"] = 1.2  # Change an existing value
exchange_rates
{'EURUSD': 1.2, 'GBPUSD': 1.2454, 'AUDUSD': 0.6161}
exchange_rates["CADUSD"] = 0.714  # Add a new key/value pair
exchange_rates
{'EURUSD': 1.2, 'GBPUSD': 1.2454, 'AUDUSD': 0.6161, 'CADUSD': 0.714}
{**exchange_rates, **{"SGDUSD": 0.7004, "GBPUSD": 1.2222}}
{'EURUSD': 1.2,
 'GBPUSD': 1.2222,
 'AUDUSD': 0.6161,
 'CADUSD': 0.714,
 'SGDUSD': 0.7004}
currencies = {1: "EUR", 2: "USD", 3: "AUD"}
currencies[1]
'EUR'
# currencies[100] would raise an exception. Instead of 100,
# you could use any other non-existing key, too.
currencies.get(100, "N/A")
'N/A'

Tuple

currencies = ("EUR", "GBP", "AUD")
currencies[0]  # Accessing the first element
'EUR'
# Concatenating tuples will return a new tuple.
currencies + ("SGD",)
('EUR', 'GBP', 'AUD', 'SGD')

Set

set(["USD", "USD", "SGD", "EUR", "USD", "EUR"])
{'EUR', 'SGD', 'USD'}
portfolio1 = {"USD", "EUR", "SGD", "CHF"}
portfolio2 = {"EUR", "SGD", "CAD"}
# Same as portfolio2.union(portfolio1)
portfolio1.union(portfolio2)
{'CAD', 'CHF', 'EUR', 'SGD', 'USD'}
# Same as portfolio2.intersection(portfolio1)
portfolio1.intersection(portfolio2)
{'EUR', 'SGD'}

Summary

currencies = "USD", "EUR", "CHF"
currencies
('USD', 'EUR', 'CHF')
list(currencies)
['USD', 'EUR', 'CHF']

Control Flow

If Statement

i = 20
if i < 5:
    print("i is smaller than 5")
elif i <= 10:
    print("i is between 5 and 10")
else:
    print("i is bigger than 10")
i is bigger than 10
is_important = True
if is_important:
    print("This is important.")
else:
    print("This is not important.")
This is important.
values = []
if values:
    print(f"The following values were provided: {values}")
else:
    print("There were no values provided.")
There were no values provided.
is_important = False
print("important") if is_important else print("not important")
not important

For and While Loops

currencies = ["USD", "HKD", "AUD"]

for currency in currencies:
    print(currency)
USD
HKD
AUD
range(5)
range(0, 5)
list(range(5))  # stop argument
[0, 1, 2, 3, 4]
list(range(2, 5, 2))  # start, stop, step arguments
[2, 4]
for i in range(3):
    print(i)
0
1
2
for i, currency in enumerate(currencies):
    print(i, currency)
0 USD
1 HKD
2 AUD
exchange_rates = {"EURUSD": 1.1152,
                  "GBPUSD": 1.2454,
                  "AUDUSD": 0.6161}
for currency_pair in exchange_rates:
    print(currency_pair)
EURUSD
GBPUSD
AUDUSD
for currency_pair, exchange_rate in exchange_rates.items():
    print(currency_pair, exchange_rate)
EURUSD 1.1152
GBPUSD 1.2454
AUDUSD 0.6161
for i in range(15):
    if i == 2:
        break
    else:
        print(i)
0
1
for i in range(4):
    if i == 2:
        continue
    else:
        print(i)
0
1
3
for i in range(1, 4):
    print(i)
print(i)
1
2
3
3
n = 0
while n <= 2:
    print(n)
    n += 1
0
1
2

List, Set and Dictionary Comprehensions

currency_pairs = ["USDJPY", "USDGBP", "USDCHF",
                  "USDCAD", "AUDUSD", "NZDUSD"]
usd_quote = []
for pair in currency_pairs:
    if pair[3:] == "USD":
        usd_quote.append(pair[:3])
usd_quote
['AUD', 'NZD']
[pair[:3] for pair in currency_pairs if pair[3:] == "USD"]
['AUD', 'NZD']
[pair[3:] + pair[:3] for pair in currency_pairs]
['JPYUSD', 'GBPUSD', 'CHFUSD', 'CADUSD', 'USDAUD', 'USDNZD']
exchange_rates = {"EURUSD": 1.1152,
                  "GBPUSD": 1.2454,
                  "AUDUSD": 0.6161}
{k: v * 100 for (k, v) in exchange_rates.items()}
{'EURUSD': 111.52, 'GBPUSD': 124.54, 'AUDUSD': 61.61}
{s + "USD" for s in ["EUR", "GBP", "EUR", "HKD", "HKD"]}
{'EURUSD', 'GBPUSD', 'HKDUSD'}

Code organization

Functions

def convert_to_celsius(degrees, source="fahrenheit"):
    if source.lower() == "fahrenheit":
        return (degrees-32) * (5/9)
    elif source.lower() == "kelvin":
        return degrees - 273.15
    else:
        return f"Don't know how to convert from {source}"
convert_to_celsius(100, "fahrenheit")  # Positional arguments
37.77777777777778
convert_to_celsius(50)  # Will use the default source (fahrenheit)
10.0
convert_to_celsius(source="kelvin", degrees=0)  # Keyword arguments
-273.15

Modules and the Import Statement

import temperature
This is the temperature module.
temperature.TEMPERATURE_SCALES
('fahrenheit', 'kelvin', 'celsius')
temperature.convert_to_celsius(120, "fahrenheit")
48.88888888888889
import temperature as tp
tp.TEMPERATURE_SCALES
('fahrenheit', 'kelvin', 'celsius')
from temperature import TEMPERATURE_SCALES, convert_to_celsius
TEMPERATURE_SCALES
('fahrenheit', 'kelvin', 'celsius')

Date and Time

# Import the datetime module as "dt"
import datetime as dt
# Instantiate a datetime object called "timestamp"
timestamp = dt.datetime(2020, 1, 31, 14, 30)
timestamp
datetime.datetime(2020, 1, 31, 14, 30)
# Datetime objects offer various attributes, e.g. to get the day
timestamp.day
31
# The difference of two datetime objects returns a timedelta object
timestamp - dt.datetime(2020, 1, 14, 12, 0)
datetime.timedelta(days=17, seconds=9000)
# Accordingly, you can also work with timedelta objects
timestamp + dt.timedelta(days=1, hours=4, minutes=11)
datetime.datetime(2020, 2, 1, 18, 41)
# Format a datetime object in a specific way
# You could also use an f-string: f"{timestamp:%d/%m/%Y %H:%M}"
timestamp.strftime("%d/%m/%Y %H:%M")
'31/01/2020 14:30'
# Parse a string into a datetime object
dt.datetime.strptime("12.1.2020", "%d.%m.%Y")
datetime.datetime(2020, 1, 12, 0, 0)