Learning the basics of Python shell and writing the first program
In the previous section, we walked through the process of installing Python and setting up a suitable Integrated Development Environment (IDE). With these tools in place, we are now ready to start coding.
Understanding the Python Shell
The Python shell is an interactive interpreter that allows us to run Python code line by line. It’s an excellent tool for testing out small snippets of code and debugging.
Let’s open the Python shell. If you’re using an IDE like PyCharm or Visual Studio Code, the Python shell (also called the Python console) is usually available within the IDE. If you’ve installed Python directly, you can open the shell by typing python
(or python3
on some systems) in your terminal.
In the Python shell, you should see something like this:
Python 3.9.0 (default, Oct 5 2020, 00:00:00)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
symbol is the Python prompt where we can type Python code.
Let’s try a simple command:
>>> print("Hello, Python shell!")
Hello, Python shell!
In the above example, we called Python’s built-in print
function, which outputs the provided string to the console.
Writing Your First Program
Now that we’ve gotten a taste of the Python shell, let’s write our first Python program.
Python programs are simply text files with the extension .py
. You can create a Python file in your chosen IDE or text editor.
Let’s create a file called hello_world.py
and write the following code in it:
print("Hello, Python program!")
We’ve written our first Python program! To run it, navigate to the directory containing hello_world.py
in your terminal and run the command python hello_world.py
(or python3 hello_world.py
if needed). The output should be:
Hello, Python program!
Congratulations! You’ve now used the Python shell and written and run your first Python program.
Next Steps
In the following sections, we will discuss variables, data types, and operators. You’ll start to learn how to store and manipulate data in Python, which is the basis of all Python programming.
This is a very basic introduction. As we progress further into Python, we’ll delve into more complex topics and create more elaborate programs.