Variables, Data Types, and Operators
Outcome 2: Variables, Data Types, and Operators · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx
Objectives
- Create variables in Python using assignment and identify the data type of a value.
- Distinguish between int, float, str, and bool and choose the correct type for a given value.
- Apply arithmetic, comparison, and assignment operators to form expressions.
- Convert between data types using int(), float(), and str() and explain when conversion is needed.
- Follow Python naming conventions for variables.
Key terms
- variable
- A named storage location in memory that holds a value; its value can be reassigned during execution.
- assignment operator
- The = symbol in Python, which binds a value to a variable name.
- int
- Python's integer type — whole numbers with no decimal component: ..., -2, -1, 0, 1, 2, ...
- float
- Python's floating-point type — numbers with a decimal component: 3.14, -0.5, 2.0.
- str
- Python's string type — a sequence of characters in single or double quotes: 'hello', "world".
- bool
- Python's Boolean type — exactly two values: True or False (capital first letter required).
- expression
- A combination of values, variables, operators, and function calls that evaluates to a single value.
- type conversion
- Changing a value's data type using built-in functions: int() truncates to whole number, float() adds decimal, str() produces text.
- arithmetic operator
- + (add), - (subtract), * (multiply), / (divide), // (integer divide), % (modulo), ** (exponent).
- comparison operator
- == (equal), != (not equal), < (less than), > (greater than), <= (less or equal), >= (greater or equal) — returns bool.
The concept
Variables are the building blocks of every program. A variable is a name you give to a storage location in memory. In Python, you create a variable simply by assigning it a value: age = 17. Python automatically determines the type based on the value on the right side of the equals sign. This is called dynamic typing.
Python has four fundamental data types you will use every day. An int is a whole number: 0, 42, -7. A float is a number with a decimal point: 3.14, -0.5, 2.0 (the .0 makes 2.0 a float even though its value is a whole number). A str is text enclosed in single or double quotes: 'hello', "Python". A bool is one of two values: True or False — note the capital first letter, which is required in Python.
Operators perform operations on values. Arithmetic operators do math: + adds, - subtracts, * multiplies, / divides (always producing a float), // divides and drops the decimal (integer division), % gives the remainder (modulo), and ** raises to a power. Comparison operators compare values and return a bool: == tests equality, != tests inequality, < > <= >= compare size. Assignment operators update a variable: += adds to the current value (x += 3 is shorthand for x = x + 3).
Type conversion is essential when combining different types. You cannot add an int and a str directly — 5 + '3' raises a TypeError. Use int('3') to convert the string to an integer first. The int() function truncates (drops the decimal) when converting a float: int(3.9) produces 3, not 4. Use float() to add a decimal, and str() to convert any value to its text representation for output.
Naming rules in Python: variable names must start with a letter or underscore, contain only letters, digits, and underscores, and may not use reserved keywords like if, for, while, True, False, class, or def. By convention, use lowercase with underscores for variable names (student_name, total_score). Names are case-sensitive: Score and score are different variables.
Blueprint Pillar 3 — Technology and Society: Variables mirror how humans categorize information — whole counts versus measurements, text versus numbers. Understanding data types prevents real-world errors like treating a phone number as an integer (losing leading zeros) or performing math on a zip code string. Every major data breach or financial calculation error in software history involves a type mismatch or incorrect variable assumption.
Worked examples
Common mistakes
- Using = (assignment) instead of == (equality test) inside a condition. if x = 5: is a SyntaxError. The condition must be if x == 5:.
- Forgetting that / always returns float. 10 / 2 returns 5.0, not 5. Use // for integer division: 10 // 2 returns 5.
- Mixing types without conversion. Concatenating a string and an int — 'Score: ' + score — raises TypeError. Use str(score) or an f-string: f'Score: {score}'.
Self-check
Try each one before you look. A miss here costs nothing and tells you exactly what to reread.
Canvas is the official record. This companion enhances the PGCC curriculum; it does not replace it. Last name and class year only. Students with a 504 plan or IEP: your accommodations apply.