AlgoMaster Logo

First Python Program

Last Updated: December 6, 2025

7 min read

You've set up your environment, and now it's time to dive in and write your very first Python program. This is where the magic begins, and trust me, it’s both thrilling and empowering to see your code come to life.

In this chapter, we’ll walk through creating a simple Python program, explore the structure of Python code, and discuss some common practices that will set you up for success as you continue your coding adventure. Let’s get started!

Your First Python Program

Let’s kick things off with a classic: the "Hello, World!" program. This program is a rite of passage for every programmer, and writing it will give you a feel for how Python's syntax works.

To create your first Python program, open your favorite IDE or text editor, create a new file named hello.py, and enter the following code:

Now, let’s break down what’s happening here:

  • print(): This is a built-in function that outputs text to the console.
  • "Hello, World!": This is a string, which in Python is a sequence of characters enclosed in quotes.

To run your program, open your terminal, navigate to the directory where your file is located, and execute:

You should see the output:

This simple program demonstrates the basic syntax of Python and introduces you to running scripts. It's a small step, but it’s a significant one!

Understanding Python Syntax

Python is often praised for its clean and readable syntax. Let's dive deeper into some fundamental concepts that make Python distinct.

Indentation

In Python, indentation is not just for readability; it defines the structure of your code. Unlike other languages that use braces or keywords to indicate blocks of code, Python uses whitespace.

For example:

If you forget to indent, Python will raise an IndentationError. Consistent indentation is crucial. The standard practice is to use four spaces per indentation level.

Comments

Comments are essential for documenting your code, making it easier for you and others to understand later. In Python, a comment starts with a # symbol:

You can also create multi-line comments using triple quotes, though they are technically string literals:

Data Types and Variables

As you get comfortable with Python, you'll start using data types and variables to store information. Understanding these will help you write more complex programs.

Variables

In Python, you don’t need to declare the type of a variable explicitly. You can assign a value to a variable directly:

Here, name is a string, and age is an integer. Python is dynamically typed, meaning you can change the type of a variable:

Common Data Types

Let’s explore some common data types in Python:

  • Integers: Whole numbers, e.g., 42
  • Floats: Decimal numbers, e.g., 3.14
  • Strings: Text, e.g., "Hello"
  • Booleans: True/False values, e.g., True

You can check the type of a variable using the type() function:

Example: Using Variables

Here’s a quick example that combines variables and string manipulation:

This program creates two variables for the first and last names, combines them into a full name, and prints it out.

Control Flow Basics

Now that you have the basics down, let’s look at how to control the flow of your program with conditional statements and loops.

Conditional Statements

Conditional statements allow you to execute different code based on certain conditions. The basic syntax uses if, elif, and else:

Each block is executed based on the condition being true. Notice how we use indentation to show which code belongs to which condition.

Loops

Loops are great for executing a block of code multiple times. The for loop and while loop are the most common types.

For Loop

A for loop iterates over a sequence, such as a list:

While Loop

A while loop continues executing as long as a condition is true:

In this example, the loop will print the count from 0 to 4.

Functions: Structuring Your Code

Functions are reusable blocks of code that perform a specific task. They help you organize your code and make it more modular.

Defining Functions

You can define a function using the def keyword:

In this example, greet is a function that takes a parameter name and prints a greeting.

Return Values

Functions can also return values using the return statement:

This function takes two arguments and returns their sum, which you can then use elsewhere in your program.

Real-World Application: A Simple Calculator

Now that we've covered the basics, let's tie everything together with a simple calculator program. This program will prompt the user for two numbers and an operation, then return the result.

In this example, we define four basic operations as functions. The calculator function handles user input and determines which operation to perform.

This program illustrates how to combine everything you've learned so far: functions, conditionals, and user input. As you build more complex applications, this structure will serve as a foundation for your coding projects.

Congratulations! You've just written your first Python program and explored the core components that make up Python syntax, data types, control flow, and functions.

These concepts are the building blocks for all your future projects.

We will dive into how the interpreter works, how to use it effectively, and some common commands that will enhance your programming experience.