Python is a general-purpose programming language known for code that is easy to read. It is a common first language for beginners, and it is also widely used for automation, data analysis, web development and electronics projects. This guide covers the basics: how to run Python code, how to write and run your first “Hello, world” program, and which data types you will use most.
What You Need Before You Start
You need only two things: Python installed on your computer and a place to type code. The installer from the official Python website includes IDLE, a simple editor and shell that works well for learning. You can also use any plain text editor together with the command line.
Python 3 is the version to learn today. Older tutorials, including the first version of this one, use Python 2 syntax. The biggest difference beginners notice is the print command, which is covered below.
Running Python in Interactive Mode
Interactive mode lets you type one line of Python and see the result right away. It is the quickest way to try out ideas and learn how the language behaves.
To start it, open a terminal or command prompt and type python (on Windows the launcher py also works). You will see a prompt that looks like >>>. Type an expression such as 2 + 3 and press Enter, and Python prints 5. To leave interactive mode, type exit().
The video below shows Python running in interactive mode.
Your First Program: Hello World
The traditional first program prints a short message. In Python 3 it is one line:
print(“Hello, world!”)
The original version of this article used print “hello world”;. That form only works in Python 2. In Python 3, print is a function, so it needs parentheses. Python also does not need a semicolon at the end of a line.
Running It with Python IDLE
IDLE is the easiest way to run a program on Windows without touching the command line. The steps are:
- Open IDLE from the Start menu.
- Choose File, then New File.
- Type the print line shown above.
- Save the file with a .py extension, for example hello.py.
- Choose Run, then Run Module (or press F5).
The output appears in the IDLE shell window. The video below walks through the process on Windows.
Running It from the Windows Command Line
You can also run the same file from the command prompt. Python does not compile your program in a separate step the way some languages do. It reads the file and runs it directly.
- Open Command Prompt.
- Move to the folder that holds your file with the cd command.
- Type python hello.py and press Enter.
If Windows says Python is not recognized, Python was probably not added to your PATH during installation. Running the installer again and ticking the option to add Python to PATH usually fixes this.
Basic Python Rules to Know
- Indentation matters. Python uses spaces at the start of a line, not curly braces, to group code. Four spaces is the usual choice.
- Comments start with #. Python ignores everything after it on that line.
- Case matters. name and Name are two different variables.
- Variables need no declaration. You write age = 30 and Python works out the type for you.
Python Data Types
Every value in Python has a type. Knowing the common ones helps you avoid many beginner errors. You can check any value with the type() function, for example type(3.5).
Numbers
- int: whole numbers such as 7 or -12.
- float: numbers with a decimal point, such as 3.14.
Text and Booleans
- str: text in single or double quotes, such as “Python”.
- bool: either True or False. These values drive decisions in your code.
Collections
- list: an ordered group of items you can change, written like [1, 2, 3].
- tuple: like a list, but it cannot be changed after creation, written like (1, 2, 3).
- dict: pairs of keys and values, written like {“name”: “Ana”}.
- set: a group of unique items with no fixed order.
There is also None, a special value that means “nothing here”.
Where to Go Next
Once you can run a script and understand the basic types, the next step is making programs respond to data. Learning the if statement in Python lets your code make decisions, and reading input in Python lets users type values into your program. Note that the input() function always returns text, so convert it with int() or float() when you need a number.
If you already know another language, it can help to see how C compares with Python in syntax and speed. When you are ready for hands-on practice, Python also works well with hardware. Our list of Raspberry Pi Python projects has ideas for every level. For practical scripts, you can also try sending email in Python.
Tips for Learning Python Faster
- Type the examples yourself instead of copying them.
- Change a small part of each example and see what happens.
- Read error messages carefully. Python usually names the line and the kind of problem.
- Build a tiny project, such as a calculator or a number guessing game, as soon as you can.
Frequently Asked Questions
Is Python hard to learn for beginners?
Python is generally considered one of the easier languages to start with. Its syntax reads close to plain English, and you can see results quickly in interactive mode.
Why does print “hello world” give an error?
That is Python 2 syntax. In Python 3 you must use parentheses: print(“Hello, world!”).
Do I need to compile Python code?
No separate compile step is needed. You run a script with python filename.py, and Python handles the rest behind the scenes.
Should I use IDLE or the command line?
IDLE is simpler when you are just starting. The command line becomes more useful as your projects grow, and it is worth learning both.