Python Get Started

Chapter 2 13 mins

Learning outcomes:

  1. Installing Python
  2. IDEs for Python

Introduction

In the previous chapter, we got a brief introduction to the Python programming language, including where its idea sprung up, who invented it, what's its popularity in the dev community today and many more such amazing facts.

Now in this chapter we shall see how to turn our computer machine into something that can understand Python code. This step is necessary before almost all programming languages i.e we need to set up an environment that enables executing scripts written in the programming language.

Specifically, we'll see how to install the Python interpreter along with some features equally useful like the PIP tool, IDLE editor and so on.

Let's begin!

Installing Python

Before we can start working in Python, we first need to install it on our computer. Without installing Python, scripts written in it can't be interpreted, translated into machine language and ultimately executed.

Fortunately, installing Python is very easy, compared to languages like Java, PHP which do require a little bit of grunt work on our side.

Follow the steps below in order to install Python on your specific operating system. We're demonstrating the steps for Windows OS, however they would work equally good on Mac or Linux systems.

  1. Head over to https://www.python.org/downloads/.

  2. Wait a couple of seconds until the page loads, and automatically detects your operating system. After this, the download button will point to the latest stable version of Python specifically for your operating system. Go ahead and press the Download button.

  3. Once the download is complete, run it.

  4. An installation dialogue will appear guiding you through the installation of Python.

    Continue with the recommended configurations by clicking Install Now.

    And don't forget to check the option Add Python 3.9 to PATH. This creates an environment variable in Windows with the name 'python' that points to the Python interpreter on your machine, so that you could easily access the interpreter just by typing in python at the terminal.

    Without adding Python to PATH, you'd need to write the whole path to the interpreter (on your machine) in order to work with Python in the command prompt/terminal.
  5. Once the installation completes, you'll get the following. Just continue by pressing the Close button.

At this point, you have successfully set up Python on your computer. You are a genius!

Everything is ready — now you just need to start working with Python.

IDEs for Python

Although we can write scripts in any programming language in text editors like Notepad, and then run those scripts using the repective interpreter/compiler of the language, almost no one does this; and even shouldn't!

Coding in Notepad is boring, time-consuming and completely inefficient. There is no sort of project management, no sort of structure to our scripts, nothing at all.

Coding is usually done on dedicated software, known as an IDE. IDE stands for Integrated Development Environment, and as the name suggests, is an environment specifically crafted for program development.

Amongst many of its features are code highlighting that allows us to quickly differentiate between different parts of a program; intellisense that automatically understands what we want to type and shows matching suggestions; realtime debugging using breakpoints that allows one to easily spot errors in code and rectify them; and many more.

Different IDEs have different features — some are way too complex for beginners whereas some are way too simple to be even called an IDE.

Nonetheless, it is the developer whose preferences govern the most suitable IDE for him/her — it's really not a good-vs-bad comparison.

By default, Python comes with a lightweight, native IDE for development known as IDLE.

If you installed Python in the default preferences, then you'd surely have IDLE installed on your computer.

Let's open it up and start exploring it!

Go to Start, type in IDLE and run it.

What opens up is known as the shell where you could enter any valid Python code and see its result in situ. The shell window is a really good place to practice Python.

Python IDLE Shell window.
Python IDLE Shell window.

However, it isn't meant to entertain large Python scripts. For this we have something else known as the Editor window.

The Editor window can be opened by going to File → New File.

Python IDLE Editor window.
Python IDLE Editor window.

In the editor window, we can write a whole Python script spanning multiple lines and then get is all executed together.

In this course we'll spend most of the time in the editor, leaving basic practices for the shell.

Now that we have the environment for writing Python, let's write our very first Python code. We'll demonstrate it in both places: shell and the editor.

First Python code in IDLE

For the shell, headover to IDLE and type print() in the shell window that appears.

You get a "Hello World" message printed beneath the print() statement you just wrote. This is the output of the shell.

Now let's write the same Python code in the editor.

  1. Once again, open up IDLE.
  2. Go to File → New File. This opens up the editor window where you can write a complete Python script, and save it as a .py field.
  3. Type in the same print('Hello World!') statement and then save this file as first-script.py on your desktop.
  4. Now go to Run → Run Module, or simply press the F5 key. This will execute the script and open up the shell where all the output of the program is shown.

Congradulations!

You've successfully written your very first Python code and probably without much hassle. You might agree that setting up Python wasn't difficult at all — install it and then start working right away on IDLE.

Although IDLE is definitely good for many applications, at some point you may find it boring, laggy and lacking many features. This is where a more sophisticated IDE can step in...

PyCharm

PyCharm is a pretty well-off IDE for Python, created by JetBrains Inc.

It's graphically more appealing than IDLE and feature-wise far more equipped.

PyCharm comes in two versions — one is a professional trial version, while the other one is a free community edition. The latter is the one we're concerned with.

Head over to https://jetbrains.com/pycharm and download the free community edition IDE for your specific OS. Once you've downloaded the installer, install Python by following the instructions in the installer.

Once you're done, open up PyCharm.

It should look something like this.

PyCharm main page.

Now create a new project.

Projects in PyCharm are simply directories where all the files and data of the project lies.

As you're beginning to learn Python this notion of creating directories for your Python files can be helpful. It'll teach you a bit of project management skills — a must need for complex applications.

Create an empty directory on your desktop and name it Python Learning. Make sure to keep it empty — we are starting from scratch and so it'll be a good idea if our directory also does.

Coming back to PyCharm, create a new project and base it on this newly created directory. Every new file that you create from this point on using PyCharm will go inside this directory — in this way you'll be able to better keep track of your learning process very closely.

IDLE or PyCharm?

It's entirely upto you which IDE you want to go with, which one feels more native to you, which one has all the tools that you need.

IDLE will fulfill most of your needs without any hassle, but as your programs grow it'll become slow and unresponsive. You'll understand this much better when you actually code in IDLE — when you print a lot of stuff in the shell it tends to become laggy.

PyCharm is a really good IDE for beginners. Yes it has a lot of features alien to novice developers, but in general it has an elegant interface, learning which wouldn't take a long time at all.

Let's do it this way — when you want to explore the simple aspects of Python such as arithmetic operations, string manipulation, inspecting data types etc., open up IDLE.

Similarly, when you want to explore the advanced aspects of Python such as iterators, file manipulation, database queries etc., where you know you'll be working with a lot of input, go with PyCharm.

Moving on

At this point, you know what Python is, how to set up its environment and write your own Python code — everything that you need to get started.

Starting from the next chapter, we will explore the bits and pieces of the Python programming language and set you on the road to becoming a professional Python dev.

Let's go!