CommonLounge Archive

Python console and Python programs

December 19, 2018

You can use Python on your computer in two ways — using the Python console, or by writing a Python program and running it. Let’s discuss these one by one.

Python prompt

To start playing with Python, open up the command line on your computer. You should already know how to do that – you learned it in the Introduction to the Command-line Interface chapter.

Once you’re ready, follow the instructions below.

We want to open up a Python console, so type in python on Windows or python3 on Mac OS/Linux and hit enter.

$ python3
Python 3.6.1 (...)
Type "help", "copyright", "credits" or "license" for more information.

After running the Python command, the prompt changed to >>>. For us this means that for now we may only use commands in the Python language. You don’t have to type in >>> – Python will do that for you.

If you want to exit the Python console at any point, just type exit() or use the shortcut Ctrl + Z for Windows and Ctrl + D for Mac/Linux. Then you won’t see >>> any longer.

For now, let’s just check everything is working properly before exiting the console. Type in some math, like 2 + 3 and hit enter.

>>> 2 + 3

Nice! See how the answer popped out?

You can also try this:

>>> name = 'Maria'
>>> name

When you just type something into the Python console, it responds with the string representation of the result. For example, above when you typed name, the Python console responds with the string representation of the variable name, which is the letters M-a-r-i-a, surrounded by single quotes, ''.

The Python console allows you to run Python code one line at a time. This is great for quick experimentation and for getting a feel for something new you are learning.

Saving Python programs

Normal programs are saved in files and executed by our programming language interpreter or compiler. You need to:

  • Exit the Python interpreter
  • Open up our code editor of choice
  • Save some code into a new python file
  • Run it!

To exit from the Python interpreter that you’ve been using, simply type the exit() function

>>> exit()

This will put you back into the command-line prompt.

Earlier, you picked out a code editor from the Code Editor section. You’ll need to open the editor now and write some code into a new file:

print('Hello, Commonlounge!')

Obviously, you’re a pretty seasoned Python developer now, so feel free to write some code that you’ve learned so far.

Now you need to save the file and give it a descriptive name. Let’s call the file and save it to your desktop. You can name the file anything you want, but the important part here is to make sure the file ends in .py. The .py extension tells the operating system that this is a Python program and Python can run it.

Note: You should notice one of the coolest thing about code editors: colors! In the Python console, everything was the same color; now you should see that the print function is a different color from the string. This is called “syntax highlighting”, and it’s a really useful feature when coding. The color of things will give you hints, such as unclosed strings or a typo in a keyword name (like the def in a function, which we’ll see below). This is one of the reasons we use a code editor. :)

With the file saved, it’s time to run it! Using the skills you’ve learned in the command line section, use the terminal to change directories to the desktop.

Change directory: OS X

On a Mac, the command will look something like this:

$ cd ~/Desktop

Change directory: Linux

On Linux, it will be like this (the word “Desktop” might be translated to your local language):

$ cd ~/Desktop

Change directory: Windows Command Prompt

On Windows Command Prompt, it will be like this:

> cd %HomePath%\Desktop

Change directory: Windows Powershell

And on Windows Powershell, it will be like this:

> cd $Home\Desktop

Now use Python to execute the code in the file like this:

$ python3
Hello, Commonlounge!

Note: on Windows ‘python3’ is not recognized as a command. Instead, use ‘python’ to execute the file:

> python

Alright! You just ran your first Python program that was saved to a file. Feel awesome?

One more example

Replace your code in with:

name = input("Whats your name? ")
print("Hello " + name + "!")

Then, run the file again:

$ python3
Whats your name? Commonlounge
Hello Commonlounge!

Again, on Windows, ‘python3’ is not recognized as a command. From now on, replace ‘python3’ with ‘python’ to execute the file.

> python

You’ll see the following:

Whats your name? Keshav
Hello Keshav!

Note: Above, the program gave the output Whats your name? , and I typed Keshav. Then, the program gave the output Hello Keshav!.

Remember we said a function is a sequence of instructions that Python has to perform? The following is the sequence of instructions for input

  • print the value the function is called with
  • Wait for user to type something and press enter
  • Return what the user typed

When we did name = input("Whats your name? "), the program outputted Whats your name? and then waited input from the user. Once I typed Keshav and hit enter, the value got stored in variable name. Then, we used the print function to greet the user.

Good job, you’ve gotten very far! You just wrote a Python program on your computer that interacts with the user and does something useful!


In the last few exercises you learned about:

  • the prompt – when you type in commands (code) into the Python console it responds with the result
  • saving Python program to a file and running it
  • interacting with the user for input using the input function

Time for the next lesson!

© 2016-2022. All rights reserved.