Wikipedia does a great job of describing about Python in a few words. So, I'll just copy-paste relevant information here:
Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. Python is often described as a "batteries included" language due to its comprehensive standard library.
As of December 2020 Python ranked third in TIOBE’s index of most popular programming languages, behind
See also docs.python: General Python FAQ for answers to questions like "What is Python?", "What is Python good for?", "Why is it called Python?", etc.
On modern Linux distributions, you are likely to find Python already installed. It may be a few versions behind, but should work just fine for most of the topics covered in this book. To get the exact version used here, visit Python downloads page and install using the appropriate source for your operating system. Should you face any issues in installing, search online for a solution. Yes, that is something I expect you should be able to do as a prerequisite for this book, i.e. you should have prior experience with basic programming and computer usage.
See docs.python: What's New to track changes across versions. As mentioned in the Preface chapter, 3.9.0 is the version used in this book.
In case you are facing installation issues, or do not want to (or cannot) install Python on your computer for some reason, there are plenty of options to execute Python programs using online tools. Some of the popular ones are listed below:
- Repl.it — Interactive playground. Code, collaborate, compile, run, share, and deploy Python and more online from your browser
- Pythontutor — Visualize code execution, also has example codes and ability to share sessions
- PythonAnywhere — Host, run, and code Python in the cloud
It is customary to start learning a new programming language by printing a simple phrase. Create a new directory, say
Python/programs for this book. Then, create a plain text file named
hello.py with your favorite text editor and type the following piece of code.
# hello.py print('*************') print('Hello there!') print('*************')
If you are familiar with using command line on a Unix-like system, run the script as shown below. Other options to execute a Python program will be discussed in the next section.
$ python3.9 hello.py ************* Hello there! *************
A few things to note here. The first line is a comment, used here to indicate the name of the Python program.
print() is a built-in function, which can be used without having to load some library. A single string argument has been used for each of the three invocations.
print() automatically appends a newline character by default. The program ran without a compilation step. As quoted earlier, Python is an interpreted language. More details will be discussed in later chapters.
All the Python programs discussed in this book, along with related text files, can be accessed from my GitHub repo learnbyexample: 100_page_python_intro. However, I highly recommend typing the programs manually by yourself.
An integrated development environment (IDE) might suit you better if you are not comfortable with the command line. IDE provides features likes debugging, syntax highlighting, autocompletion, code refactoring and so on. They also help in setting up virtual environment to manage different versions of Python and modules (more on that later).
If you install Python on Windows, it will automatically include IDLE, an IDE built using Python's
tkinter module. On Linux, see if you already have the program
idle3.9. Otherwise you may have to install it separately, for example,
sudo apt install idle-python3.9 on Ubuntu.
When you open IDLE, you'll get a Python shell (discussed in the next section). For now, click the New File option under File menu to open a text editor. Type the short program
hello.py discussed in the previous section. After saving the code, press F5 to run it. You'll see the results in the shell window.
Screenshots from the text editor and the Python shell are shown below.
Popular alternatives to IDLE are listed below:
- Thonny — Python IDE for beginners, lots of handy features like viewing variables, debugger, step through, highlight syntax errors, name completion, etc
- Pycharm — smart code completion, code inspections, on-the-fly error highlighting and quick-fixes, automated code refactorings, rich navigation capabilities, support for frameworks, etc
- Spyder — typically used for scientific computing
- Jupyter — web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text
- VSCodium — community-driven, freely-licensed binary distribution of VSCode
- Vim, Emacs, Geany, Gedit — text editors with support for syntax highlighting and more
One of the best features of Python is the interactive shell. Such shells are also referred to as REPL, which is an abbreviation for Read Evaluate Print Loop. The Python REPL makes it easy for beginners to try out code snippets for learning purposes. Beyond learning, it is also useful for developing a program in small steps, debugging a large program by trying out few lines of code at a time and so on. REPL will be used frequently in this book to show code snippets.
When you launch Python from the command line, or open IDLE, you get a shell that is ready for user input after the
$ python3.9 Python 3.9.0 (default, Dec 2 2020, 10:42:13) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Try the below instructions. The first one displays a greeting using the
print() function. Then, a user defined variable is used to store a string value. To display the value, you can either use
print() again or just type the variable name. Expression results are immediately displayed in the shell. Name of a variable by itself is a valid expression. This behavior is unique to the REPL and an expression by itself won't display anything when used inside a script.
>>> print('have a nice day') have a nice day >>> username = 'learnbyexample' >>> print(username) learnbyexample # use # to start a single line comment # note that string representation is shown instead of actual value # details will be discussed later >>> username 'learnbyexample' # use exit() to close the shell, can also use Ctrl+D shortcut >>> exit()
I'll stress again the importance of following along the code snippets by manually typing them on your computer. Programming requires hands-on experience too, reading alone isn't enough. As an analogy, can you learn to drive a car by just reading about it? Since one of the prerequisite is that you should already be familiar with programming basics, I'll extend the analogy to learning to drive a different car model. Or, perhaps a different vehicle such as a truck or a bus might be more appropriate here.
Depending on the command line shell you are using, you might have the
readlinelibrary that makes it easier to use the REPL. For example,
downarrow keys to browse code history, re-execute them (after editing if necessary), search history, autocomplete based on first few characters and so on. See wikipedia: GNU readline and wiki.archlinux: readline for more information.
You can use
python3.9 -qto avoid version and copyright messages when you start an interactive shell. Use
python3.9 -hor visit docs.python: Command line and environment for documentation on cli options.
The offical Python website has an extensive documentation located at https://docs.python.org/3/. This includes a tutorial, which is much more comprehensive than the contents presented in this book, several guides for specific modules like
argparse and various other information.
Here's a couple of annotated screenshots:
Python provides a
help() function, which is quite handy to use from the REPL. If you type
help(print) and press the Enter key, you'll get a screen as shown below. If you are using IDLE, the output would be displayed on the same screen. Otherwise, the content might be shown on a different screen depending on your
pager settings. Typically, pressing the
q key will quit the
pager and get you back to the shell.
Quotes are necessary, for example
help('del'), if the topic you are looking for isn't an object.
If you get stuck with a problem, there are several ways to get it resolved. For example:
- read/search for that particular topic from documentation/books/tutorials/etc
- reduce the code as much as possible so that you are left with minimal code necessary to reproduce the issue
- talk about the problem with a friend/colleague/inanimate-objects/etc (see Rubber duck debugging)
- search about the problem online
You can also ask for help on forums. Make sure to read the instructions provided by the respective forums before asking a question. See also how to ask smart-questions. Here's some forums you can use:
- /r/learnpython and /r/learnprogramming/ — beginner friendly
- python-forum — dedicated Python forum, encourages back and forth discussions based on the topic of the thread
- /r/Python/ — general Python discussion
- stackoverflow: python tag
The Debugging chapter will discuss more on this topic.