Python Tutorial: Introduction to Python Programming

Python Tutorial: Introduction to Python Programming

A detailed overview of the Python Programming language, how to install it on your machine and to how to run programs.

Are you completely new to python or programming? or you've gained some programming knowledge and you will like to learn how the python programming language works, or let's say for some other reasons you want to learn to write python codes, here is a good place to start your adventure.

In today's article and a few more series of articles, I'll go over most of the important topics in Python, from the fundamentals to more complex concepts.This article will feature practice examples, exercises, and quizzes to assist you in developing the muscle memory required to retain and apply the concepts you'll learn here in future projects.

Today's article will focus primarily on a detailed introduction to python, and some of the topics includes what python is, why it was created, who created it, why it is used by so many developers and why it is a very good language to learn, it's strength and downsides, it's use cases, how python run programs, how you can run programs, as well as it's installation and a simple python script.

Let's get right into it.

What is Python?

Python is a general-purpose, interpreted, and high-level language. It is easy to learn yet so powerful.

Python as an interpreted language means that python programs are executed directly by what is referred to as an interpreter. The interpreter abstracts away all the underlying compilation process, allowing for speedier testing and development. In lower level languages like C, these compilation processes are usually done step by step before an executable file is generated. The interpreter will be explained more in-depth later in this article.

Python is an high-level language because it supports simplicity and understandibility. Python is designed in a way that is easy to read. This can make Python codes easier to write and maintain as well as debug, especially for beginners or developers with little programming experience.

In short Python was created with simplicity in mind.

Who Developed Python?

Python was developed by Guido van Rossum between the 1980 and 1991 as a successor to the ABC programming language. It's first stable release was released in 1991 as python 0.9.0.

Some of the programming languages that influenced python includes ABC, Ada, ALGOL 68, APL, C, C++, CLU, Dylan, Haskell, Icon, and some other languages.

Python has influenced languages such as Ruby, Swift, Go, CoffeScript and Julia.

Why Is Python Used by many people?

As a beginner, you should know why you want to learn a language. Sometimes, it might be a bad idea to jump into learning a programming language without thorough research on why you should learn it.

Is it because of the ease that comes with learning it? or because it has a huge community ready to provide resources and support during the learning process, or ofcourse you want to make money with it. Whichever reason it is, having a reason will go a really long way.

Looking at Python, all the 3 reasons listed above are applicable. I'll explain the already listed reasons and some other reasons why people use python, in details.

Ease of use:

Python was designed with simplicity in mind, as already stated above. It's easy for beginners to approach python since it's syntax are readable just like English. Does it mean Python is made just for simple task? The short answer is No. Python is not just for simple tasks. Rather, it makes tasks simple by its ease of use and flexibility. Python has a simple feature set, but it also allows for building more complex softwares as needed.

Support and community:

There's a large community of python developers around the world. As a result, you will receive the best possible assistance both when you first begin learning Python and as you grow. There is no reason to feel isolated.

Developer productivity:

Python allows developers to get more complicated tasks done in lesser time with fewer lines of codes. It allows for a great development speed because of it's simple syntax, dynamic typing, built-in tools and absence of manual compilation steps.

Portability:

A Python program that works well without any error on a window OS will most likely work without any errors on a Linux OS. It's just a matter of copying codes between these machines. The efficiency of programs written in low-level languages like C depends on the operating system on which they are run.

Libraries support:

Python comes with a large collection of libraries often regarded as standard libraries. Python also support a vast collection of third-party libraries. These libraries provide functionalities which makes it possible to use Python to solve a wide variety of tasks such as web development, game development, data analysis, AI and machine learning, to name a few.

What are the Advantages of using Python?

This is one of the questions often asked by developers. It is worth knowing some of python's features which makes it stands out.

  1. It's free.

  2. It's object-oriented.

  3. It's portable.

  4. It's easy to learn and write.

  5. It's interpreted.

  6. It supports automatic memory management.

  7. It can be easily integrated.

  8. It's interactive.

What's the downside of using Python?

The only downside of using python is the execution speed. Python is not as fast as compiled languages like C or C++. But this is not always a problem. Let's talk about this.

Whenever you compile a program written in a lower level language, let's say C, the executable file is always in a binary form containing 0s and 1s. But in Python, the standard implementation of python known as CPython, compiles source codes to an intermediate form called byte codes and then interprets the byte code.

