This document explains Python expressions and variables, including arithmetic operations, assignment, variable naming, and practical usage for storing and manipulating values.
This document covers Python expressions and variables, including arithmetic operations, assignment, variable naming conventions, and practical examples for storing and manipulating values efficiently.
Expressions in Python describe operations performed by the computer, such as arithmetic calculations. Variables are used to store and reuse values in code.
Expressions are combinations of operands and operators that produce a result.
+): Adds numbers. Example: 100 + 60 results in 160.-): Subtracts numbers. Example: 10 - 20 results in -10.*): Multiplies numbers. Example: 5 * 5 results in 25./): Divides numbers. Example: 25 / 5 results in 5.0; 25 / 6 results in approximately 4.167.//): Divides and rounds down to the nearest integer. Example: 25 // 6 results in 4.Python follows mathematical conventions, performing multiplication before addition unless parentheses change the order.
1# Example of order of operations
2result = (32 + 32) * 60 # result is 1920
Variables store values for later use. Assignment uses the equal sign (=).
1my_variable = 1 # Assigns 1 to my_variable
2my_variable = 10 # Updates my_variable to 10
The previous value is replaced by the new assignment.
Variables can store results of expressions:
1x = 10 + 20 + 30 # x is 60
2y = x / 22.5 # y is 2.666...
3x = y # x is now 2.666...
Using meaningful variable names helps track code purpose and improves readability.
_) to separate words: total_mintotal_hourExample:
1total_min = 142
2# Convert minutes to hours
3total_hour = total_min / 60 # total_hour is approximately 2.367
Changing total_min updates total_hour without modifying other code.
Python expressions and variables enable efficient calculations and data management. Understanding operators, assignment, and naming conventions helps write clear, maintainable code.
(4) The caret (^) is not used for exponentiation in Python; use ** instead.
| Concept | Description |
|---|---|
| A. Operand | 1. Symbol representing an operation |
| B. Operator | 2. Value used in an expression |
| C. Assignment | 3. Storing a value in a variable |
| D. Variable | 4. Name used to store a value |
A-2, B-1, C-3, D-4.
Changing the value of a variable in Python automatically updates dependent calculations.
True. Updating a variable changes the result of any calculation that uses it.