Variables and Data Types
In Python, a variable is like a labeled box that stores a value (such as a number or text). You can name these variables to keep track of data. For example, score = 85 stores the number 85 in a variable called score. Python is dynamically typed, meaning you don't declare variable types explicitly; the type (integer, string, etc.) is determined by the value you assign.
Common data types in Python include:
- Integer (int): Whole numbers, e.g.
n = 42. - Float: Decimal numbers, e.g.
price = 17.50. - String (str): Text enclosed in quotes, e.g.
name = "Juan". - Boolean (bool):
TrueorFalsevalues, e.g.is_raining = False.
Understanding types is important because you can only perform certain operations on certain types. For example, 5 + 3 gives 8, but "5" + "3" (string addition) gives "53" (concatenation).
Operators and Expressions
Operators let us do calculations or comparisons. Key operator types:
- Arithmetic operators:
+,-,*,/,%(modulo),**(power). Example:3 * 4results in12. - Relational (comparison) operators:
==,!=,<,>,<=,>=. These produce Boolean results. Example:5 > 3isTrue. - Logical operators:
and,or,not(combine Boolean conditions). Example:True and FalseisFalse.
An expression is a combination of variables, literals, and operators that evaluates to a value. For instance, total = price * quantity uses the arithmetic * operator. Remember operator precedence (multiplication before addition) when writing expressions, or use parentheses to make your intent clear: (a + b) * c.
Input and Output
ProReviewer — locked
Drills, code labs, and full solutions.
Module Summary
ProReviewer — locked
Drills, code labs, and full solutions.
Practice & Exam Drills — Lesson 10
ProReviewer — locked
Drills, code labs, and full solutions.