Beginning Python Programming Part 1 — Variables, Constants, and Types

This is the documentation for Python 3.





orgPlease note that Python releases new versions often, so don’t feel like you need the latest version.

As long as you have the most recent minor version (3.

7), you should be alright.

As we move through this series, I’ll provide a few more links for you to go through.

How This Tutorial is DifferentMany tutorials show you how to do something; they hardly ever explain why you do something a certain way.

My goal is to teach you why to the best of my abilities and adding in feedback from the community.

Many tutorials throw everything at you all at once.

I plan to go into detail on the topic(s).

You will learn slowly, but by the time you complete each part, I hope I will have been able to teach you more than what you would have learned reading the same content somewhere else.

What This Tutorial IsThis tutorial is designed in a way that you will learn the basics of programming in Python.

Many things I will teach are transferrable to similar languages, but all of the syntaxes are in Python.

Unlike my Swift course, we will go through some essential apps that do exciting things along the way.

What This Tutorial Isn’tThis tutorial will not teach you how to use Python editors, how to make GUI screens, <insert Python module here>, Jupyter notebooks, data science, or under-the-hood filetypes.

While some of these topics are cool, the scope is way too broad to keep this tutorial short.

The Series ObjectiveThe objective of this entire series isn’t to push out another programmer who barely knows anything; it is to teach new programmers what programming is.

To provide the reader with a better understanding, both from a classroom and practical standpoint, of programming in general.

To give new programmers a better understanding of the concepts that will help them through interviews and throughout their career.

This tutorial assumes you know:What a computer isWhat programs areEnough logic to understand what the answer to 2 + 2 isEnough logic to realize if there is milk in your refrigeratorYou can download Python directly from Python.

org; however, there are better ways to download Python for your system.

If you are on a Mac install Python using Homebrew:The installer should be run as your user account, not with elevated permissions as it needs your user account to configure permissions correctlyOnce installed, type the following command to install Python on your system.

brew install pythonThis will install the latest version of Python and all of its dependencies on your computer.

Run the following command to ensure it installed correctly.

python3 –versionIf you are on Windows, you are stuck downloading from the website, download the setup program, and install it using the default options.

If you are using Ubuntu, you can install the latest version using the following command.

sudo apt install python3This will install Python 3.


7 on Ubuntu 18.

04, but it should be sufficient enough for this tutorial.

On Centos, you’ll need to install two packages.

sudo yum install epel-releasesudo yum install python36This will install Python 3.


3, which again, should be good enough.

On RHEL 7, you’ll need to enable software collections before you can install Python 3.

sudo subscription-manager repos –enable rhel-server-rhscl-7-rpmssudo yum install rh-python36On RHEL 8 and Fedora, you can install the Python application stream.

sudo yum module install python3You might want to pick up an editor.

Visual Studio Code does a great job with Python, but if you want a full-fledged python IDE, I’d suggest JetBrains PyCharm Community Edition.

If you go with the latter, start a new project, choose Pure Python as the project type, and select Python 3.

x as the Base Interpreter to follow along.

Both of these are available for Mac, Windows, and Linux.

Photo by Patrick Lindenberg on UnsplashA PrimerYour computer has a special place to hold things that are used by programs.

It’s called memory, more technically Random Access Memory, or just RAM.

Random Access Memory is laid out in blocks, think of Lego blocks; each block can hold so much data.

Data is stored in bits or bytes.

8 bits = 1 byte1024 bytes = 1 Kilobyte (KB)1024 KB = 1 Megabyte (MB)1024 MB = 1 Gigabyte (GB), and so on, and so on.

I know your eyes may be watering at this point if you’ve never seen this before, but it’s something you need to understand if you want to learn how to write programs.

We will get into this later on when we talk about types (among other things), but I wanted to talk about this a little before we continue.

Enough with the upfront stuff.

Let’s start!Photo by Franck V.

on UnsplashIn my Swift series, I immediately started talking about variables.

This was easy because it is a compiled language.

Python is different as it is an interpreted language.

In compiled languages, your code is compiled into machine code for the processor you are using.

Interpreted languages are compiled into byte-code that is later converted into machine code when the program is running.

This can lead to optimizations in your code that you’d typically have to think about in other languages.

Because of this, Python is easy to pick up.

VariablesVariables in Python are normally instantiated as they are declared.

