Programming Basics - SSS 2 Note
PROGRAMMING BASICS
Class: SSS 2
Term: Second Term
Week: 3
Topic: Programming Basics
Duration: 80 Minutes (Double Period)
Learning Objectives
By the end of the lesson, students should be able to:
- Declare and use variables in simple programs.
- Identify common data types used in programming.
- Apply arithmetic, comparison and logical operators in simple programs.
- Explain and write simple expressions and statements.
1. Introduction to Programming Basics
Programming is the process of writing instructions that a computer can execute to perform tasks. These instructions are written using a programming language such as Python or JavaScript.
In the previous lessons, we introduced Python and JavaScript and learned how to write and run simple programs. To develop more useful programs, we need to understand variables, data types, operators, expressions and statements.
For example, a program that calculates the total cost of school supplies must store prices, add values together and display the result. Variables and operators make these tasks possible.
2. Variables and Their Uses
Variables allow programmers to keep values under meaningful names rather than repeatedly writing the same values in a program.
2.1 Examples of Variables in Python
name = "Ada"
age = 16
score = 85
price = 250.50
print(name)
print(age)
print(score)
print(price)
In this example:
namerefers to the text value"Ada".agerefers to the integer value16.scorerefers to the integer value85.pricerefers to the decimal value250.50.
The equals sign (=) is the assignment operator in Python. It assigns the value on the right to the variable on the left.
2.2 Uses of Variables
- Storing data: Variables hold values such as names, ages and scores.
- Performing calculations: They store numbers used in calculations.
- Making programs easier to understand: Meaningful names help explain what values represent.
- Updating values: A variable can refer to a new value during program execution.
- Reducing repetition: A stored value can be reused in different parts of a program.
2.3 Rules for Naming Variables in Python
- A variable name may contain letters, numbers and underscores.
- A variable name must not begin with a number.
- Spaces are not allowed in variable names.
- Python variable names are case-sensitive. For example,
scoreandScoreare different names. - Python keywords, such as
ifandfor, should not be used as variable names. - Use meaningful names such as
student_nameandtotal_price.
3. Common Data Types
Programming languages work with different kinds of data. Four common Python data types are integers, floating-point numbers, strings and Boolean values.
| Data Type | Meaning | Example |
|---|---|---|
Integer (int) |
Whole numbers without a decimal point. | age = 16 |
Floating-point (float) |
Numbers that contain a decimal point or represent decimal values. | height = 1.75 |
String (str) |
Text enclosed in quotation marks. | name = "Ada" |
Boolean (bool) |
A logical value that is either True or False. |
is_registered = True |
3.1 Integer
An integer is a whole number. It may be positive, negative or zero.
students = 35
temperature_change = -2
zero_value = 0
3.2 Floating-Point Number
A floating-point number represents a number with a fractional or decimal part.
item_price = 250.50
average_score = 72.5
3.3 String
A string is a sequence of characters used to represent text. In Python, strings are commonly enclosed in single or double quotation marks.
student_name = "Chika"
subject = 'Digital Technologies'
3.4 Boolean
A Boolean value represents one of two logical states: True or False. Boolean values are useful when a program needs to make decisions.
is_present = True
has_paid = False
3.5 Checking the Type of a Value
Python provides the type() function to identify the type of a value.
age = 16
price = 250.50
name = "Ada"
is_present = True
print(type(age))
print(type(price))
print(type(name))
print(type(is_present))
The output identifies the values as int, float, str and bool, respectively.
4. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations in a program.
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
Addition | 8 + 3 |
11 |
- |
Subtraction | 8 - 3 |
5 |
* |
Multiplication | 8 * 3 |
24 |
/ |
Division | 8 / 2 |
4.0 |
// |
Floor division | 8 // 3 |
2 |
% |
Remainder (modulus) | 8 % 3 |
2 |
** |
Exponentiation | 2 ** 3 |
8 |
Example: Calculating the Total Cost
exercise_books = 3
price_per_book = 500
total_cost = exercise_books * price_per_book
print(total_cost)
Output:
1500
The program multiplies the number of exercise books by the price of each book and stores the result in total_cost.
5. Comparison Operators
Comparison operators compare two values. In Python, a comparison produces a Boolean result: True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 8 > 4 |
True |
< |
Less than | 2 < 6 |
True |
>= |
Greater than or equal to | 5 >= 5 |
True |
<= |
Less than or equal to | 3 <= 2 |
False |
Example: Comparing Students' Scores
student_score = 75
pass_mark = 50
print(student_score >= pass_mark)
Output:
True
The result is True because 75 is greater than or equal to 50.
=) assigns a value to a variable, while the double equals sign (==) checks whether two values are equal.
6. Logical Operators
Logical operators combine or reverse Boolean conditions. They are useful when a program must consider more than one condition.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and |
True when both conditions are True. | 5 > 2 and 8 > 4 |
True |
or |
True when at least one condition is True. | 5 < 2 or 8 > 4 |
True |
not |
Reverses a Boolean value. | not (5 > 2) |
False |
Example: Checking Two Conditions
age = 16
has_permission = True
can_join = age >= 15 and has_permission
print(can_join)
Output:
True
Both conditions are true, so the expression produces True. This is a classroom example of a logical condition, not a statement of any real admission or permission policy.
7. Simple Expressions and Statements
7.1 Expressions
Examples of expressions include:
10 + 5
price * quantity
score >= 50
age >= 15 and has_permission
The expression 10 + 5 produces the value 15. The expression score >= 50 produces either True or False, depending on the value of score.
7.2 Statements
Examples include assignment statements and output statements.
price = 500
quantity = 3
total = price * quantity
print(total)
In this program:
price = 500assigns a value to a variable.quantity = 3assigns another value.total = price * quantitycalculates and stores the total.print(total)displays the result.
8. Practical Program: Calculating and Checking a Score
The following program combines variables, data types, arithmetic operators, comparison operators and logical operators.
student_name = "Ada"
test_score = 35
exam_score = 45
total_score = test_score + exam_score
has_passed = total_score >= 50
high_score = total_score >= 70 and total_score <= 100
print("Student:", student_name)
print("Total score:", total_score)
print("Passed:", has_passed)
print("Score between 70 and 100:", high_score)
Expected output:
Student: Ada
Total score: 80
Passed: True
Score between 70 and 100: True
Explanation:
- The program stores the student's name, test score and examination score in variables.
- The arithmetic operator
+calculates the total score. - The comparison operator
>=checks whether the total is at least 50. - The logical operator
andchecks whether the score is both at least 70 and at most 100. - The
print()function displays the results.
9. Common Programming Errors
| Error | Example | Correction |
|---|---|---|
| Using a variable before assigning a value to it | print(total) before defining total |
Assign a value to total first. |
| Incorrect variable name | student score = 70 |
Use a name without spaces, such as student_score = 70. |
| Confusing assignment and comparison | Using = to test equality |
Use == to compare equality. |
| Forgetting quotation marks around text | name = Ada |
Write name = "Ada". |
| Using the wrong operator | Writing price + quantity to calculate total cost |
Use multiplication when calculating price per item times quantity. |
10. Summary
Variables are named references used to store or refer to values in a program. Common Python data types include integers, floating-point numbers, strings and Boolean values. Arithmetic operators perform calculations, comparison operators compare values, and logical operators combine or reverse conditions. Expressions produce values, while statements are instructions executed by the program. Understanding these concepts helps students write simple and useful programs.
11. Evaluation Questions
- What is a variable, and state three uses of variables in programming.
- List four common Python data types and give one example of each.
- State five arithmetic operators and explain their uses.
- What is the difference between
=and==? - What result does a comparison operator produce in Python?
- Explain the uses of
and,orandnot. - Differentiate between an expression and a statement.
- Write a Python statement that calculates the total cost of four items costing ₦250 each.
12. Assignment
Write a simple Python program for a school supplies shop. Your program should:
- Store a student's name in a string variable.
- Store the quantity of exercise books purchased in an integer variable.
- Store the price per exercise book in a numeric variable.
- Calculate and display the total cost.
- Use a comparison operator to check whether the total cost is greater than ₦1,000.
- Use a logical operator in an additional simple condition.
13. Interactive Revision Quiz
Choose one answer for each question and click Mark Quiz to check your result.
Comments
Post a Comment