Chapter 1-
What Is Python and His History and Why Learn Python
What Is Python?
Python, created in 1990 by Guido van Rossu, is a general-purpose, high-level programming language. It has become trendy over the past decade, thanks to its intuitive nature, flexibility, and versatility. Python can be used on a wide variety of operating systems. It’s clean, readable code style makes it relatively beginner-friendly, while not as fast as other languages, such as C++ or JAVA, Python code is often much shorter and simpler than other languages.
Python also supports several packages and modules created by other developers to make the development of Python applications quicker and easier.
Why Learn Python?
There are hundreds of different programming languages out there in the world, with Wikipedia listing over 700 notable languages. Given how many languages you could potentially learn, why learn Python?
Python has seen an explosion in popularity in recent years, driven by several aspects that make it an incredibly versatile and intuitive language. A huge selling point of Python is the cleanliness and readability of its syntax and structure. Commands in Python can often be carried out using simple English keywords, which makes the language much more intuitive than many other languages. Python is also quite versatile in the sense that it supports both structured programming and object-oriented programming approaches. Python even allows the use of certain aspects of functional programming.
Python is supported by many different operating systems, including Windows, Mac, and Linux platforms. Since Python is an interpreted programming language, Python programs can be run on multiple platforms without being recompiled.
Python comes with a robust standard library of modules, functions,
and tools. Every module that comes with Python is a powerful tool you can use without creating additional code. Python comes pre-packaged with modules that assist in the creation of various web services, manipulating strings, and working with the operating system’s interface. Python also makes it easy for users to create their libraries and frameworks, meaning that there is a large, open-source Python community continually creating a wide variety of applications. These applications can significantly speed up/simplify the development of your application.
Despite its simplicity, Python is also sturdy and robust enough to carry out sophisticated scientific and mathematical tasks. Python has been designed with features that drastically simplify the visualization and analysis of data, and Python is also the go-to choice for the creation of machine learning models and artificial intelligence.
For all of these reasons, Python is one of the fastest-growing and most in-demand computer programming skills.
A Note on Python Versions
There are various versions of Python available for use. It is highly recommended that you use version 3.7 or advanced when following along with this book. While Python 2 remains popular in some communities, support for Python 2 will end in 2020, meaning that security issues will not be resolved, and additional improvements won’t be made to it. Once Python 2 is officially retired, only Python 3.5 and advanced will see continued support. Python 2’s syntax is a little different from Python 3’s syntax, and this will not teach Python 2 because its retirement is impending.
Definitions: Interpreter, Terminal, Shell, IDE
Early on in this book, and as you continue to program with Python, you will see many references to concepts like “interpreter,” “terminal,” “shell,” and “IDE.” These concepts can be somewhat confusing for a beginner, so to make things simpler, let’s define these concepts here.
An “interpreter” in the computer science/programming sense is a computer program that can execute code, carrying out the written instructions specified by a programming or scripting language. An interpreter carries out code immediately and directly. In contrast, a “compiler” is a program that translates instructions into efficient machine code. Meanwhile, a “shell” is a wrapper or environment whose primary function is to run other programs and the word shell is often used to refer to the command-line of the OS. The command line takes in commands centered on the name of applications the user wishes to interact with. The interface you see above is an example of the Python shell, and it is running an interpreter.
Python has its shell; an interactive interpreter specialized for running Python commands. It lets the user immediately execute Python code and see the result as soon as the user enters the command. The Python shell that can be accessed through the command-line is an example of a “terminal,” which is simply the environment that allows the user to input text and receives outputs. For the purpose of this book, the terms “shell” and “terminal” may be used interchangeably in reference to an instance of the Python interpreter accessed through the command line.
The Python Interpreter
There are two main ways to work with Python: with the interpreter and command line or with an Integrated Development Environment (IDE).
We will be doing the majority of our programming in an IDE, but first, let’s make sure you understand how to work with Python in the terminal.
Let’s start by opening the terminal/command prompt and checking that Python is installed correctly by just typing the command “python.” If Python is properly installed, the command prompt should transition you to an instance of the Python interpreter/shell. This interpreter allows you to create and run Python code. For instance, if you copied this line of code into the terminal, you’d get “Using the terminal interpreter!” printed back out:
print ("Using the terminal interpreter!")
The command print() is responsible for printing out to the terminal whatever is specified inside the parentheses.
Most programming is done in an IDE, but it is still a good idea to learn how the Python interpreter works because there may be occasions where you may have to do some programming in it. With that in mind, let’s take a few moments to familiarize ourselves with the Python interpreter.
As mentioned, the Python interpreter can typically be invoked from the command line only by entering the command “Python,” or perhaps the specific Python version you want to run:
python3.8
The interpreter can typically be exited with the quit command: exit() - or depending on the version you are running - quit().
The help() command is an incredibly helpful command that you will always want to remember because it shows you all the various commands and functions that you can use in the interpreter.
When you enter a command by hitting the return key, the statement will be evaluated for correct syntax. If there is a syntax error, the error will be displayed.
Python is waiting for a command if you see the “primary prompt,” which is often indicated by the presence of three greater-than signs (>>>). If you are on the second line of an input, these greater than signs will instead be replaced with three periods.
Using an IDE
I wanted to make you aware of the Python interpreter in the terminal’s existence, but most of our programming will be done in an IDE. If you experimented with the terminal a little bit, you’d quickly find a significant disadvantage of using the terminal, and it is that you can’t preserve many lines of code on the same screen. In addition, whenever you enter a line of code, and it contains any errors, a syntax error will be thrown immediately. IDEs make the process of learning a language simpler because they will often highlight syntax errors for you. Other benefits of using an IDE include auto-completion for specific key phrases and functions, more accessible collaboration with other programmers, and the ability to make changes to a script even while an instance of the programming is running.
You can try out the code examples found in this either the terminal or in an IDE. However, most of the examples presented in this will be presented in an IDE. One excellent IDE is PyCharm (
https://www.jetbrains.com/pycharm
), an open-source IDE designed from the ground up for use with Python. PyCharm highlights syntax errors enables easy refactoring/renaming of files and comes with an integrated debugger. PyCharm also has an integrated terminal, and when you run programs in PyCharm, the results of the program’s execution will be displayed in the terminal at the bottom of the IDE.
Using PyCharm
Let’s go over some of the functions in PyCharm in greater detail, so that you are familiar with how to use it.
After installing PyCharm and setting it up for the first time, you may be slightly intimidated by all the options, but don’t worry, you won’t be using most of these options for the exercises in this book.
As you can see in the image above, when you open PyCharm and are confronted with the interface, you can navigate up to the file option in the top left corner. Opening the file drop-down menu will let you either open an existing project or create a new project. Opening an existing project enables you to reopen projects you’ve already started and saved or even open the projects that other people have worked on and which you have downloaded/cloned. For now, just create a new project for the exercises through the “File” option in the top left.
The New Project dialog box may look slightly different depending on which version of PyCharm you are using, but it should ask you to select a project interpreter. The default virtual environment (virtualenv) is beautiful for now, and it should automatically detect your base Python interpreter if it is correctly installed on your computer.
After this, you can create a folder to hold the scripts you create by right-clicking in the project frame and choosing the “New” option from the drop-down menu. To create a new Python script, just right-click on the folder you’ve created and navigate to “New” and then the “Python File” option. Now just enter a name for your new file Python file.
After you create a new Python file, it should automatically open in the editor panel to the right. You can now enter code into the editor. If, for some reason, the editor didn’t automatically open the file, just double click on the file to open it up in the editor.
PyCharm should automatically save changes to the file, which means you don’t need to worry about manually saving them. If for some reason, the file doesn’t auto-save, or you just want to be sure it has saved, you can right-click on the file to be presented with a drop-down menu that should contain the option to save the file. You can also press Ctrl + S to save all the files currently open in PyCharm.
Once you’ve written some code and want to try running it, you can either navigate up to the “Run” tab on the top toolbar and select “Run (Current file name here),” or press Shift + F10. The image above shows a program has finished its run in PyCharm’s compiler. Note that the results of the program are printed to the built-in terminal.