To declare a variable means you create a bucket to store a value, to instantiate means to give the variable some value.

Variables store values that can be changed.

When writing variables you should use snake_case.

This means that words in the variable are separated by underscores ( _ ).

Behind the scenes, your computer does a few things when you create a new variable.

First, it looks at a and says “Hey, I’m going to need to ask RAM for memory, let’s see how much.

” Then it looks at = 1 and says “OK, I need to ask RAM for enough memory to hold the number 1.

” It asks RAM for space; if RAM doesn’t see an issue with giving it space, RAM says “Hey I can take it.

” and the value is stored in memory.

The same thing happens with other variables.

If you ever want to change the value of a variable it’s simple.

a = 3name = "John"A neat feature of Python that compiled languages cannot do, is that you can use the variable with a different type.

(More on types later)a = 1a = "Bob"This is still valid code.

If you tried doing this in C or Swift, you’d receive an error, but Python understands how to deal with this.

This does not mean use one variable over and over again, it just provides you this flexibility.

However, armed with this knowledge, understand that using the same variable name can cause problems, which we will go into later in this series.

Ignore this sign…Photo by Michael Mroczek on UnsplashConstantsConstants are values that cannot change.

Before the haters come around to correct me, there are no constants in Python.

If you came here hoping to read differently, I’m sorry but there isn’t.

However, there are ways to visibly show what your constants should be which hints to everyone else that these values should never change.

You can define these by using a variable name in all caps.

MEANING_OF_LIFE = 42FIRST_NAME = "Bob"Constants are used for names, places, things, dates of birth, dates of marriage, dates of death (until the zombie apocalypse comes).

If I were to create a service that talks to a specific database, I might have the server hostname, the database name, and the table names as constants.

At the very least I’d use the server hostname because the only time I’d ever need to change it is if the database was placed on a new server.

Photo by Hunter Haley on UnsplashTypesTypes are essentials of every language, they are the basic building blocks that help you do what programmers do; move bits.

I will cover the basics here and include more advanced types as needed later in the series.

String — a string type can contain any number of characters.

It doesn’t matter if it’s abc or supercalifragilisticexpalidocious.

It’s still just a string.

Starting with Python 3 we can include type annotations to define a variable as a specific type.

Use my_awesome_variable: str to ensure you use the correct type.

Strings typically use 2 bytes of memory.

Integer — an integer type holds numbers.

Something special about Python is that you are not limited to the 2 billion numerical limits in 32-bit machines like you are in compiled programs.

Python allows you to use the full amount of RAM you have available to make a number, which makes it a great language for data science.

This is also a downside because you have no upper limit to crash your program.

Instead, you either crash your program when you hit the memory limit of your hardware or you crash your computer because it ran out of memory.

Don’t try to instantiate the variable with commas for large numbers, instead, use 2000 or for easier reading 2_000.

32-bit programs store integers in 32 / 8 = 4 bytes, and 64-bit programs store integers in 64 / 8 = 8 bytes.

Float — Floats are essentially decimal numbers.

Unlike other languages that include Doubles which provide more precision, floats in Python act like doubles in that you use a lot of memory to gain precision with a float.

It does cut off, but you’ll lose interest in knowing the full value of the float before you get to that point.

To the best of my knowledge, floats are always represented as 64 bits or 8 bytes.

Bool — Booleans are basically True or False, 1 or 0.

They were named after George Boole who came up with them.

Booleans only take up 1 byte of memory.

Comments — Comments aren’t really a type but they do help you understand what is going on in your code.

You don’t want to write code, come back to it a year later and not know what it’s doing.

Comments help you out.

There are two types of comments that I will cover here, single line comments and multi-line comments.

# This is a single line commentmy_variable = "This is code that will run" # this is a comment too"""This is amultilinecomment!!"""my_second_variable = this is more code that will execute'''This is anothermultiline commentusing single quotes'''SummarySo today you took your first steps, you learned a little about how programs put variables and constants in memory and what a few of those things are and how much space they use up.

It’s slow learning, but we are making progress.

There are a lot of tutorials out there that are much faster than I will be, but I want to make sure you understand the how and the why.

What’s Next?Next up in the series we will go over reference types, pointers, and collection types.

I am not affiliated with Python Software Foundation (PSF), Microsoft, or JetBrains.

All recommendations are my own.

The links are being provided as a convenience and for informational purposes only.


. More details

Leave a Reply