Codeskill

Learn to code, step by step

Hello, Python! Writing Your First Python Program

Writing and running your first Python program. You will create a simple script, run it from the terminal, and try a few basic syntax features along the way.

Why Python for a first program

Python syntax is clean and readable – close to plain English in places. That makes it a good first language. Do not let the simplicity fool you though; the same language runs everything from small scripts to large web applications.

Setting up

Assuming you have already set up your development environment, open your editor and create a new file. Call it first_program.py.

Hello, Python!

The traditional first program prints a greeting. In Python it is one line:

print("Hello, World!")

Save the file. Open your terminal, go to the folder where you saved it, and run python first_program.py. You should see Hello, World! printed. That is a working Python script.

Basic Python syntax

A few more things worth trying on top of that first print.

  • Variables and data types: Python is dynamically typed – you do not declare a type upfront.
message = "Hello, Python world!"
print(message)

Here message is a string. Python also has integers, floats, and booleans.

  • User input: programs get more interesting when they respond to someone typing.
name = input("What is your name? ")
print(f"Hello, {name}!")

Asks for a name, then prints a personalised greeting.

  • Basic arithmetic: Python handles numbers without much fuss.
num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
sum = float(num1) + float(num2)
print(f"The sum is {sum}")

Takes two numbers as input and prints their sum. Note the float() conversion – input() always returns a string.

Comments and good coding practices

Comments are lines Python ignores. Use them to explain why, not what. A comment starts with #:

# This is a comment explaining the following line of code
print("Hello, World!")

Meaningful variable names and the occasional comment make code easier to read six months later – usually by you.

Troubleshooting: syntax errors

You will hit errors. That is normal. Syntax errors usually mean a typo, a missing colon, or bad indentation. Python’s error messages point at the line that caused the problem. Read them – they are more helpful than they look.

What comes next

Once this feels comfortable, move on to loops, conditionals, functions, and data structures. The basics here are the foundation for all of it.

PreviousPython Basics: Setting Up Your Development Environment