A first program with python

python-logo.png

Last year my younger son started programming in scratch, following a course taught at his school. So, I figured out that a nice summer project would be programming together in python.

What is programming?

Programming is giving instruction to the computer, so that it can maintain a dialogue with the user.

In order to keep dialogue, one needs to interchange information, through for example talking and listening.

Talking and listening in python

Let us highlight that a program is a set of instructions (or orders) for the computer, hence when you read them you have to take the place of the computer.

The python command (or function; see What is a function?) for talking is print(), while the command for listening is input().

The print() command admits arguments, for example a phrase, and shows their contain in the screen.

print("Hello, World!")

The function to listening in python is input(). This function, in its presented form, raises a prompt expecting the user's input.

input()

However, input() has an interesting feature… it can print a phrase before raising the prompt. This is useful to provide context of the expected input. In the code block below we exemplify this feature.

input("Hi, What is your name? ")

NOTE: The last code block is equivalent to

print("Hi, What is your name? ")
input()

We have written our first python program, but there is an issue. A conversation follows according to the previous sentences. So far we aren't able to retrieve the information listened from the user.

Some memories never fade

Suppose that a couple adopts a dog. They can call it shouting 'dog'. However, if they adopt several dogs, the right strategy would be to give (or assign) a different name to each dog. Assigning names (or labels) is the way to keep track of the inputted information.

In our example, we shall assign the label user_name to the user's input. The assignation in python is achieved using the equal sign.

user_name = input("Hi, What is your name? ")

The information can be retrieved using the print() function.

print(user_name)

Some technicalities

There are several types of information: numbers, letters, phrases, images, etc. In python different kinds of information are called data types. Our phrases, which you might have noticed are written between double quotes "" (might be also written between single quotes ''), are called strings.

Strings can be concatenated forming longer strings, using the + operation:

longer_string = "This is a string data type" + 'this one too'

Within the print() function a set of string arguments, which are separated by commas, are concatenated:

print("This is a string data type", 'this one too')

The input() function assumes that whatever the user writes is a string. Hence, we can concatenate our input string with other strings… withing the print() function:

print("Nice to meet you ", user_name)

Our complete first python program

Here it is:

user_name = input("Hi, What is your name? ")
print("Nice to meet you ", user_name)

Date: 2025-12-26 Fri 11:22

Author: Oscar Castillo-Felisola

Created: 2025-12-27 Sat 07:34