Every program works with data: names, ages, prices, messages. In Python you store that data in variables, and it comes in different types, mainly text (strings) and numbers. Understanding these basics is the foundation for everything else you will build.
This guide assumes you have Python installed and can run a file. If not, read my guide on writing your first 10 lines of Python. Type each example yourself and compare the output with what you see here.
Variables: labeled boxes
A variable is a name that holds a value. You create one with the equals sign:
name = "Sam"
age = 20
height = 1.75
print(name, age, height)
Output:
Sam 20 1.75
The = sign does not mean "equals" like in maths. It means "store the value on the right in the name on the left".
Naming rules
- Use letters, numbers and underscores, and don't start with a number.
- Names are case sensitive:
ageandAgeare different variables. - Use clear names like
user_nameinstead ofx, and use lowercase with underscores. - You cannot use Python's reserved words, such as
if,fororclass.
The three types you will use most
print(type(name), type(age), type(height))
Output:
<class 'str'> <class 'int'> <class 'float'>
- str (string): text, written in quotes, like
"Sam". - int (integer): whole numbers, like
20. - float: numbers with a decimal point, like
1.75.
Working with strings
Joining text
greeting = "Hello, " + name + "!"
print(greeting)
print(f"{name} is {age} years old.")
Output:
Hello, Sam!
Sam is 20 years old.
The second line uses an f-string: put an f before the quotes and place variables inside curly brackets. It is the easiest way to mix text and values.
Useful string tools
print(len(name))
print(name.upper())
print(name.lower())
print(name[0])
print(name[1:3])
Output:
3
SAM
sam
S
am
len()counts the characters..upper()and.lower()change the case.name[0]gets the first character. Python starts counting at 0, not 1.name[1:3]is a slice: it starts at position 1 and stops before position 3.
Working with numbers
print(7 + 3, 7 - 3, 7 * 3)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 5)
Output:
10 4 21
3.5
3
1
32
+,-and*add, subtract and multiply./divides and always gives a decimal result (3.5).//divides and keeps only the whole part (3).%gives the remainder (1). It is handy for checking whether a number is even or odd.**raises to a power (2 ** 5is 32).
Converting between types
Text that looks like a number is still text. Python will not multiply it until you convert it:
price = "10"
total = int(price) * 3
print(total)
Output:
30
Without int(), "10" * 3 would repeat the text three times instead of doing maths. The functions to remember are int(), float() and str(). Remember that input() always returns text, so convert it when you need a number.
Changing a variable
age = age + 1
print(age)
print(round(3.14159, 2))
Output:
21
3.14
A variable can be updated at any time. round() rounds a number to a chosen number of decimal places.
Common beginner mistakes
- Mixing text and numbers.
"Age: " + 20causes an error. Use an f-string orstr(20). - Forgetting quotes around text, or putting quotes around a variable name you want the value of.
- Starting counting at 1. The first character of a string is at position 0.
- Using unclear names.
x1andtemp2will confuse you next week. - Assuming
/gives a whole number. Use//if you want one.
Practice tasks
- Create variables for your name, age and favourite number, and print a sentence using an f-string.
- Print your name in uppercase and its length.
- Ask the user for two numbers with
input()and print their sum. Remember to convert them. - Check whether a number is even using the
%operator.
Final thoughts
Variables, strings and numbers appear in every Python program you will ever write. Practice them until they feel natural, and don't worry about memorizing everything. Looking things up is part of programming. Once you are comfortable, the next steps are decisions, loops, lists and functions.
Which task was the trickiest for you? Tell me in the comments, and follow the blog for more guides on programming, AI, crypto and Kali Linux.
.webp)





