Jump to content

Python Concepts/Introduction and Setup

From Wikiversity

Objectives

[edit | edit source]
  • Install Python on your computer.
  • Learn how to run Python from the command line.
  • Learn how to make and run a Python script.
  • (Optional): Learn how to use IDLE.

Lesson

[edit | edit source]

Python 3 vs Python 2

[edit | edit source]

There are two major versions of Python, Python 3 (newer) and Python 2 (older). You should consider them as separate languages, because code from Python 2 cannot be used in Python 3, and vise versa. This course teaches Python 3 only. In 2020, Python 2 reached end-of-life and support has stopped completely, so there is no point in learning it.[1] It is important to make sure you are installing and using Python 3 instead. However, Python 2 sometimes comes preinstalled on certain operating systems. Installation instructions below will take this into account and guide you through using the correct version of Python. If you end up having both versions installed on your computer, you will need to use the python3 command anywhere the course tells you to use the python command (as python would run Python 2 in that case).

Installing Python

[edit | edit source]

Before you can enter into the magical world of Python, you'll need to have Python installed on your computer. It may be possible, depending on your operating system, that Python is already installed on your computer. The instructions below will help you determine whether this is the case. Please select the installation instructions for your operating system, then return to this lesson once you have installed Python:

Online Interpreter

[edit | edit source]

Downloading and installing Python onto your computer is the recommended way to run your code. However, if you feel concerned about downloading Python onto your computer or you just can't do that, then you can use a website to develop and run your code. An excellent free website to use is https://www.online-python.com/, which will let you follow along with most of this course without installing Python. However, this approach will eventually hinder you from learning the more advanced Python topics later on in the resource. Installation will eventually be required.

Checkpoint: Before continuing, you should have Python installed, or the online interpreter bookmarked in your web browser.

The Interactive Shell

[edit | edit source]

The shell, terminal, command prompt, command line, or whatever you like to personally call it, is one of the mediums of communication between you and the Python interpreter. Through out much of this resource, you'll see examples of Python code being used in a shell. Essentially, the shell lets you type out Python code and run it directly, without creating a file first.

Before we go ahead, open interactive python in your operating system's shell. Simply open a new shell (command prompt, terminal, etc.) and type python into it. (Remember, if you're using a machine that had Python 2 preinstalled on it, you'll need to specifically type python3 instead.) Congratulations, you now have a working Python interpreter. This is what the shell may look like when you run interactive Python on your computer:

Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 

You may not get the first couple of lines exactly as shown above, since everyone has different versions, computers, and builds. As you've probably guessed, the interpreter tells us the Python version, date of build, the compiler used to build it, the computer's architecture, and the operating system. This information is important for debugging and is useful for checking Python's version. The most important thing that you'll need to know are those three greater-than signs (>>>). They symbolize a prompt that asks you for some input. Since this prompt is shown in examples, you'll need to remember that the prompt isn't some special keyword or thing you'd need to type on a daily bias. Here's an example with the prompt displayed only.

Try typing the following into your prompt. (Don't type the >>>. That part is already there.)

>>> print("Hello, world!")
Hello, world!

As you can see, a newline without the prompt is output. In this case, the print() function printed the text to the screen. (You just wrote your first line of Python code!) Another important thing to know is that the prompt changes to three periods (...) if you enter a command with indentations (the Python interpreter is expecting more input). Overall, you'll need to be careful not to mistake the prompt for actual Python code. You will learn when to use indentations later in this course, but for now, you don't need to worry about them.

>>> if True:
...     print("Hello, world!")
...
Hello, world!

To exit the interactive shell, type:

>>> exit()

Python Script

[edit | edit source]

You will eventually find it tedious to retype programs into your shell. Like other interpreted programming languages, you can store and execute Python code from a file. There are two main types of Python files, script files and bytecode files. Script files are simple plain text files that you can create and edit with just a simple plain text editor. The other type of file, bytecode files, are compiled CPython files. These files are not meant to be edited. Don't worry about this type of file; it will be discussed later on in this course.

A Python script file name ends with the extension .py and will be executable when you execute the script by itself on most operating systems. Create a file called hello.py and write the following into it:

print("Hello world, from a Python script!")
input("Type anything then press enter: ")

Now that you have created your script, it's time to run it. In your shell (command prompt, terminal, etc.) change directory to the folder where your hello.py file is. (If you do not know how to use the cd command to do that, refer to this article.) Then, once your shell is in the folder with the script, type in python hello.py. (Again, if you're using a machine that had Python 2 preinstalled on it, you'll need to type python3 hello.py.)

You'll notice that the python command did not open the interactive shell this time around, but instead executed the contents of hello.py. The script should print Hello world, from a Python script! and Type anything then press enter: , then it should let you type until you press the enter key. Congratulations, you got your first Python script to work.

This is an example of IDLE 3.4.3 running on Windows XP.

IDLE, or Integrated Development and Learning Environment, is the default IDE that comes bundled when you install CPython. IDLE is an alternative way to write and run Python scripts in the same program, rather than using a text editor and a shell separately. IDLE is merely a convenience and not required for this course -- if you prefer editing and running scripts like you did with hello.py, you can skip this section.

On Windows, IDLE can be accessed by right clicking the Python script and then by clicking on Edit with IDLE. On all operating systems, the program can be found in the start menu (or the Launchpad for Mac). When you run IDLE, you will notice Python's interactive shell. You can open or create a .py file in the menu, and run the script directly from within the program by pressing F5 or selecting "Run" from the menu.

When working with IDLE, you'll notice a couple things. First, you'll notice that your code is colored. By default, strings are green, comment red, built-in functions purple, etc. This allows you to see certain parts of your code. This gives you an advantage over mono-colored text. Secondly, you'll notice that IDLE will make automatic indentations when your text is indented. IDLE will also convert presses of the tab key to four spaces.

Although the default settings are great, not all people will like them. Therefore, IDLE is extremely customizable and can be changed as needed. For example, this gives a color-blind person the ability to change the colored text to something more friendly. You can change IDLE's default settings under Options in the menu bar.

Assignments

[edit | edit source]
  • Try out the Python Interpreter. Get the general feeling of using it.
  • Create a Python script. Then, try and run it, errors or not.
  • (Optional): Open up IDLE and play around with it. Use and customize it at your own will.

Checkpoint: Before moving on to the next lesson, you should be comfortable creating .py script files, and running them in your shell or in IDLE.

Completion status: Ready for testing by learners and teachers. Please begin!

Footnotes

[edit | edit source]
  1. For advanced users only: There are options if you don't want to do either of these steps, but be careful. When you right-click your mouse on the desktop or in an explorer window, you'll be able to select a new file to create. If you have experience working with Windows registries you can add a Python file into New. I should warn you now, it can be dangerous editing your registries, so do so at your own risk. If you're still intent on doing this, run the program regedit.exe either from Run... or from the command prompt. When the program opens open My Computer, then open HKEY_CLASSES_ROOT. From here, scroll down until you find .py and open it. Then right-click .py, move the mouse over new and then select Key. Name this Key ShellNew. Now right-click ShellNew and add a New String this time. Name it NullFile and leave its value empty. There, you've done it! You'll need to restart your computer.

References

[edit | edit source]
  1. "Should I use Python 2 or Python 3 for my development activity?". Python.org. Python Software Foundation. Retrieved 2022-06-25.