2 Variables, Data Types, and Expressions

Build a practical foundation in Python variables, data types, expressions, assignment, conversion, and input-output processing.

Names, Values, and Literals

Programming begins with three connected ideas: values hold information, names make that information reusable, and operations transform it. A gives a value a name, while a is a value written directly in source code.

For example, a program might associate student_name with the string "Ada" and score with the integer 95. A can then be reused in another , such as final_score = score + bonus.

Naming guidelines

  • Use letters, digits, and underscores, but usually do not begin a name with a digit.

  • Do not use spaces, punctuation, or reserved words such as if, class, or return as names.

  • Remember that names are often case-sensitive: score and Score may refer to different names.

  • In Python, prefer lowercase words separated by underscores, such as total_price.

  • Prefer descriptive names such as temperature, total_cost, and number_of_items over names such as x or thing.

Takeaway: Give each stored value a clear name so later expressions communicate its purpose.

Data Types and Appropriate Operations

A describes what a value represents and which operations make sense for it. Common types include:

  • Integer: a whole number, such as -3, 0, or 42.

  • Floating-point number: a number with a fractional part, such as 2.5 or -0.01.

  • Boolean: one of two logical values, True or False.

  • String: a sequence of characters, such as "hello" or "2026".

  • Null or none value: the absence of a meaningful value, represented by None in Python.

  • Collection: a group of values, such as [10, 20, 30].

Numbers support arithmetic, but floating-point values can have small rounding differences because some decimal fractions cannot be represented exactly in binary. For currency, decimal or fixed-point facilities may be more appropriate.

A string that looks like a number is still text: "42" is different from 42. Convert it before using it in a numeric calculation. Boolean values commonly result from comparisons, such as age >= 18.

Takeaway: Always consider both what a value contains and what its type allows you to do with it.

Expressions and Operators

An produces a value. It can combine literals, variables, operators, function calls, and nested expressions. For example, price * quantity produces a total, while age >= 18 produces a Boolean result.

Arithmetic operators

Common Python arithmetic operators include:

  • Addition: 7+2=97 + 2 = 9

  • Subtraction: 7−2=57 - 2 = 5

  • Multiplication: 7∗2=147 \mathbin{*} 2 = 14

  • Division: 7/2=3.57 / 2 = 3.5

  • Floor division: 7//2=37 \mathbin{//} 2 = 3

  • Remainder: 7%2=17 \mathbin{\%} 2 = 1

  • Exponentiation: 23=82^3 = 8, written as 2 ** 3 in Python.

Comparison operators produce Boolean values: == tests equality, != tests inequality, and <, <=, >, and >= compare order. Logical operators such as and, or, and not combine or modify Boolean expressions.

Do not confuse = with ==: the first performs , while the second tests equality.

Precedence

Multiplication is normally evaluated before addition, so the 2 + 3 * 4 produces 1414. Parentheses change the order: (2 + 3) * 4 produces 2020. Use parentheses when they make the intended calculation clearer.

Takeaway: Expressions calculate values; operators determine how values are combined and compared.

and Constants

stores a computed value in a . In Python, an uses =. In the statement count = count + 1, the current value of count is read, one is added, and the result replaces the previous value. This is a programming operation rather than a mathematical claim that a quantity equals itself plus one.

Python also provides shorthand : count += 1 has the same effect as count = count + 1, and price *= 1.10 multiplies the current price by 1.101.10 before storing the result. Multiple values can also be assigned together, as in width, height = 800, 600.

A is a value that a program intends not to change after it is defined. Python does not enforce constants through a special declaration, so uppercase names communicate the intention. Examples include TAX_RATE, MAX_ATTEMPTS, and MAX_SCORE.

Replacing a repeated unexplained with a named improves readability and makes future changes safer. For example, if a score is compared with a passing threshold, a name such as PASSING_SCORE explains the meaning of that threshold.

Takeaway: Use to update state and meaningful constants to document stable concepts.

, Conversion, and

enters a program, and communicates a result. A reliable -processing- pattern has four stages:

  1. Read the required .

  2. Convert it to appropriate data types.

  3. Compute a result with expressions.

  4. Display or store the result.

In Python, () returns text. If a user enters a quantity, convert it with int(...); if the user enters a price, convert it with float(...). must be valid for the target type: converting "20" to an integer succeeds, but converting "hello" with int(...) raises an error. A robust program anticipates invalid and responds appropriately.

For a minutes-to-hours calculation, define TOTAL_MINUTES as 60. Given an integer minutes, complete hours can be calculated with minutes//60minutes \mathbin{//} 60, and the remaining minutes with minutes%60minutes \mathbin{\%} 60. Thus, 125 minutes gives 125//60=2125 \mathbin{//} 60 = 2 complete hours and 125%60=5125 \mathbin{\%} 60 = 5 remaining minutes.

Formatted can combine text and computed values, for example by using an f-string such as f"{hours} hour(s) and {remaining_minutes} minute(s)". Explicit conversions and clear formatting help prevent errors when handling user , money, dates, or measurements.

Takeaway: Treat incoming text as text until it has been deliberately validated and converted, then use expressions to produce clear .