So, because Python programs are not compiled all the way to binary machine code, some programs will run slowly.

Why is this always not a problem? The speed of your programs depends on the kind of programs you're writing and moreover, a lot of optimization have been done to make python programs run at C speed.

What can I do with Python?

Python being a general-purpose language, the possibilities of what can be done with it is endless. Listed below are some categories of real-world tasks that can be completed with python

  1. Web development

  2. Software programming

  3. Scripting

  4. Component Integration

  5. Database programming

  6. Game programming

  7. Scientific programming

Some of the Companies that Use Python?

Python is very high in demand, and some of the major companies are seeking experienced python developers to develop softwares, build websites and applications, or to work with machine learning and AI tools. Python is a general-purpose language, as such, the possibilities of what can be done with it is endless. Listed below are some of the companies that make use of python

  1. Google

  2. YouTube

  3. Intel

  4. NASA

  5. PayPal

  6. Facebook

  7. IBM

  8. Amazon

  9. Netflix

  10. Pinterest

  11. iRobot

  12. JPMorgan Chase

  13. Uber and many more

Careers with Python?

Learning Python is a good choice of career.

Gaining enough knowledge on how the python programming language works, automatically opens up countless opportunities, that can help improve your standard of living. Listed below are some of the career options where Python is a key skill:

  1. Web developer

  2. Full stack developer

  3. Python developer

  4. Game developer

  5. Data analyst

  6. Data scientist

  7. Machine learning Engineer

What's an Interpreter?

An interpreter is a software package that executes other programs. As stated earlier, the python interpreter abstracts away all the compilation process, allowing you to focus mainly on writing codes.

After installing python, a few other packages gets installed with it, on your machine. These includes the interpreter and a large collection of support libraries which I've talked about earlier.

Setting up your Programming environment

Before you can start using Python on your machine, it must be properly installed, and a more recent version must be installed. Python's most recent version is 3.11.3.

Python, as a cross-platform programming language, operates on nearly all, if not all, operating systems. Does this imply that Python is available on all operating systems? The short answer is No. The long answer is that some operating systems, such as MacOS and Linux, come with Python installed, but others, such as Windows, do not.

In that case, I'll show you how to setup python on these 3 major operating systems; windows, Mac, and Linux.

Python on Windows

Python is not always included with Windows. You must download and install it.

But, before you proceed with the download, you should check if at all python is installed. Open the command window.

To do this,

  1. Go to the start menu.

  2. Type in cmd and open the command prompt.

  3. In the terminal, type python --version.

If you get an error, python isn't installed. If it doesn't return an error, you also want to check if the installed version is still supported. If you see a version earlier than python 3.x.x, you need to download a more recent version.

To download the latest version, Go to https://python.org/downloads/

You should see a button for downloading the latest version of Python. Click the button, and the download will start automatically.

After the download is complete, double click on the download file to start the installation. Make sure you select the option Add Python to PATH, which will make it easier to configure your system correctly.

Python on macOS

Python is pre-installed on most macOS systems, however it is most likely an outdated version that is unsuitable for learning.

You can as well check the version first before downloading it.

To download the latest version of python on macOS, Go to https://python.org/downloads/

You should see a button for downloading the latest version of Python. Click the button, and the download will start automatically.

After the download is complete, double click on the download file to start the installation. Make sure you select the option Add Python to PATH which will make it easier to configure your system correctly.

Python on Linux

Since Linux systems are designed for programming, python is already installed on it. But you should check if it's python3.

Open the terminal window, let's say you're using Ubuntu, type in python3. You should get something like this.

From the screenshot above, you can see i have python 3.8.10 installed and this is enough to learn with.

How To Run Programs

At this point, I assume that you already have Python installed, and it's time to start writing some Python programs.

Python applications can be run in a variety of ways. In this section of the article, I will walk you through some of the most common ways to run Python programs. I will describe what it means to run Python interactively, how to build and run Python scripts from the terminal, how to use Python's IDLE, and how to use graphical user interfaces (GUIs).

The Interactive Prompt

The simplest way to run python is by using the interactive prompt. The interactive prompt looks something like this

It allows you to type one line of code at a time. So how do you start the interactive prompt? It's simple. But you should know that the way you start the interactive prompt differs slightly with operating systems. The most common way is by typing python on the command prompt.

