Lesson 7 of 70 – First Python Program
9.8%

First Python Program

After installing Python and setting up a development environment, you are ready to write your first Python program. Python programs are simple to write and easy to understand.

Note: The print() function is commonly used to display information on the screen. We will use it to create our first Python program.
Writing Your First Python Program

Let's create a simple Python program that displays the message Hello World!.

Example:
print("Hello World!")
Output:
Hello World!

The print() function sends the specified value to the standard output, which is normally the screen or terminal.

Understanding the Program

Let's understand the different parts of our first Python program.

print("Hello World!")
  • print is the name of a built-in Python function.
  • () contains the value or expression passed to the function.
  • "Hello World!" is a string.
  • The quotation marks indicate that the value is text.
Printing Text

The print() function can display different text messages.

Example:
print("Welcome to Python")
print("I am learning programming")
print("Python is easy to learn")
Output:
Welcome to Python
I am learning programming
Python is easy to learn

Each print() call normally displays its output on a new line.

Printing Numbers

The print() function can also display numbers.

Example:
print(10)
print(25)
print(100)
Output:
10
25
100

Numbers do not need quotation marks when they are written as numeric values.

Printing Calculations

Python can perform calculations and display the result using print().

Example:
print(10 + 20)
print(50 - 20)
print(5 * 4)
print(20 / 5)
Output:
30
30
20
4.0
Printing Multiple Values

You can pass multiple values to the print() function by separating them with commas.

Example:
name = "Rahul"
age = 20

print(name, age)
Output:
Rahul 20

By default, print() separates multiple values with a space.

Creating a Python File

Python programs are normally saved in files with the .py extension.

Example File:
first_program.py

Open your code editor and write the following code:

print("My First Python Program")

Save the file and then run it using the Python interpreter.

Running a Python Program

A Python file can be executed from the terminal.

Suppose your file is named first_program.py. Open the terminal in the same folder and run:

python first_program.py

On systems where Python 3 is invoked using python3, use:

python3 first_program.py
Output:
My First Python Program
Using Python Interactive Mode

Python also provides an interactive mode. In interactive mode, you can enter Python commands one at a time and immediately see the result.

Example:
>>> print("Hello Python")
Hello Python

Interactive mode is useful for quickly testing Python expressions and small pieces of code.

Printing Strings with Single Quotes

Python strings can be written using single quotes as well as double quotes.

Example:
print('Hello Python')
Output:
Hello Python

Both single and double quotation marks can be used to create strings.

Printing Text and Variables

The print() function can display variables along with text.

Example:
name = "Amit"
print("My name is", name)
Output:
My name is Amit
Printing with f-Strings

Python provides f-strings for inserting variable values directly into strings.

Example:
name = "Amit"
age = 20

print(f"My name is {name}")
print(f"My age is {age}")
Output:
My name is Amit
My age is 20

The f before the string allows expressions inside curly braces to be evaluated.

A Simple Student Program

Let's create a simple program that displays student information.

Example:
name = "Rahul"
course = "Python"
duration = 6

print("Student Name:", name)
print("Course:", course)
print("Duration:", duration, "months")
Output:
Student Name: Rahul
Course: Python
Duration: 6 months
Common Beginner Mistakes

Beginners often make small syntax mistakes when writing their first Python programs.

  • Forgetting quotation marks around text.
  • Misspelling the print() function.
  • Forgetting closing parentheses.
  • Using incorrect indentation in code blocks.
  • Running the wrong Python file.
  • Saving the file without the .py extension.
Incorrect:
print(Hello World)
Correct:
print("Hello World")
Practice Programs

Try writing the following Python programs:

  1. Print your name.
  2. Print your age.
  3. Print your city.
  4. Print your course name.
  5. Print the result of 25 + 15.
  6. Print the result of 100 - 45.
  7. Print the result of 12 × 5.
  8. Print your name and age together.
  9. Print three different messages.
  10. Display simple student information.
Key Points
  • The print() function is used to display output.
  • Python programs normally use the .py extension.
  • Strings can be written using single or double quotes.
  • Numbers can be printed without quotation marks.
  • Python can perform calculations directly inside print().
  • Multiple values can be passed to print() using commas.
  • Variables can be displayed using print().
  • f-strings can be used to insert variables into text.
  • Python programs can be executed from a terminal.
  • Python also provides an interactive mode for testing code.

🧠 Quick Quiz

Which function is commonly used to display output in Python?