Search⌘ K
AI Features

First Python Program

Explore how to write your first Python program using print statements, comments, and input functions. Understand variable naming rules, suppress line breaks, and create personalized messages. Practice by writing a program that collects user details and tells a story using those inputs.

Problem solving with Python

The basic tools for understanding the flow and the results of simple programs are execution sheetsAn execution sheet is a manual way for observing and updating the values of the variables at each step of a program’s execution. and flowchartsA flowchart is a diagram that helps understand the sequence of instructions in a program having decisions.. However, they become complicated when we have problems with a large number of decisions. Therefore, we provide executable embedded code widgets to observe the program output by executing it.

We can run a program as many times as we want to try out different inputs of variables. We can also edit these programs to observe the result of a change in a value or logic for our learning.

Hello World

It’s time to write our first program in Python, which will simply display the message, “Hello World!”, on the screen.

This is the code widget that we’ll use to write our Python code. The print statement is used to display the text "Hello World!" in parentheses ( ). We may execute our Python program by clicking the “Run” button at the bottom of the widget.

Python 3.10.4
print ("Hello World!") # printing Hello World

Note: We can add non-executable text in a Python program by adding a hash sign (#) at the start of the text. This type of text is called a comment. We use comments to include descriptions in the program.

Suppress the new line in the output

The normal behavior of the print() statement is to add a line break at the end. We may suppress it using end="", as demonstrated in the following code:

Python 3.10.4
print("print without end causes a line-break") # The print without end adds a new line
print("Single",end="") # The next output will be on the same line
print("Line") # The next output will also be on the next line
print("It demonstrates the line break") # This line is shown on a new line

In the code above:

  • Line 1: The text of this line is shown with a line break at the end.
  • Line 2: It shows the use of end="" as a separate item in the print() statement to suppress the line break after displaying Single. The comma , is used to separate it from the text.
  • Line 3: It illustrates that there is no line break after the previous print() so this text is joined with the previous text in the output.
  • Line 4: It displays the text on the new line.

Personalize messages and add variables

Let’s write another Python program that inputs the name of the user before displaying a greeting message. We use name as a variable to store the input given by the user in Python.

Note: A variable name can only contain letters (AZ and az) and the underscore symbol (_). It may also contain digits (09), but the variable name can’t start with those. For example, Key_2 is a valid variable name but 2_key is not. Variable names are case-sensitive, meaning that name, Name, and NAME are three different variables.

The main.py in the left pane of the following widget is the name of the program file. The .py indicates that it’s a Python program. The input statement is used to take the input from the user. The text "Enter your name: " in parentheses ( ) is displayed as the prompt before taking the input. When we run this program, it shows a black screen called the terminal, where we can input values and see the resulting output.

name = input("Enter your name: ") # Taking input by user in variable name
print("Hello ",name) # Printing User's name with Hello
print("Welcome to Educative")
Take and display the input of the name (variable)

The word exit at the end of the output indicates the completion of the program in the terminal.

The code above displays the message "Hello " as a label before the value of the name variable on the first line. The comma , separates the label and the variable. Then, it displays “Welcome to Educative” on the second line.

Python programming practice

Let’s practice writing the first Python program that takes multiple inputs from the user.

Tell a story

Let’s write a program that tells a story with information given by the user. The program should ask the user to enter their name, age, city, college, profession, and pet’s name.

We’ll need to provide user input in the box labeled “Enter the input below.” Therefore, when the solution is ready for execution, we can test it by providing the following information line by line in the input box on behalf of the user.

  • User’s name
  • User’s age
  • User’s city
  • User’s college
  • User’s profession
  • User’s pet’s name

We should also keep in mind that we use the same order of inputs in our code. The program should display the following story, inserting the user’s input into the appropriate locations in the following text:

There once was a person named NAME who lived in CITY. At the age of AGE, NAME went to college at COLLEGE. NAME graduated and went to work as a PROFESSION. Then, NAME adopted an animal named PETNAME. They both lived happily ever after!
# Write your code below
Taking and displaying multiple inputs