Beginner Chapter 1 · 20 min read · Module 1: Python Fundamentals
Introduction to Python
- After this chapter you can explain what Python is in simple words.
- You can describe why Python is a good first language.
- You can explain the difference between source code, an interpreter and output.
- You can run simple Python statements such as print() and basic arithmetic.
- You can recognise Python's most important beginner features: readable syntax and indentation.
Python is a programming language. A programming language is a way of giving clear instructions to a computer so that it can do a job for you. The job may be small, such as adding two numbers, or large, such as running a website that thousands of people visit every day. Python is famous for being easy to read and easy to learn, which is why many schools, universities and companies use it.
When you read a Python program, it often looks like plain English with a few symbols added. This is not an accident. The people who designed the language wanted code to be clear to humans, not just to machines. As a beginner this helps you a lot, because you can spend your energy on learning how to think about problems instead of fighting with strange symbols.
In this chapter you will meet the big ideas behind Python. You will learn what an interpreter is, how a program runs, and how to write your first lines. You do not need any earlier programming experience. Read slowly, run every example yourself, and change the values to see what happens. Doing is the fastest way to learn.
What is Python?
Python is a high-level, general-purpose programming language. High-level means that you write instructions in a form close to human language, and Python takes care of the small machine details for you. General-purpose means that it is not made for only one kind of job. You can use the same language for websites, data analysis, automation, games and much more.
Python is an interpreted language. This means you do not have to run a separate build step before your code works. You write the code, give it to the Python interpreter, and the interpreter runs it. Behind the scenes Python first turns your code into a simpler form called bytecode and then runs that bytecode, but you do not need to manage this yourself.
Why learn Python first?
- Easy to read: the code is short and looks clean, so you can understand what a program does quickly.
- Easy to start: you can run a single line and see the result at once. You do not need a long setup.
- Very useful: the same skills help you in web development, data science, automation and many other fields.
- Large community: millions of learners and developers share answers, tutorials and free libraries.
- Many ready-made tools: the standard library and thousands of extra packages mean you rarely need to build everything from zero.
Your first Python program
The traditional first program prints a greeting on the screen. In Python, the print() function does this. A function is a named action. You write its name, then put the value you want to use inside round brackets. Text values are called strings, and you must place them inside quotation marks.
print("Hello, World!")When Python runs this line, it finds the print function, sends it the text, and the text appears in the output. Notice that the quotation marks are not printed. They only tell Python where the text begins and ends.
Using Python as a calculator
Python understands the usual maths symbols. Use + to add, - to subtract, * to multiply and / to divide. Two stars ** mean power, and // gives a whole-number division. The % symbol gives the remainder. Python follows the normal order of operations, so multiplication happens before addition unless you use brackets.
print(2 + 3)
print(10 - 4)
print(6 * 7)
print(9 / 2)
print(9 // 2)
print(9 % 2)
print(2 ** 5)
print(2 + 3 * 4)
print((2 + 3) * 4)/ always gives a decimal number (a float), even when the answer looks whole. For example 8 / 2 gives 4.0.Variables store values
A variable is a name that points to a value. You create one with the equals sign. The equals sign here does not mean "is equal to" in the maths sense. It means "store this value under this name". After that you can use the name instead of the value. Python decides the type of the value by itself, so you do not have to declare it first.
name = "Asha"
age = 15
height = 1.62
print(name)
print(age + 1)
print(type(name))
print(type(age))
print(type(height))The three types seen above are str for text, int for whole numbers and float for decimal numbers. You will learn them in detail in later chapters.
Indentation matters
Many languages use curly brackets to group code. Python uses indentation, which means the spaces at the start of a line. Lines that belong together are indented by the same amount. This forces programs to look tidy, and it is one reason Python code is so readable. The usual rule is four spaces for each level.
score = 72
if score >= 40:
print("You passed")
print("Well done")
print("Finished")The two indented lines run only when the condition is true. The last line has no indentation, so it runs every time.
How a Python program runs
- You write source code in a text file that ends with
.py. - You ask the Python interpreter to run the file, for example with
python hello.py. - Python reads the code from top to bottom and checks the syntax.
- Python carries out each instruction in order.
- Results appear in the terminal, or errors are shown if something is wrong.
# saved in a file called hello.py
print("Running from a file")
# in the terminal:
# python hello.pyYou can also use the interactive mode. If you type python alone in a terminal, you get a prompt that looks like >>>. Here you can type one statement at a time and see the answer immediately. This is a great place to test small ideas.
Reading error messages
Everyone makes mistakes when coding, including experienced developers. When Python cannot understand a line, it stops and prints an error message. Do not be afraid of it. The message tells you the kind of error and the line where it was found. Read the last line first, because it names the problem. For example, a SyntaxError means the code is not written in a valid way, and a NameError means you used a name that does not exist yet.
A good habit is to write a little, run it, and fix problems at once. Small steps make mistakes easy to find.
Python syntax
print(value)name = value# this is a commentif condition:
statementReal-world applications
- Building websites and web services with frameworks such as Django and Flask.
- Analysing data and drawing charts for school, business or research.
- Automating boring computer tasks such as renaming files or sending reports.
- Teaching programming in schools and colleges because of its simple style.
- Writing small scripts that save time in everyday work.
Common mistakes
Fix: Put text inside single or double quotes: print("Hello").
Fix: Python is case-sensitive. Built-in names such as print are written in lowercase.
Fix: Use four spaces for each level and keep them the same inside a block.
Fix: Use == to compare and = to store a value in a variable.
Important tips
- Type the examples yourself instead of only copying them. Your fingers learn too.
- Change a value and predict the output before you run the code.
- Read the last line of an error message first.
- Try small ideas in interactive mode before putting them in a file.
- Practise a little every day. Short regular practice beats one long session.
Key points summary
- Python is a high-level, general-purpose, interpreted language.
- The interpreter reads your code and runs it step by step.
- print() shows values on the screen.
- Variables store values under names, and Python works out the type for you.
- Indentation defines blocks of code.
- Python is case-sensitive.
Exam and interview points
- Python is a high-level, interpreted, general-purpose language.
- Python uses indentation instead of curly brackets to mark blocks.
- print() is a built-in function that writes output to the screen.
- The = symbol is assignment, while == compares two values.
- Python is case-sensitive: name and Name are different.
Quick revision
- Python = readable, general-purpose, interpreted.
- Interpreter runs source code line by line.
- print("text") shows text.
- / gives a float, // gives whole division, % gives remainder.
- Four spaces of indentation per block.
- Read error messages from the last line.
Frequently asked questions
Is Python a good first language?
Yes. Its syntax is clean and close to everyday English, so beginners can focus on logic instead of complicated symbols. Many schools and universities choose it for this reason.
Do I need to be good at maths to learn Python?
No. Basic arithmetic is enough to start. You will need more maths only if you move into areas such as data science or machine learning, and you can learn it step by step.
What is the difference between Python and the Python interpreter?
Python is the language, which is a set of rules. The interpreter is the program on your computer that follows those rules and runs your code.
Is Python free?
Yes. Python is free to download, use and share, and its source code is open.
Do I need to compile my Python code?
You do not run a separate compile step yourself. You give the file to the interpreter, and it does the needed work automatically.
Practise this chapter
20 practice questions Take the chapter quiz