×
By the end of this chapter, you should be able to:
Python is a popular programming language that you can use to write server-side code for your web applications. You're only a Google search away from a number of different descriptions of Python, but if you're looking for an overview, a good place to start is the Python website itself. According to the about page, Python is "powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open."
In 2008, a new version of Python (3.0) was released. Python 3 is the future of the language and is what we will be learning in this class. Not all libraries are compatible with Python 3, but that will change shortly and should not be an issue for your learning. You can read more about the differences here.
Python is an extremely versatile language and is used by some of the largest companies in the world in multiple fields. It's beginner-friendly and has a welcoming community and set of standards around writing code in a "Pythonic" way.
We will be using Python 3, so make sure to download it here. Once you have downloaded Python 3, your next step is to be sure that you have the package manager for Python 3 installed too. The package manager for Python 3 is called pip3
, and it's what you'll be using to install any external Python 3 libraries.
To determine whether or not you've got pip3
installed, type pip3
into the terminal. If you see that the command is not found, follow instructions here to install it.
If you type python3
into the terminal and type in import this
you can see the guiding principles behind the language. Adhering to these principles will help you write more "Pythonic" code (we will see lots of examples of what that looks like). To exit the Python REPL you can type quit()
.
In Python there is something called PEP
or Python Enhancement Proposals
. Each proposal is simply a document, which can cover new features for the language, style guides, and more. Each proposal has a number that is assigned by the PEP editors, and once a proposal is accepted by the community, it is never changed. You can read more about them here.
One of the most important PEP
s is PEP8
, which is a style guide for Python. This guide was written by the creators of the language and is widely accepted as the standard for writing Python code. You can read more about it here.
If you'd like to read through the entire style guide, you can find it here.
Python has quite a few native datatypes. We'll provide an overview of a few of the more common ones. For a complete list of the built-in types, check out the docs. You can also find a list of even more data types available in Python here.
True
and False
(be mindful of the capitalization).[1, 2, 3, "the end"]
.(4, 2, 1)
is an example of a tuple.set("a", "b", "c")
.{"key": "value", "a": 0}
.We'll learn much more about these data types in the coming chapters!
To get started with a Python REPL
, which is where you will be writing your code, make sure python is installed and anywhere in the terminal, type in python3
. To exit the REPL
you can type quit()
or press control + d
.
If you find it easier to use, you can check out repl.it for Python3 and start here. To run your code you can click the run button (or use the keyboard shortcut you see when you hover over the button).
If you would like to write larger Python files, you can save them as .py
files. Let's imagine that you create a file called first.py
. If you want to run that file, you have to make sure you are in the same directory (folder) where the file is saved. To run the file, make sure you are in the Terminal/Command Line and run python3 first.py
. If you are not comfortable in the Terminal, check out our free online courses on Terminal and UNIX!
When you're ready, move on to Variables and Data Types