On windows, type python to start the interactive prompt.

It's thesame for macOS and Linux, typing python3 will start the interactive prompt.

Let's talk on how you can run programs with the interactive prompt

>>> indicates that you should type in your codes here. When working interactively, you use the Enter Key to display the results of what you've typed and also to go to a new line.

From the above image, you can see that the interactive prompt started when I typed python. I used python because I'm working on a windows OS.

You don't need to worry about the print statement for now. All you need to know is that it prints out values to the terminal.

The first print statement prints out the string Hello, world! the terminal. We'll talk more on strings in coming lessons.

The result of what I typed is printed below the >>> sign.

Next, I assigned the string David to the variable, name.

Notice that I also performed a basic arithmetic in the interactive prompt.

The python's interactive prompt is called an interactive prompt for a reason. You get your results on the go. This is what makes it interactive.

There are few things to note when using the interactive prompt

  • The interactive prompt doesn't save your code in a file. Meaning once you close the interactive prompt, you can't access the codes you've written already. This is why the interactive prompt is a perfect place to experiment with the language.

  • You'll probably want to run a piece of code without having to create a new file, this is a good situation to use the interactive prompt.

  • The print statement is not necessarily needed. Since the interactive prompt runs and prints the result of an expression automatically, you do not need to use the print statement when working with it.

  • The interactive prompt runs one statement at a time.

  • You can't copy and paste multiple lines of code into the interactive prompt. Trying this will return a SyntaxError. There's a catch, if there's a blank line after each statement, it will run without any error.

  • To exit the the interactive prompt, Type in exit().

The IDLE User Interface

Up until this moment, you've been working from the terminal and it will be nice if you can work with something a bit more visual.

IDLE is a standard environment from writing python programs. It comes installed with python and it is usually refered to as integrated development environment.

The IDLE is a GUI that lets you write, edit, open and debug python programs. It looks something like this

There are few things to know about the IDLE

  • You can save your codes into a file

  • You must add .py when saving your files.

  • There's a button to run programs after they are written.

  • IDLE is a python program that uses the standard library's tkinter GUI toolkit.

  • You can customize the IDLE.

Text Editors, Command lines and System files

The interactive prompt is a good environment to write codes, but it is only appropriate for tiny programs such as a fast experiment or test. When it comes to building huge programs, using the interactive prompt becomes a major issue. There is a better way to do this.

Using text editors like vi, emacs, sublime text, VSCode and others, you can write programs in files and run then on the command line.

Let's see how this works.

One of my favorite text editor is vi. It's a shell-based editor used on the command. I have a file created already, and this file simply prints out Hello, world to the command line, and I have this program saved in a file named hello.py.

You can run this file by typing python hello.py. Make sure you're in the right directory.

Here are a few things you should know

  • The files where programs are kept are sometimes called modules.

  • You can access these files anytime you want.

  • Make sure to type the name the file with the correct extension. Use python hello.py not python hello

  • You will need to download and install a text editor yourself, unless you are working on a UNIX-like environment (e.g. Linux), in which case vi is already installed.

Creating UNIX Executable Scripts

In Linux, or any UNIX-like environment, you can turn python files into executable scripts.

Executable scripts has two main attributes which are

  1. The first line usually starts with #! commonly referred to as Hashbang or Shebang followed by the path to the python interpreter on your machine. I have a file created with the following lines of code in it
#!/usr/bin/python3
print('Hello, world')
  1. These files are usually granted "executable" permissions. The chmodcommand in Linux is used to set permissions for directories and files. I have a file named hello.py already.

    Typing chmod +x hello.py will make the file an executable one. Make sure you're in the right directory.

The x means execute. Other permission mode are r for read and w for write.

To run this executable file, simply type, ./hello.py or ./hello

Final Thoughts

Python is a really good choice of Programming language to learn, and in this article, I've given a well-detailed introduction to Python. I've worked you through the overview of python, how programs are run, how to install and Setup python, as well as how to run programs.

In the course of writing this article, I've learned a lot and I hope you do too.

That's it for this article. If you have any question or you think there's something you want me to add to this article or upcoming ones, please drop them in the comment section below.

And If you like this article, please follow for more and don't forget to share with others who might find it helpful.

Finally, I'd like to connect with you on LinkedIn | Twitter | GitHub | YouTube.