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.
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.
- Head over to https://www.python.org/downloads/.
- Press the Download button to download the latest stable version of Python for your operating system.
- Once the download is complete, run it.
- An installation dialogue will appear guiding you through the installation of Python. Continue with the recommended configurations.
- Once installation completes, Python is capable of being executed on your machine.
At this point, you have successfully set up Python on your computer. Everything is ready — now you just need to work with it.
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.
However, it isn't meant to entertain large Python scripts. For this we have something else known as the 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.
- Once again, open up IDLE.
- Go to File → New File. This opens up the editor window where you can write a complete Python script, and save it as a
- Type in the same
print('Hello World!')statement and then save this file as
first-script.pyon your desktop.
- Now go to Run → Run Module, or simply press the
F5key. This will execute the script and open up the shell where all the output of the program is shown.
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 rightaway 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 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.
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.
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.