Python Basics: Everything You Need to Know to Get Started

Python Basics: Everything You Need to Know to Get Started Featured Image

Programming is not something you can pick up in a day. That said, if you wanted a crash course in Python basics, you’ve come to the right place. This article will give you a quick and dirty overview of many of the core concepts of my favorite programming language, Python.

Table of Contents

Installation

There’s really no point in talking about Python if you don’t already have a way to run it. Fortunately, there are several ways to get your system ready to go.

First, you can skip installing Python by using an online editor instead. For example, you might head over to replitOpens in a new tab. which has an online editor for both Python 2 and Python 3. From there, you can jump straight down to the syntax section and start writing some code.

But, wait a minute! What’s all this about two different versions of Python? This is one of the quirky bits of learning to program. Programming languages, in general, change over time as they receive new features and bug fixes. To indicate these changes, version numbers are used.

As of 2021, Python has gone through three major iterations. The most recent version, Python 3, has been around since 2008. It’s predecessor, Python 2, came out in 2000. Currently, Python 2 has reached end-of-life, so it’s recommended that you stick with Python 3—as I’ve discussed in the past.

With that said, the second way to get Python running on your system is to head over to the Python website to download the installOpens in a new tab.. At the time of writing, the most recent version of Python was Python 3.9.5. Once you’ve downloaded it and run the install, you should be good to go!

Tools

Awhile back, I wrote a whole article on Python tools, but here’s the quick summary. There are three main ways to write Python code:

  • Interpreters
  • Text Editors
  • IDEs

An interpreter (or more specifically, a REPL) is a tool that allows you to write code line-by-line to see what it does. You can think of this sort of like a calculator where you punch in a command and observe its output. Typically, folks use these to test small snippets of code or to learn code as beginners. Python comes with one of these out of the box called IDLE, if you want to give it a whirl.

A text editor is sort of the next step up. It allows you to create files which can store multiple lines of code. Later, these files can be executed using plugins or external tools to perform more complex tasks. For example, I use a Python file to generate my featured images at the top of each post. A common text editor in 2021 is VS Code, but there are probably 100s of options.

An integrated development environment (IDE) is the final evolution of the text editor which includes ways to write and execute code—among other handy features. IDES are recommended if you want an all-in-one solution which can be quick to setup and run. That said, they often carry a bit of a learning curve which is why folks might recommend starting with a REPL first. I personally use PyCharm, but there are a handful of other choices as well.

Syntax

Once you’ve downloaded Python and picked a development tool, the rest is learning the syntax (i.e., the language grammar). Fortunately, Python syntax is pretty quick to pickup. There are basically two main sets of structures you need to worry about: statements and expressions.

Expressions are pieces of code that equate to values. For example, adding two numbers together is an expression that equates to a sum. These expressions are important because they allow us to think about data in a variety of ways.

That said, expressions don’t do anything. Sure, we can use expressions to calculate values, but those values don’t perform any action. To do anything in a program, we need statements.

A statement can be thought of as any line of code (for the most part). The goal of a line of code is to alter the current state of the program. For example, we might use an expression to compute a value. That value will quickly disappear if we don’t do anything with it, so we might store it in a variable. Now, the state of the program includes the definition of a variable which we can reuse later.

Together, we use statements and expressions to perform more complex tasks such as machine learning or data visualization. But you’re probably wondering, what do these different structures actually look like? We’ll take a look in the following sections.

Common Expressions

In Python, any code that produces a value can be considered an expression. For example, the following numbers are expressions:

>>> 5
5
>>> 7
7
>>> 19
19

Of course, we can also combine numbers in a variety of ways using operators like addition and subtraction:

>>> 2 - 7
-5
>>> 5 * 9
45
>>> 13 + 2
15
>>> 4 / 5
0.8

That said, we’re not restricted to numbers in Python. There are several data types that we can evaluate including strings, lists, and dictionaries:

>>> "hello!"
'hello!'
>>> [3, 4, 5]
[3, 4, 5]
>>> {"RGB": "00AA00"}
{'RGB': '00AA00'}

