Learning Python has been something I’ve postponed over the years. I’ve run scripts here and there and created small scripts but nothing serious. I’m dealing with a scenario where I need to expand my knowledge. I’m excited about the opportunity, so here we go!

This article assumes a working knowledge of the command line and at least one programming language.

The Basics

Python is an object-oriented, interpreted, and easy-to-learn programming language. The syntax is concise and approachable to beginners. I’ve used it in the past off and on and have found myself saying, “That’s it? Wow!”. You’ll need to install Python to follow along.

Similar to Ruby, there is a pyenv utility for managing your python versions. It’s best to leave your system Python versions alone to avoid trouble with system dependencies.

#!/usr/bin/env python3
import pandas as pd

filename = "example.csv"
print(f"Chunking records for {filename}")
print("\n")
chunksize = 3
for chunk in pd.read_csv(filename, chunksize=chunksize):
    print(chunk)

Running this code requires a little more work. You’ll need to install pip to install the Pandas library for data analysis tasks. I’m on macOS, so I need to run this command.

python3 -m pip install --user --upgrade pip

I need to install the Pandas package.

pip3 install pandas

Download these files to run the code above and put them in the same directory.

example.csv & analyze-csv.py & .gitignore

Run this command in your terminal to make the program executable.

chmod +x ./analyze-csv.py

Run this command to list all your files.

ls -w

You should see output like this.

$ ls -w
analyze-csv.py		example.csv

Now execute the file.

./analyze-csv.py

You should see the following output.

$ ./analyze-csv.py
          store|sales
Office A            7
Office B            3
Office C            9
          store|sales
Office D          100
Office E            4
Office F           96
          store|sales
Office G           56
Office H           34
Office I           37
          store|sales

Low effort is needed to create a CSV file chunking program.

Virtual Environments

I’ve learned that using a virtual environment is preferred over using the operating system’s interpreter. The modern method to create a virtual environment is to use the venv package. Run this command in the directory you downloaded the analyze-csv.py file to. This command will initialize your virtual environment.

python3 -m venv venv/

I ran into issues running this on a network drive. If the command hangs, try moving your folder to a physical disk.

Run this command to activate the environment.

source venv/bin/activate

You should see a shell prompt like this if successful. Note the (env) text indicating that the virtual environment is activated.

(venv) Jeffs-Laptop:python jeffbailey$

Now your program is isolated from the system. We can relax knowing our program will more reliably run in a virtual environment; virtual environments save users headaches. Users can skip package management with your program.

To leave the virtual environment, run this command in your directory.

deactivate

Packages

Now that we have a virtual environment, we can install the Pandas package, but this time in the virtual environment.

pip install pandas

We can run this nifty command to create a requirements.txt file. This file tells users of our program what package versions to use.

pip freeze > requirements.txt

The freeze command will create a file that contains the dependencies you’ll need to run the analyze-csv.py file. Run this command to view the contents.

cat requirements.txt

You should see an output like this.

$ cat requirements.txt
numpy==1.18.4
pandas==1.0.3
python-dateutil==2.8.1
pytz==2020.1
six==1.15.0

The pip freeze command lists all packages installed on your system. We’ve now defined which dependencies are required for the program to run. Users of your application can run this command to install the necessary modules.

pip install -r requirements.txt

The virtual environment will keep us from using our system-wide installation of Python, which may have issues.

Virtual environments will make our programs happy little clams.

Push It

If you like GitHub, create a GitHub repository called learn-python and push your new code to it. If you’re in a hurry and already know all this stuff, you can clone my repository on GitHub and play around with it. Look in the programming/python directory.

Run these commands to initialize a new repository and prepare it for the push.

git add . && git commit -am "Initial Commit"

Now let’s push to our newly created repository. You’ll want to replace my username with your GitHub username.

git remote add origin git@github.com:jeffabailey/learn-python.git
git push -u origin master

Extend this program and have fun learning more Python!

Fundamentals

Learn Python without considering the fundamental programming constructs of Python? That’s crazy talk.

Variables

Variable assignment is simple enough; there is no magic here, just how I like it.

class color:
   BOLD_UNDERLINE = '\033[1m\033[4m'
   END = '\033[0m'

print(color.BOLD_UNDERLINE + 'Assign a string value to a variable' + color.END)
print("\n")
office_name = "Office A"
print(office_name)
print("\n")
print("----------------------------------------------")

print(color.BOLD_UNDERLINE + 'Assign a multiple line string value to a variable' + color.END)
print("\n")
office_name = """Office A\n
Office B
"""
print(office_name)
print("\n")
print("----------------------------------------------")

print(color.BOLD_UNDERLINE + 'Assign an integer value to a variable' + color.END)
print("\n")
office_sales = 7
office_sales_type = type(office_sales)
print(f"office_sales is type: {office_sales_type}")
print("\n")
print("----------------------------------------------")


print(color.BOLD_UNDERLINE + 'Assign a floating point value to a variable' + color.END)
print("\n")
office_score = 7.5
office_score_type = type(office_score)
print(f"office_score is type: {office_score_type}")
print("\n")
print("----------------------------------------------")

print(color.BOLD_UNDERLINE + 'Assign an boolean value to a variable' + color.END)
print("\n")
office_is_active = bool(True)
office_is_active_type = type(office_is_active)
print(f"office_is_active is type: {office_is_active_type}")
print("\n")
print("----------------------------------------------")

