SkarpSkarp

Chapter 3 of 11

Speaking Python: Variables, Data Types, and Basic Operations

Move beyond one-off commands and start teaching Python to remember things and do useful work. You’ll see how variables, numbers, and text fit together so you can write small snippets that already feel like real programs.

15 min readen

From One-Off Commands to Remembering Things

Moving Beyond One-Off Commands

You have run simple Python commands like `print("Hello")` or `2 + 3`. Those give instant results, but they do not remember anything after they run.

What Is a Variable?

A variable is like a labeled box in your computer's memory. You put a value in the box, give the box a name, and then use that name later in your code.

What You Will Learn

In this module you will: create and update variables, use basic data types (`int`, `float`, `str`, `bool`), do arithmetic, work with strings, and add comments to explain your code.

Variables and Assignment

Creating a Variable

You create a variable with assignment: write a name, then `=`, then a value. Example: `age = 15` creates a variable called `age` that stores the number 15.

Naming Rules

Variable names: use letters, numbers, and underscores; do not start with a number; no spaces; and they are case-sensitive, so `age` and `Age` are different.

Updating a Variable

You can change a variable by assigning again. If you write `age = 15` and later `age = 16`, the same variable name now remembers the new value 16.

Try It: Your First Variables

Run this code in your Python interpreter or editor. Read the comments to see what each line does.

```python

Store your age in a variable

age = 15

Store your name in a variable

name = "Alex"

Print a sentence using both variables

print("Name:", name)

print("Age:", age)

Update the age variable

age = age + 1 # increase age by 1

print("Next year you will be:", age)

```

Core Data Types: int, float, str, bool

Integers and Floats

int are whole numbers like `0`, `7`, `-3`. float are numbers with a decimal point like `3.14`, `0.5`, `-2.0`. Python decides based on how you write the number.

Strings

A str (string) is text inside quotes: `"Hello"`, `'Python'`, `"123"`. Even if it looks like a number, if it is in quotes, Python treats it as text.

Booleans

A bool (boolean) can only be `True` or `False`. Example: `is_student = True`. These are very useful for decisions in code.

Checking Types

Use `type()` to see a variable's type. Example: `score = 10`; `print(type(score))` will show that `score` is an `int`.

Numbers in Action: Arithmetic Operators

Basic Arithmetic

Python supports `+` (add), `-` (subtract), `*` (multiply), and `/` (divide). You can use them with variables, not just raw numbers.

Division Types

`/` gives a float result like `3.4`. `//` gives floor division, a whole number rounded down, like `3` if you divide 17 by 5.

Remainder and Powers

`%` gives the remainder, so `17 % 5` is `2`. `` is exponent, so `2 3` is `8`. These are useful for many math and coding tasks.

Pizza Example

With 17 slices and 5 friends, `17 / 5` is `3.4`, `17 // 5` is `3` whole slices each, and `17 % 5` is `2` leftover slices.

Think It Through: Integers vs Floats

Imagine you are tracking your study time this week.

  • On Monday you study 1.5 hours.
  • On Tuesday you study 2 hours.
  • On Wednesday you study 2.25 hours.
  1. Decide: which of these should be `int` and which should be `float` in Python? Why?
  2. In your head (or on paper), write variable names and values for each day. Example: `monday_hours = 1.5`.
  3. Add them up: what is your total study time?

Now, turn your plan into code in your editor and run it. Change one of the values to a whole number (like `2` instead of `2.0`) and check that Python still handles the math correctly.

Reflect: When might it be important to keep the decimal part (float) instead of rounding to an integer?

Working With Text: Strings and Methods

Creating Strings

Strings are text in quotes: `"Hello"` or `'Python'`. Example: `first_name = "Jordan"` stores a name as a string.

Joining Strings

Use `+` to concatenate (join) strings. Example: `fullname = firstname + " " + last_name` adds a space between first and last name.

Changing Case

String methods like `upper()` and `lower()` create new versions of the string in all caps or all lowercase. Example: `full_name.upper()`.

Strings vs Numbers

You can only `+` strings with strings. To join text and a number, convert the number: `"Age: " + str(age)`.

Booleans: True or False

What Is a Boolean?

A boolean (`bool`) is a value that is either `True` or `False`. It is used for yes/no style information in your programs.

Examples of Booleans

Example variables: `isstudent = True`, `passedtest = False`. These can later be used in conditions like `if is_student:`.

Why They Matter

Booleans help your code make decisions. For now, remember they only have two values, `True` and `False`, and the first letter must be capitalized.

Comments: Explaining Your Code to Humans

What Is a Comment?

A comment is text in your code that Python ignores. In Python, everything after `#` on a line is a comment for humans, not for the computer.

Comment Example

Example: `# This program calculates area` or `width = 5 # width in meters`. These notes explain what the variables and code mean.

Why Use Comments?

Comments help you and others understand the code later. Focus on explaining why you do something, not just restating the code.

Quick Check: Variables and Types

Test your understanding of variables and basic data types.

Which line of code will cause an error in Python?

  1. city = "Paris"
  2. age = 16
  3. pi = 3.14
  4. greeting = "Hello" + 5
Show Answer

Answer: D) greeting = "Hello" + 5

`"Hello" + 5` tries to add a string and an integer, which is not allowed. You must convert the number to a string first, like `"Hello" + str(5)`.

Quick Check: Arithmetic Operators

Another short question to reinforce your understanding of division and remainder.

What is the value of `17 // 4` and `17 % 4`?

  1. `17 // 4` is 4 and `17 % 4` is 1
  2. `17 // 4` is 4 and `17 % 4` is 0
  3. `17 // 4` is 4 and `17 % 4` is 5
  4. `17 // 4` is 4.25 and `17 % 4` is 1
Show Answer

Answer: A) `17 // 4` is 4 and `17 % 4` is 1

`17 // 4` is floor division, so it gives the whole number part 4. `17 % 4` is the remainder when 17 is divided by 4, which is 1.

Key Terms Review

Flip through these flashcards to review the main ideas from this module.

Variable
A named storage location in memory that holds a value, like `age = 15`. You can use the name later to access or change the value.
Assignment
The act of giving a value to a variable using `=`, for example `score = 10`.
int
Integer type in Python: whole numbers without a decimal point, such as `-3`, `0`, `42`.
float
Floating-point type in Python: numbers with a decimal point, such as `3.14`, `0.5`, `-2.0`.
str (string)
Text data type in Python, written inside quotes, for example `"Hello"` or `'Python'`.
bool (boolean)
Logical data type with only two values: `True` or `False`.
Concatenation
Joining strings together using `+`, such as `"Hello" + " " + "World"`.
Comment
A note in the code that Python ignores, starting with `#`. Used to explain what the code does.
Floor division `//`
Division that returns the whole number part, rounded down. Example: `17 // 5` is `3`.
Modulo `%`
Operator that returns the remainder of a division. Example: `17 % 5` is `2`.

Key Terms

int
Python's integer type for whole numbers without decimals.
str
Python's string type for text wrapped in quotes.
bool
Python's boolean type with only two values: `True` or `False`.
float
Python's floating-point type for numbers with decimal points.
modulo
The `%` operator that returns the remainder from dividing one number by another.
comment
Text in code that starts with `#` and is ignored by Python, used to explain the code.
variable
A named storage location in memory that holds a value you can read or change while the program runs.
assignment
Using `=` to store a value in a variable, for example `x = 5`.
concatenation
Joining two or more strings together using the `+` operator.
floor division
Division using `//` that returns the integer part of the result, rounded down.

Finished reading?

Test your understanding with a custom practice exam on this chapter.

Test yourself