Chapter 1 - Python Basics (Python)
Here's a concise walkthrough of the main ideas in Chapter 1, each with a small example.
Expressions and the interactive shell
An expression is a combination of values and operators that Python can evaluate down to a single value. You can try them interactively in the Python shell (REPL).
Example: basic math expressions.
2 + 3 # 5
10 - 4 # 6
3 * 7 # 21
22 / 8 # 2.75
22 // 8 # 2 (integer division)
22 % 8 # 6 (remainder/modulo)
2 ** 10 # 1024 (exponent)
Math operator precedence
Python follows standard order of operations: exponentiation (**) first, then multiplication/division/remainder (*, /, //, %), then addition/subtraction (+, -). Use parentheses to override.
2 + 3 * 6 # 20 (multiplication first)
(2 + 3) * 6 # 30 (parentheses override)
Data types: integers, floats, and strings
Every value in Python has a data type. The three most common are integers (whole numbers), floating-point numbers (numbers with a decimal point), and strings (text).
42 # int
3.14 # float
"Hello" # str
You can check the type of a value with type():
type(42) # <class 'int'>
type(3.14) # <class 'float'>
type("Hello") # <class 'str'>
String concatenation and replication
The + operator joins (concatenates) two strings together. The * operator repeats (replicates) a string a given number of times.
Example: combining and repeating strings.
"Alice" + " " + "Bob" # 'Alice Bob'
"Ha" * 3 # 'HaHaHa'
You cannot concatenate a string with a number directly:
"I am " + 29 # TypeError
"I am " + str(29) # 'I am 29'
Variables and assignment statements
A variable is a name that stores a value. You create it with an assignment statement using =.
Example: storing and updating a value.
spam = 42
print(spam) # 42
spam = spam + 1
print(spam) # 43
spam = "Hello"
print(spam) # Hello (variables can change type)
Variable naming rules
Variable names must follow these rules:
- Can only contain letters, numbers, and underscores.
- Cannot start with a number.
- Cannot be a Python keyword (like
if,for,class).
my_name = "Alice" # Valid
_count = 10 # Valid
item2 = "book" # Valid
# 2things = "nope" # Invalid: starts with a number
# class = "nope" # Invalid: class is a keyword
By convention, variable names use snake_case in Python.
Comments
A comment starts with #. Python ignores everything after it on that line. Comments are notes for the programmer.
# This is a comment
spam = 1 # This is an inline comment
# Comments help explain what code does:
# Calculate the total cost with tax
total = price * 1.08
The print() function
print() displays a value on the screen. It can take multiple arguments separated by commas.
print("Hello, world!") # Hello, world!
print("My age is", 29) # My age is 29
print("cats", "dogs", "mice") # cats dogs mice
The input() function
input() pauses the program and waits for the user to type something. It always returns a string.
Example: ask the user for their name.
name = input("What is your name? ")
print("Hello, " + name)
Because input() always returns a string, you need to convert it if you want a number:
age = input("What is your age? ") # age is a string like '29'
age = int(age) # now age is the integer 29
The len() function
len() returns the number of characters in a string (or the number of items in a list).
len("hello") # 5
len("") # 0
len("Hi there") # 8 (spaces count)
Type conversions: str(), int(), float()
You can convert values between types using these functions. This is useful when combining user input (always a string) with numbers.
str(29) # '29'
int("42") # 42
float("3.14") # 3.14
int(7.8) # 7 (truncates, does not round)
float(10) # 10.0
A practical example:
age = input("Enter your age: ") # '25'
years_left = 100 - int(age) # 75
print("You have " + str(years_left) + " years until 100.")
Your first program
The book walks through writing and saving a .py file that greets the user and does simple math with their input.
# This program says hello and asks for my name.
print("Hello, world!")
my_name = input("What is your name? ")
print("It is good to meet you, " + my_name)
print("The length of your name is:")
print(len(my_name))
my_age = input("What is your age? ")
print("You will be " + str(int(my_age) + 1) + " in a year.")
The round() and abs() functions
round() rounds a number to a given number of decimal places. abs() returns the absolute (positive) value.
round(3.14159, 2) # 3.14
round(2.5) # 2 (banker's rounding)
abs(-42) # 42
abs(42) # 42
How computers store data with binary
Computers represent all data in binary (base-2, only 0s and 1s). A single 0 or 1 is a bit; eight bits make a byte. Python lets you write binary literals with the 0b prefix.
0b1010 # 10 in decimal
0b111 # 7 in decimal
bin(13) # '0b1101'
int("1101", 2) # 13
Overall idea of the chapter
The chapter's main message is: Python programs are built from expressions, data types, variables, and a handful of built-in functions (print, input, len, str, int, float, round, abs, type). Understanding how values, types, and operators work together is the foundation for everything that follows.