First Encounter with Python

We may not know what we are doing here, but experimenting will not hurt anything (or anyone)!

Checking the Python Installation

Assuming that you have successfully installed Python (check the Appendix for help). Open up a command prompt window and follow along!

Warning

The examples in this lecture were produced on my Mac. Obviously, things will look a bit different on your PC. The most obvious difference will be the system prompt.

$ python --version
Python 3.8.6

Hmmm, it looks like my Mac has an older release of python. Version 3.7 jus cme out, I will probably need to upgrade!

As long as you see something similar on your screen, we know that you have Python properly installed and you can run it from the command line.

Hello, World!

We better get that long-standing tradition out of the way. Let’s make the computer print out “Hello, World!” on the console (fancy talk for the :term:command-prompt` termnal window.

To do this, we need to edit an extremely short file that we will name “hello1.py:

print("Hello, World!")

Now, let’s see if Python can process this file:

$ python hello1.py
Hello, World!

Phew! The Python gods are now happy, and we can get on with our work!

User Input

No program worth much of anythng runs with all the data needed coded into the program. Let’s ask the user for some inut. Create a second file, called “hello2.py” (creative, huh?) with this content:

name = input("Enter your name")
print("Hello " + name)

Let’s see if this runs:

name = input("Enter your name: ")
print("Hello", name)
print("Goodbye " + name)

And her it is running:

$ python hello2.py Enter your name: Roie Hello Roie GoodBye Roie