"""
Interesting tidbits

Python employs a concept called name mangling for private variables.abs

The code follows the naming section of Google’s style guide. I am using code in the style guide source code as an example. Snake case variable naming is common when looking at Python code.

Comments

Comments for sanity. Tell your future self and your friends why you did something weird once.

# A simple single-line comment
print("There's a comment before this line, trust me.")
"""
A
Comment
on multiple lines
"""
print("\n")
print("A few comments just happened, honest!")

Control Structures

We saw a for loop in our CSV analysis program.

offices = ["Office A", "Office B", "Office C"]

for office in offices:
  print(office)

Here’s another example.

A while loop

offices = ['Office A', 'Office B', 'Office C']
while offices:
  print(offices.pop(-1))

An if-else statement

office_a_sales = 3
office_b_sales = 100
if office_b_sales > office_a_sales:
  print("Office B sales are greater than Office A sales")
elif office_a_sales > office_b_sales:
  print("Office A sales are greater than Office B sales")

Functions

Functions are the bread and butter of any programming language. I also threw in an f-string, which allows you to format your strings with variables elegantly.

def file_check(fn):
  try:
      example_csv = open("example.csv", "r")
      try:
          print("Found file, printing file")
          print("\n")
          print(example_csv.read())
      finally:
          example_csv.close()
  except IOError:
      print('Error: File does not exist.')

file_check("example.csv")

Save this content to example.csv to run.

store|sales
"Office A","7"
"Office B","3"
"Office C","9"
"Office D","100"
"Office E","4"
"Office F","96"
"Office G","56"
"Office H","34"
"Office I","37"
"Office J","7"

Classes and Objects

Classes and objects are the nuts and bolts.

class Office:
    def __init__(self, name, location, sales):
        self.name = name
        self.location = location
        self.sales = sales

office = Office("Office A", "Portland, Oregon", 7)

print("Office Object Properties:")
print("\n")
print(f"Name: {office.name}")
print(f"Location: {office.location}")
print(f"Sales: {office.sales}")

Lambdas

Lambdas smooth out the rough edges. Use them sparingly; most Python programmers are not a fan of them. Consider using a function rather than a lambda in more complex situations.

offices = [{'name': 'Office A', 'sales': 7},
          {'name': 'Office B', 'sales': 3},
          {'name': 'Office C', 'sales': 9}]

print("Get the office with the highest amount of sales")
print(max(offices, key=lambda x: x['sales']))
print("\n")

print("Get the office with the least amount of sales")
print(min(offices, key=lambda x: x['sales']))

Exception Handling

Exception handling to keep from angering the user gods. Keep your errors under control and handle the flow.

def read_file(file_name):
    example_csv = None
    try:
        example_csv = open(file_name, "r")
    except IOError:
        print("Error: File does not exist.")
    return example_csv

example_csv = read_file("example.csv")
print(f"CSV File Contents:\n\n{example_csv.read()}")

Save this content to example.csv to run.

"store","sales"
"Office A","7"
"Office B","3"
"Office C","9"
"Office D","100"
"Office E","4"
"Office F","96"
"Office G","56"
"Office H","34"
"Office I","37"
"Office J","7"

Arrays

Arrays for life. Python arrays don’t contain surprises, making them easy to use.

offices = ["Office A","Office B", "Office C"]

print(f"We have an office array: {offices}")
print("\n")

print("Accessing array item 0")
x = offices[0]
print(f"Found {x}")
print("\n")

print("Update an array value")
offices[0] = "Office Z"
print(f"Updated Office A to {offices[0]}")
print("\n")

print("Get the length of an array")
x = len(offices)
print(f"{x} offices exist in the array")

print("\n")
print("Loop a sorted array")
for x in sorted(offices):
 print(x)
print("\n")

print("Add an array element named Office Y")
offices.append("Office Y")
print(offices)
print("\n")

print("Remove an array element")
offices.pop(1)
print(offices)
print("\n")

print("Delete an array element")
offices.remove("Office C")
print(offices)
print("\n")

Operators

Operators for operating stuff.

class color:
   BOLD = '\033[1;37;48m'
   CYAN = '\033[1;36;48m'
   YELLOW = '\033[1;33;48m'
   BOLD_YELLOW = BOLD + YELLOW
   BOLD_CYAN = BOLD + CYAN
   END = '\033[1;37;0m'

print(color.BOLD_CYAN + "Arithmetic Operators" + color.END)
print("\n")

addition = 1 + 1
print(f"1 + 1 = {addition}")
print("\n")

subtraction = 2 - 1
print(f"2 - 1 = {subtraction}")
print("\n")

multiplication = 3 * 3
print(f"3 * 3 = {multiplication}")
print("\n")

division = 10 / 5
print(f"10 / 5 = {division}")
print("\n")

modulus = 6 % 3
print(f"6 % 3 = {modulus} ")
print("\n")

exponentiation = 2 ** 3
print(f"2 ** 3 = {exponentiation}")
print("\n")

print(color.BOLD_YELLOW + "Assignment Operators" + color.END)
print("\n")

equals = 1
print(f"1 = {equals}")

equals += 1
print(f"1 += {equals}")

minus_equals = 2
minus_equals -= 1
print(f"2 -= {minus_equals}")

multiply_and = 5
multiply_and *= 5
print(f"5 *= {multiply_and}")

divide_and = 5
divide_and /= 5
print(f"5 /= {divide_and}")

modulus_and = 6
modulus_and %= 3
print(f"6 %= {modulus_and}")

exponent_and = 2
exponent_and *= 3
print(f"2 *= {exponent_and}")

y = 7
floor_division = 78125.0
floor_division//=y
print(f"7 //= {floor_division}")

Most Python operators are standard but watch out for type comparisons. Type comparisons with operators will compare the type names rather than the types themselves. Use the isInstance built-in function to compare types instead.

There are many other constructs to learn in Python. It’s a joy to work with Python, thanks to its natural and straightforward behavior.

Learn Python Beyond the Basics

There are thousands of Python books and loads of free tutorials online. Here are a couple of noteworthy titles that will help you learn effectively. I’ll leave you with The Zen of Python.

Books

Videos