Featured image for Five things to know about Python.

Getting started with a new programming language can be challenging. Whether you're a beginner or a grizzled veteran, there are a number of larger context questions to answer that go beyond simply learning the language's syntax. This article provides a high-level overview of five important things to keep in mind as you begin your journey into Python. You won't learn the specifics of the language here, but you'll gain a general picture of how Python works. 

Note: Also see the Five things to know before learning Python video from Red Hat Developer.

1: Python is an interpreted language

Programming languages fall into two categories: Those that require a compilation step prior to running (such as Java and C) and those that are interpreted directly from the source code (like JavaScript and Ruby). Python falls into the latter category. Python source code files, commonly referred to as “scripts,” are used directly by a Python interpreter to execute.

For example, take the following code:

print(‘Hello World’)

When saved to a file, for example hello.py, it can be passed to a Python interpreter without the need for an explicit compilation step:

$ python hello.py
Hello World

2: Python is object-oriented, but not exclusively

If you come from an object-oriented background, particularly Java where everything is an object, the hello.py example may look a little strange. The single-line script not only doesn’t define any classes, but it isn’t even inside of a method declaration.

Python supports object-oriented programming, but you aren’t locked into it. You can add functions directly to a script when there isn’t a need for the overhead and complication of defining a class.

For example, take the following (obviously academic) class:

class PhoneNumber(object):

    def __init__(self, area_code, number) -> None:
        self.area_code = area_code
        self.number = number

    def display(self):
        print(f'({self.area_code}) {self.number}')

pn = PhoneNumber('973', '555-1234')
pn.display()

Note: This article won't get into the details of Python. However, it is worth mentioning that the self reference in this snippet is used to indicate object variables.

Running this script produces the formatted output (973) 555-1234.

If the output is the only goal, it arguably doesn’t need to be a class. You could rewrite it as a function, instead:

def display_pn(area_code, number):
    print(f'({area_code}) {number}')

display_pn('973', '555-7890')

A third option is to combine the two, defining stateless functions where appropriate and having objects use those methods:

class PhoneNumber(object):

    def __init__(self, area_code, number) -> None:
        self.area_code = area_code
        self.number = number

    def display(self):
        display_pn(self.area_code, self.number)

def display_pn(area_code, number):
    print(f'({area_code}) {number}')

pn = PhoneNumber('973', '555-1234')
pn.display()

3: Python is not strongly typed (which is a double-edged sword)

Take a look at the following, perfectly valid, Python code:

x = 'ba'
x = 1
x = print
x = None

That snippet assigns to the variable x a string literal, an integer, a function, and the Python value for null. On top of that, the variable didn't even need to be explicitly declared.

Python uses the concept of duck typing—if it swims like a duck and quacks like a duck, it's probably a duck. In other words, if the value of a variable has certain abilities, the actual type of object it is doesn't really matter.

Take the concept of iteration as an example. The for built-in function iterates over a collection of items. How those items are stored is irrelevant; the important part is that the object supports the ability to be iterated.

This is fairly obvious for simple constructs such as lists and sets:

x = [1, 2, 3]  # list
y = {1, 2, 3}  # set

for i in x:
    print(i)

for i in y:
    print(i)

For key-value pairs (known as a dict in Python), the for function will iterate over just the keys (producing the output a b c from the following snippet):

z = {'a': 1, 'b': 2, 'c': 3}

for i in z:
    print(i)

There are times, however, where this power and flexibility can produce ... interesting results. For example, a string is also considered iterable, meaning it can be passed into a for loop without producing a runtime error. But the results are often unexpected:

w = 'water'

for i in w:
    print(i)

That snippet will run without error, producing the following:

w
a
t
e
r

Note: This particular example is meant to demonstrate a situation where a list of length 1 (in other words, a list with the word water) was expected, rather than the literal string. There are many other situations where duck typing doesn't produce a runtime exception; however, the behavior is not what was intended.

4: Whitespace matters in Python

It may seem odd to highlight something as seemingly trivial as whitespace, but it's such an important aspect of Python's syntax that it warrants mentioning.

Python uses indentation to indicate scope, freeing it from the arguments about curly brace placement that other languages encounter. Generally speaking, a code block is defined by statements that share the same indentation level. Looking again at the phone number example:

class PhoneNumber(object):

    def __init__(self, area_code, number) -> None:
        self.area_code = area_code
        self.number = number

    def display(self):
        display_pn(self.area_code, self.number)

def display_pn(area_code, number):
    print(f'({area_code}) {number}')

The two assignments in the __init__ method (Python's implementation of a constructor) are considered part of the method definition. We know this because they are indented further than the declaration and share the same indentation level. If the second statement (self.number = number) was offset by even a single space in either direction, the code would fail to run (with an error similar to IndentationError: unexpected indent).

Along the same lines, the display_pn function is indented at the same level as the PhoneNumber class, indicating it is not part of the class definition. Keep in mind, however, that the indentation of the body of display_pn has no bearing on the bodies of the class methods (in other words, there are no syntactic implications to the fact that the body of display_pn and the definition of display() are both indented by four spaces).

Note: See the PEP 8 Style Guide for Python Code for more details about whitespace, as well as general Python code style guidelines.

5: Use virtual environments to prevent dependency conflicts

In many cases, you'll already have a Python interpreter installed on your system. For development, however, you'll likely want to create a virtual environment, which is effectively a copy of the interpreter that is scoped specifically to that environment.

The reason for using virtual environments largely revolves around installing dependencies. Without using a virtual environment, any dependencies that are installed for your project (such as the Django, Flask, pandas, or numpy libraries) are installed to the global interpreter. Having such dependencies installed system-wide is a risk for a number of reasons, including version compatibility issues.

Instead, creating a virtual environment for your project provides an individually scoped interpreter to use. Any dependencies installed to the virtual environment only exist for that environment, allowing you to easily develop on multiple projects without fear of system-wide implications or conflicts.

There are a number of ways to manage Python virtual environments, including the built-in venv command, as well as the (arguably more user-friendly) utility packages pyenv and virtualenv.

Conclusion

This article is not a comprehensive overview of the Python language or its syntax. But it should help set the stage for what to expect and how to best work with the language. With these basic concepts in mind, the next step is to dive in and start to experiment.

Last updated: October 6, 2022