Focus on expressions, variables, and statements in Python programming.
Material sourced from "Python for Everybody" course.
Definition: Fixed values that do not change.
Numeric constants: typical numbers (e.g., integers, floating-point numbers).
String constants: text values denoted by single (') or double (") quotes.
Examples:
print(123)
outputs 123.
print(98.6)
outputs 98.6.
print('Hello world')
outputs Hello world.
Definition: Keywords that have specific meanings in Python; cannot be used as variable names.
Examples of Reserved Words:
False, None, True, and, or, if, for, while, try, def, class, import
Reserved words help to maintain the syntax and structure of the programming language.
Definition: A variable is a named storage location in memory for data that can be modified.
Variable Naming: Programmers choose variable names which are retrievable.
Example Usage:
Assigning values: x = 12.2
, y = 14
, later x = 100
updates x
.
Naming Conventions:
Must start with a letter or underscore (_).
Allowed characters: letters, numbers, and underscores.
Case Sensitive (e.g., spam
, Spam
, SPAM
are distinct).
Valid Examples: spam, eggs, spam23, _speed
Invalid Examples: 23spam, #sign, var.12
Assignment Statement: Stores a value in a variable, e.g., x = 2
.
Assignment with Expression: x = x + 2
, adds 2 to the current value of x.
Print Function: Displays information to output, e.g., print(x)
.
Components:
Variable: (e.g., x
)
Operator: (e.g., =
, +
)
Constant: (e.g., 2
)
Function: (e.g., print()
)
Concept: Naming variables in a way that reminds programmers of their purpose (memory aids).
Comment: Careful naming can confused beginners as well-named variables may resemble reserved keywords.
Code Overview:
Assigning and calculating values using variables.
x1q3z9ocd = 35.0
, x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
Print output: print(x1q3p9afd)
.
Reformulated variables:
a = 35.0
, b = 12.50
, c = a * b
, print(c)
.
More descriptive example:
hours = 35.0
, rate = 12.50
, pay = hours * rate
, print(pay)
.
Definition: Assigns a value to a variable using syntax variable = expression
.
Example: x = 3.9 * x * (1 - x)
evaluates and assigns the result to x.
Memory Location Concept: A variable is a location that stores values, e.g., if x = 0.6
, evaluates the expression on the right and assigns results.
Evaluated from the right-hand side and stored on the left-hand variable.
A variable's value can be updated when a new calculation is made.
Example of updating: replacing old 0.6
value with a new value, 0.936
.
Demonstrates that values in a variable are not fixed and can change with new evaluations.
Summary of vital concepts of expressions, variables, and assignment in Python programming.