Beginner's Guide to Python Programming Language - Part 1

Many 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 ability and through adding in feedback from the community.

Additionally, tutorials often throw everything at you all at once. I plan to go into detail on each topic. 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 Is

This tutorial is designed in a way that you will learn the basics of programming in Python. Many of the things you learn are transferrable to similar languages, but all of the syntaxes are in Python.

What This Tutorial Isn’t

This tutorial will not teach you how to use Python editors, how to make GUI screens, , 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 Objective

The 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, and 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 is
  • What programs are
  • Enough logic to understand what the answer to 2 + 2 is
  • Enough logic to realize if there is milk in your refrigerator

You 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:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The installer should be run as your user account, not with elevated permissions as it needs your user account to configure permissions correctly

Once installed, type the following command to install Python on your system.

brew install python

This 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 --version

If you are on Windows, you can download the setup program from the website and install it using the default options or you can install it using the chocolatey package manager with the following command.

choco install python

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

sudo apt install python3

This will install Python 3.6.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-release
sudo yum install python36

This will install Python 3.6.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-rpms
sudo yum install rh-python36

On RHEL 8 and Fedora, you can install the Python application stream.

sudo yum module install python3

You 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.

A Primer

Your 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 byte

1024 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!

In 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. These optimizations make Python relatively easy to pick up.

Variables

Variables 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 ( _ ).

a = 1
name = "Bob"
physics_room_number = "123 B"

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 = 3
name = "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 = 1
a = "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…

Constants

Constants 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 = 42
FIRST_NAME = "Bob"

Constants are used for names, places, things, dates of birth, dates of marriage, dates of death (until the zombie apocalypse comes), etc. 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.

Types

Types 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 comment

my_variable = "This is code that will run"  # this is a comment too

"""
This is a
multiline
comment!!
"""

my_second_variable = this is more code that will execute

'''
This is another
multiline comment
using single quotes
'''

Summary

So 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.

Thank you for reading and keep visitting!

#python #tutorial #programming

Beginner's Guide to Python Programming Language - Part 1
19.85 GEEK