Python · Beginner
Python for Beginners: Variables, Data Types, and Operators
Learn Python from scratch using variables, numbers, strings, Boolean values, and operators.
Applied Machine Learning ·
This is part 1 of the Python Fundamentals series. No programming experience is required.
Getting started
Use Google Colab to run the examples. See our Colab beginner guide for setup. Download the accompanying notebook and upload it to Colab. Run cells in order.
Learning objectives
- Understand your first python program.
- Understand variables and assignment.
- Understand basic data types.
- Understand arithmetic operators.
- Understand strings and formatting.
- Understand comparisons and boolean logic.
- Understand type conversion.
- Understand practical example: dataset statistics.
Your first Python program
Python executes instructions in order. The print function displays a value in the notebook output. Text must be enclosed in quotes.
print("Hello, Python!")
Variables and assignment
A variable is a name associated with a value. Assignment uses a single equals sign. Python names are case-sensitive and cannot begin with a digit. Use descriptive names.
sample_count = 100
learning_rate = 0.01
print(sample_count, learning_rate)
Basic data types
Integers represent whole numbers, floats represent floating-point numbers, strings represent text, and Booleans represent True or False. None represents the absence of a value. Python determines types at runtime.
values = [42, 3.14, "Python", True, None]
for value in values:
print(value, type(value).__name__)
Arithmetic operators
Addition, subtraction, multiplication and division use +, -, * and /. Floor division uses //, remainder uses %, and exponentiation uses **. Parentheses control evaluation order.
print(7 + 3, 7 - 3, 7 * 3, 7 / 3)
print(7 // 3, 7 % 3, 7 ** 2)
print((2 + 3) * 4)
Strings and formatting
Strings can be combined with +. Indexing starts at zero. An f-string inserts values into text using braces. Strings are immutable.
name = "Python"
score = 0.875
print(name[0], name.lower(), len(name))
print(f"Accuracy: {score:.1%}")
Comparisons and Boolean logic
Comparisons return Boolean values. Equality uses ==, inequality uses !=, and ordering uses <, >, <= and >=. The operators and, or and not combine conditions.
accuracy = 0.92
minimum = 0.90
print(accuracy >= minimum)
print(accuracy >= minimum and accuracy <= 1.0)
print(not (accuracy == minimum))
Type conversion
Use int, float and str to convert compatible values. Converting a decimal float to an integer truncates toward zero. Invalid numeric strings raise ValueError.
text = "42"
count = int(text)
print(count + 8)
print(float("3.5"), str(count), int(3.9))
Practical example: dataset statistics
Suppose 180 of 200 predictions are correct. Divide the number correct by the total to calculate accuracy. This is arithmetic, not model training.
correct = 180
total = 200
accuracy = correct / total
print(f"Accuracy: {accuracy:.2%}")
Common mistakes
Use == for comparison rather than =. Keep quotes around strings and check types before combining values. Division by zero raises an exception.
Practice exercises
- Calculate the mean of 12, 18 and 24.
- Convert the string โ0.85โ to a number and test whether it exceeds 0.8.
Exercise solutions
Exercise 1
print((12 + 18 + 24) / 3)
Exercise 2
print(float("0.85") > 0.8)