All of these expressions produce values, but these values don’t do anything. In other words, by the time we’ve moved to the next line of code, our data is gone. To do anything with these values, we have to introduce some statements.

Common Statements

To be able to make use of expressions, we need to incorporate statements in our code. For example, to save the result of an expression, we can use an assignment statement:

>>> addition = 2 + 7
>>> addition
9

By storing the result of our addition expression, we change the state of our program to include a new variable. We can now access that variable at any time.

That said, variables are not all that interesting on their own. Ultimately, we would like to be able to do something more interesting like make a decision given the result of the addition. Luckily, we can do that with an if statement:

>>> if addition > 0: print("The sum is positive")

The sum is positive

Here, our if statement checks if the sum is positive. If it is, we print out a message to the user. If we wanted to print out a message if the value was not positive, we can include an else statement:

>>> if addition > 0: print("The sum is positive")
else: print("The sum is non-positive")

The sum is positive

Unfortunately, even these branching statements aren’t enough to encompass all of the functionality we would like in a language. For example, what if we wanted to repeat a task several times? We’d need another kind of statement.

Luckily, Python includes tools for looping such as the “for” and “while” statements. I’ll show you what that might look like:

>>> for i in range(addition): print(i)

0
1
2
3
4
5
6
7
8

Here, we took the result of adding our numbers together and wrote a loop to iterate that many times (i.e., 9 times). Interestingly, this statement introduces a new variable, `i`, that counts from zero up until our sum. We then print that value after each loop.

Together, these three types of statements can be used to write pretty much any program we might want. Surely, Python provides other types of statements, but these are the three most common to get you started.

Data

Being able to write a program in Python goes further than being able to understand statements and expressions. You also need to be able to handle data. In this case, data can range anywhere from a single number to a set of nested lists. In this section, we’ll look at some common data types and what we can do with them.

Up first, we’ll talk about numbers. In Python, numbers operate pretty much as you would expect them to operate. This differs from other programming languages in a very good way. There is really no learning curve here if you already know how to deal with integers and decimal values (with some caveats, of course):

>>> positive = 18349371
>>> negative = -321984
>>> zero = 0
>>> rational = 4 / 10
>>> decimal = 6.7

Another very common data type is the string which is a collection of characters. These are a bit more complicated under the hood, but Python makes them about as approachable as I’ve seen in programming languages:

>>> name = "Jeremy"
>>> color = "Red"

Strings have a variety of uses, but they’re commonly used when simpler data types cannot fully represent your data (e.g., error messages).

Yet another common data type is the boolean represented as a pair of values, True and False, though you won’t see these very often in the wild. It’s much more likely to see them occur as a result of an expression:

>>> 2 < 3
True
>>> 5 > 10
False
>>> "hi" == "hi"
True

Finally, there are a variety of more complex data structures which exist to store more than a single value at a time (e.g., lists, sets, dictionaries, etc.). I won’t dwell on these, but there are a few worth seeing:

>>> cats = ["Reina", "Mandy"]
>>> colors = {"red": "FF0000", "blue": "0000FF"}

Overall, it’s worth being aware of the various ways to store data as this will provide you with many options when creating a program.

Summary

Learning any programming language can be hard. As a result, I wouldn’t expect you to fully understand everything you read in this article. Instead, I put this article together to give you some exposure to the types of things you might want to know like how to store data or what the Python syntax looks like.

In addition, a large reason I put this article together was to point you to some resources that might help you get started. For instance, I have an entire series for people who want to teach themselves Python. Here are some of the article titles to get you started:

Likewise, if you liked this article, I have a cheat sheet that covers a lot of the same material which you can find by subscribing to my monthly newsletter. Alternatively, you can check out my list of ways to grow the site.

Also, here are some resources from the folks at Amazon that might help you get started with Python (#ad):

Otherwise, I appreciate you taking the time to check out this article. Hopefully, you got something out of it. With that said, take care!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts