Form 4 - Computer
2024-2025
Floor 4 - Computer Room
Mr. Peter
Python Variables
Santa Rosa de Lima English Secondary School
聖羅撒英文中學
Colégio de Santa Rosa de Lima - Secção Inglesa
Outline
What is Variable in Python
1.
Review the concept of variable in Python
The naming rules in Python variable
2.
Focusing on the syntax of variable
Python variable implementation
3.
Declare the variable for calculations
What is variable?
What is variable in Python
Variable is a box
What is variable in Python
This box named "box"
box
What is variable in Python
This box can contain Integer
box
999999
What is variable in Python
To contain Integer, we can write like this in Python
box
999999
box = 999999
What is variable in Python
This box can contain String
box
"Apple"
What is variable in Python
To contain String, we can write like this
box
"Apple"
box = "Apple"
What is variable in Python
To show what value inside a box (variable), we can use print( ) function
box
"Apple"
print( box )
Apple
Output
Variable Naming Rules
Start with a Letter or Underscore
1.
A variable name must begin with a letter (a-z, A-Z) or an underscore (_).
Followed by Letters, Numbers, or Underscores
2.
After the first character, you can use letters, numbers, or underscores.
Case Sensitive
3.
Variable names are case sensitive (myVar and myvar are different).
Variable naming rules in Python
No Reserved Words
4.
You cannot use Python's reserved keywords as variable names (e.g., if, else, while).
No Spaces
5.
Variable names cannot contain spaces.
Variable naming rules in Python
1variable = 10
A variable name can begin with a number.
Please determine whether the following names are correct or not:
Variable naming rules in Python
variable_1 = 10
Variable names can contain underscores.
Please determine whether the following names are correct or not:
Variable naming rules in Python
_1_variable = 10
Variable names can contain underscores.
Please determine whether the following names are correct or not:
Variable naming rules in Python
my variable = 10
Python variable names can include spaces.
Please determine whether the following names are correct or not:
Variable naming rules in Python
var@name = 10
Special characters like @, #, and $ can be used in variable names.
Please determine whether the following names are correct or not:
Variable naming rules in Python
if = 10
Reserved keywords can be used as variable names.
Please determine whether the following names are correct or not:
Variable naming rules in Python
if_while = 10
Two Reserved keywords mixing together can be used as variable names.
Please determine whether the following names are correct or not:
Python Variable Exercises
Python Variable Exercises - Ex01
Create a formula to calculate the compound interest using Python operators. Compound interest is calculated on the initial principal and also on the accumulated interest of previous periods.
1.
A = P(1 + \frac{r}{n})^{nt}
A = 1000 \cdot (1 + \frac{0.05}{12})^{12 \cdot 5} \approx 1283.36
- Implement and calculate the following equation in Python
Save your file as "ClassNumber_YourName_Ex01.py"
Python Variable Exercises - Ex02
Implement a quadratic equation solver using Python variables and operators. The quadratic formula is used to find the roots of a quadratic equation in the form ax² + bx + c = 0.
2.
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
x = \frac{-5 + \sqrt{5^2 - 4 \cdot 1 \cdot 6}}{2 \cdot 1 } = -2.0
- Implement and calculate the following equation in Python
Save your file as "ClassNumber_YourName_Ex02.py"
F4 - Lesson02
By Mr Peter
F4 - Lesson02
- 48