Notes on Variable, Expression and Statement in Python Programming
Topic Learning Outcomes
- Relational Operators: Understand the use of relational operators in Python.
- Variables: Understand how to correctly use variables including naming conventions and reusability.
- Boolean Operators: Learn how to employ Boolean logic and operators in programming.
- Type Conversion: Grasp the concept of type conversion, including how to convert data types in Python.
Variables in Python
- Definition: A variable is a named memory location used to store data which can be retrieved later by using the variable name.
- Reusability: Once defined, the variable can store different values in different parts of the program (e.g.,
mileToKm = 1.60934). - In Python: Case Sensitivity is crucial. Variable names must be unique.
Creating Variables
- Example of creating variables:
number = 10 number = 2.5 (Re-assignment of value to number)a, b, c = 3, 3.5, "Python" (Multiple assignments)a = b = c = "I love Python" (Same assignment to multiple variables)
Constants
- Definition: Constants are variables whose values cannot change during execution. They serve as containers for fixed values.
- Example of declaring a constant in Python:
PI = 3.14- Using constants in other modules:
# filename: week4.py
import constant
print(constant.PI)
Naming Variables and Constants
- Rules:
- Combine letters (lowercase & uppercase), digits, and underscores.
- Avoid special symbols and starting names with digits.
- Use meaningful names (e.g.,
first_name, age). - Constants should be declared in uppercase (e.g.,
PI).
- Keywords (Reserved Words): Cannot be used as variable names (e.g.,
if, else, while, class).
Variable Types
- Variables can be of different types: Integer, Float, String, etc.
- Example of Type in Python:
ddd = 1 + 4 # ddd is of type integer
eee = 'hello ' + 'there' # eee is of type string
- Checking Type: Use
type(variable) to find out a variable's type.
Assignment Statements
- Assignment involves equating a variable to an expression:
- Example:
x = 3.9 * x * (1 - x)
- Explanation: This assigns the result of the expression on the right to
x.
Arithmetic and Numeric Operations
- Operations:
- Multiplication:
* - Addition:
+ - Exponentiation:
** (e.g., 2**3 = 8) - Integer Division:
// - Modulus:
%
- Floating Point Behavior: If a float and integer are mixed, the result is a float (e.g.,
1 / 2.0 = 0.5).
Expressions and Operator Precedence
- Definition of Expression: A combination of values and operators that compute a single value (e.g.,
1 + 4 * 3). - Precedence Rules:
- Parentheses
() > Exponentiation ** > Multiplication/Division/Remainder > Addition/Subtraction.
- Example of Evaluation Order:
x = 1 + 2 * 3 - 4 / 5 ** 6 # evaluated according to precedence rules.
Boolean Values and Comparison Operators
- Definition: Boolean values return True or False. Used in conditional statements.
- Operators:
== (equal to) != (not equal) > (greater than) and, or, not (logical operators).
- Example:
x = 5
x == 5 # returns True
x > 7 # returns False
Logic and Readability in Conditions
- Logical combinations using operators like
and enhance condition evaluation.- Example:
xDivisible = (x % 2) == 0.
- Always aim for readability by breaking down complex logical conditions across multiple lines.
Recap of Main Points
- Understanding variables, their types, assignment rules, naming conventions, and type conversions is crucial in programming with Python.
- Mastery of arithmetic operations and expressions, combined with logical operators, underpins effective conditional programming and decision-making.
- Practice making variable names meaningful and maintaining clear, readable code to enhance maintainability and collaboration.