2.1 Expressions Part 1
Chapter 2: Variables, Expressions, and Statements
Page 1: Introduction
Focus on expressions, variables, and statements in Python programming.
Material sourced from "Python for Everybody" course.
Page 2: Constants
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.
Page 3: Reserved Words
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.
Page 4: Variables
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
, laterx = 100
updatesx
.
Page 5: Python Variable Name Rules
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
Page 6: Statements and Expressions
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()
)
Page 7: Mnemonic Variable Names
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.
Page 8: Analyzing Code Examples
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)
.
Page 9: Assignment Statements
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.
Page 10: Variable Evaluation
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.
Page 11: Updating Variables
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.
Page 12: Conclusion
Summary of vital concepts of expressions, variables, and assignment in Python programming.