StayTalentReady

Variables, Data Types, and Operators

Outcome 2: Variables, Data Types, and Operators · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx

Objectives

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

Example 1: Applying operators: Given score = 87 and total = 100, compute the percentage and letter grade. percentage = score / total * 100 → 87.0 (float because / always returns float). letter = 'B' if percentage >= 80 else 'A' if percentage >= 90 else 'C'. Wait — this logic has a precedence issue; write it as: if percentage >= 90: letter = 'A' elif percentage >= 80: letter = 'B'. Note: 87.0 >= 80 is True, so letter = 'B'. Correct.
Example 2: Type conversion: user_input = input('Enter your age: ') returns a str even if the user types 17. To compare: if int(user_input) >= 18 converts the string to int first. Forgetting this causes: TypeError: '>' not supported between instances of 'str' and 'int'.

Common mistakes

Self-check

Try each one before you look. A miss here costs nothing and tells you exactly what to reread.

1. What is the data type of 0.0 in Python?
2. What does int('7') return in Python?
3. What is the result of 10 % 3 in Python?
4. Which Python naming convention is correct for a variable storing a student's GPA?
5. What error results from writing: result = 'Total: ' + 42 in Python?

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.

← Problem Analysis and AlgorithmsSelection Structures →

↑ Back to top