Why Python is a common first language
Python is a general-purpose programming language that is widely used for teaching. Its code tends to read like short English instructions, and it needs little punctuation compared with some other languages. It is also used for real work in many fields, from data analysis to web development, so skills carry forward. Python is open-source software, which means it is free to download and use.
This article assumes no experience. You will meet four ideas: values, variables, simple calculations and output. You can try each example by typing it into any Python environment, or an online one if you prefer not to install anything. Lines that start with # are comments: notes for humans that Python ignores.
Showing output with print
The print function displays something on screen.
print("Hello, world")
This displays Hello, world. The text in quotation marks is a string, a piece of text. You can use single or double quotes, as long as you open and close with the same kind.
You can print several things at once, separated by commas:
print("Total:", 5)
This displays Total: 5, with a space added between the items.
Values and types
A value is a piece of data, and every value has a type. Three you will meet immediately:
int: whole numbers, such as7or-3float: numbers with a decimal point, such as2.5str: strings, such as"cat"
The type matters because it decides what you can do. You can add numbers, and you can join strings, but mixing them without care causes an error. Python can tell you a type with the type function:
print(type(7))
print(type(2.5))
print(type("cat"))
The output is <class 'int'>, then <class 'float'>, then <class 'str'>.
Variables: names for values
A variable is a name that refers to a value. You create one with the equals sign:
price = 4.5
quantity = 3
The equals sign here does not mean “is equal to” as in a math equation; it means “store the value on the right under the name on the left”. This is a small difference from algebra basics, where a variable stands for an unknown, but the two ideas are related: both use a name as a stand-in for a number.
Once defined, you can use the variable anywhere you would use its value:
total = price * quantity
print(total)
Here 4.5 * 3 is 13.5, so the output is 13.5.
A variable can be changed:
quantity = 3
quantity = quantity + 1
print(quantity)
The right-hand side is worked out first (3 + 1 = 4), and the result is stored back under the same name. The output is 4.
Naming variables
- Use letters, digits and underscores, and do not start with a digit.
- Names are case sensitive:
Totalandtotalare different variables. - Choose meaningful names.
pricetells a reader far more thanp.
Arithmetic
Python can act as a calculator. The common operators are:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
add | 2 + 3 |
5 |
- |
subtract | 7 - 4 |
3 |
* |
multiply | 4 * 5 |
20 |
/ |
divide | 7 / 2 |
3.5 |
// |
whole-number division | 7 // 2 |
3 |
% |
remainder | 7 % 2 |
1 |
** |
power | 2 ** 3 |
8 |
Python follows the usual order of operations: powers first, then multiplication and division, then addition and subtraction, with brackets overriding everything. So 2 + 3 * 4 gives 14, while (2 + 3) * 4 gives 20.
Note that / always produces a float. Dividing 10 by 2 gives 5.0, not 5.
Working with strings
Strings can be joined with +:
first = "Ada"
greeting = "Hello, " + first
print(greeting)
The output is Hello, Ada. An easier way to mix text and values is an f-string, which starts with the letter f and puts variables inside curly brackets:
name = "Sam"
age = 12
print(f"{name} is {age} years old.")
This displays Sam is 12 years old.
You cannot add a number to a string directly. "Age: " + 12 causes a TypeError, because Python does not know whether you want arithmetic or joining. Using an f-string, or converting with str(12), avoids the problem.
Common first errors
Errors are part of learning, and the messages usually help. A few you will probably meet:
- SyntaxError: a typo such as a missing closing quotation mark or bracket.
- NameError: using a variable that has not been created, or misspelling its name.
- TypeError: combining incompatible types, such as a string and a number.
Read the last line of the message and note the line number it mentions. Treating each error as a clue is the heart of a debugging mindset.
Practice
Try storing a price and a quantity and printing the total, or use % to check whether a number is even (the remainder after dividing by 2 is 0). Saving your practice scripts with clear file naming, such as total_price.py, helps when you return to them. When you are ready to go further, the next topics are usually decisions and repetition, which come with programming loops, and many free computer science resources continue from here.