LearnPython
Everything you need to know about learning Python is here. This is a catalog that teaches you how to code in Python. In fact, everything you need to know from beginner to intermediate is here. Even experts could learn from this!
Install / Use
/learn @MatthewTamYT/LearnPythonREADME
😎 Learn Python 😎
This is a great place to start learning Python. Full credit goes to Eric Matthes.
Table of Contents
- 1 Getting Started
1 GETTING STARTED
In this chapter, you’ll run your first Python program, hello_world.py. First, you’ll need to check whether a recent version of Python is installed on your computer; if it isn’t, you’ll install it. You’ll also install a text editor to work with your Python programs. Text editors recognize Python code and highlight sections as you write, making it easy to understand your code’s structure.
Setting Up Your Programming Environment
Python differs slightly on different operating systems, so you’ll need to keep a few considerations in mind. In the following sections, we’ll make sure Python is set up correctly on your system.
Python Versions
Every programming language evolves as new ideas and technologies emerge, and the developers of Python have continually made the language more versatile and powerful. As of this writing, the latest version is Python 3.7, but everything in this book should run on Python 3.6 or later. In this section, we’ll find out if Python is already installed on your system and whether you need to install a newer version. Appendix A contains a comprehensive guide to installing the latest version of Python on each major operating system as well. Some old Python projects still use Python 2, but you should use Python 3. If Python 2 is installed on your system, it’s probably there to support some older programs that your system needs. We’ll leave this installation as is, and make sure you have a more recent version to work with.
Running Snippets of Python Code
You can run Python’s interpreter in a terminal window, allowing you to try bits of Python code without having to save and run an entire program. Throughout this book, you’ll see code snippets that look like this:
➊ >>> print("Hello Python interpreter!")
Hello Python interpreter!
The >>> prompt indicates that you should be using the terminal window, and the bold text is the code you should type in and then execute by pressing ENTER. Most of the examples in the book are small, self-contained programs that you’ll run from your text editor rather than the terminal, because you’ll write most of your code in the text editor. But sometimes basic concepts will be shown in a series of snippets run through a Python terminal session to demonstrate particular concepts more efficiently. When you see three angle brackets in a code listing ➊, you’re looking at code and output from a terminal session. We’ll try coding in the interpreter on your system in a moment. We’ll also use a text editor to create a simple program called Hello World! that has become a staple of learning to program. There’s a long-held tradition in the programming world that printing a Hello world! message to the screen as your first program in a new language will bring you good luck. Such a simple program serves a very real purpose. If it runs correctly on your system, any Python program you write should work as well.
About the Sublime Text Editor
Sublime Text is a simple text editor that can be installed on all modern operating systems. Sublime Text lets you run almost all of your programs directly from the editor instead of through a terminal. Your code runs in a terminal session embedded in the Sublime Text window, which makes it easy to see the output. Sublime Text is a beginner-friendly editor, but many professional programmers use it as well. If you become comfortable using it while learning Python, you can continue using it as you progress to larger and more complicated projects. Sublime Text has a very liberal licensing policy: you can use the editor free of charge as long as you want, but the developers request that you purchase a license if you like it and want to keep using it. Appendix B provides information on other text editors. If you’re curious about the other options, you might want to skim that appendix at this point. If you want to begin programming quickly, you can use Sublime Text to start and consider other editors once you’ve gained some experience as a programmer. In this chapter, I’ll walk you through installing Sublime Text on your operating system.
Python on Different Operating Systems
Python is a cross-platform programming language, which means it runs on all the major operating systems. Any Python program you write should run on any modern computer that has Python installed. However, the methods for setting up Python on different operating systems vary slightly. In this section, you’ll learn how to set up Python on your system. You’ll first check whether a recent version of Python is installed on your system and install it if it’s not. Then you’ll install Sublime Text. These are the only two steps that are different for each operating system. In the sections that follow, you’ll run the Hello World! program and troubleshoot anything that didn’t work. I’ll walk you through this process for each operating system, so you’ll have a beginner-friendly Python programming environment.
Python on Windows
Windows doesn’t always come with Python, so you’ll probably need to install it, and then install Sublime Text.
Installing Python
First, check whether Python is installed on your system. Open a command window by entering command into the Start menu or by holding down the SHIFT key while right-clicking on your desktop and selecting Open command window here from the menu. In the terminal window, enter python in lowercase. If you get a Python prompt (>>>) in response, Python is installed on your system. If you see an error message telling you that python is not a recognized command, Python isn’t installed. In that case, or if you see a version of Python earlier than Python 3.6, you need to download a Python installer for Windows. Go to https://python.org/ and hover over the Downloads link. You should see a button for downloading the latest version of Python. Click the button, which should automatically start downloading the correct installer for your system. After you’ve downloaded the file, run the installer. Make sure you select the option Add Python to PATH, which will make it easier to configure your system correctly.
Running Python in a Terminal Session
Open a command window and enter python in lowercase. You should see a Python prompt (>>>), which means Windows has found the version of Python you just installed.
C:\> python
Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
NOTE: If you don’t see this output or something similar, see the more detailed setup instructions in Appendix A.
Enter the following line in your Python session, and make sure you see the output Hello Python interpreter!
>>> print("Hello Python interpreter!")
Hello Python interpreter!
>>>
Any time you want to run a snippet of Python code, open a command window and start a Python terminal session. To close the terminal session, press CTRL-Z and then press ENTER, or enter the command exit().
Installing Sublime Text
You can download an installer for Sublime Text at https://sublimetext.com/. Click the download link and look for a Windows installer. After downloading the installer, run the installer and accept all of its defaults.
Python on macOS
Python is already installed on most macOS systems, but it’s most likely an outdated version that you won’t want to learn on. In this section, you’ll install the latest version of Python, and then you’ll install Sublime Text and make sure it’s configured correctly.
Checking Whether Python 3 Is Installed
Open a terminal window by going to Applications ▸ Utilities ▸ Terminal. You can also press command-spacebar, type terminal, and then press ENTER. To see which version of Python is installed, enter python with a lowercase p—this also starts the Python interpreter within the terminal, allowing you to enter Python commands. You should see output telling you which Python version is installed on your system and a >>> prompt where you can start entering Python commands, like this:
$ python
Python 2.7.15 (default, Aug 17 2018, 22:39:05)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>>
This output indicates that Python 2.7.15 is currently the default version installed on this computer. Once you’ve seen this output, press CTRL-D or enter exit() to leave the Python prompt and return to a terminal prompt. To check whether you have Python 3 installed, enter the command python3. You’ll probably get an error message, meaning you don’t have any versions of Python 3 installed. If the output shows you have Python 3.6 or a later version installed, you can skip ahead to “Running Python in a Terminal Session”. If Python 3 isn’t installed by default, you’ll need to install it manually. Note that whenever you see the python command in this book, you need to use the python3 command instead to make sure you’re using Python 3, not Python 2; they differ significantly enough that you’ll run into trouble trying to run the code in this book using Python 2. If you see any version earlier than Python 3.6, follow the instructions in the next section to install the latest version.
Installing the Latest Version of Python
You can find a Python installer for your system at https://python.org/. Hover over the Download link, and you should see a button for downloading the latest Python version. Click the button, which should automatically start downloading the correct installer for your system. After the file downloads, run the installer. When you’re finished, enter the following at a terminal prompt:
$ python3 --version
Python
Security Score
Audited on Dec 26, 2024
