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
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.
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:
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 theprint()statement to suppress the line break after displayingSingle. 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 (
A—Zanda—z) and the underscore symbol (_). It may also contain digits (0–9), but the variable name can’t start with those. For example,Key_2is a valid variable name but2_keyis not. Variable names are case-sensitive, meaning thatname,Name, andNAMEare 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")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