Programming Basics - SSS 2 Note

Programming Basics - SSS 2 | CMPNOTE

PROGRAMMING BASICS

Subject: Digital Technologies
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:

  1. Declare and use variables in simple programs.
  2. Identify common data types used in programming.
  3. Apply arithmetic, comparison and logical operators in simple programs.
  4. 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

Variable: A variable is a named reference used in a program to store or refer to a value so that the value can be used and, where appropriate, changed during program execution.

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:

  • name refers to the text value "Ada".
  • age refers to the integer value 16.
  • score refers to the integer value 85.
  • price refers to the decimal value 250.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

  1. Storing data: Variables hold values such as names, ages and scores.
  2. Performing calculations: They store numbers used in calculations.
  3. Making programs easier to understand: Meaningful names help explain what values represent.
  4. Updating values: A variable can refer to a new value during program execution.
  5. 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, score and Score are different names.
  • Python keywords, such as if and for, should not be used as variable names.
  • Use meaningful names such as student_name and total_price.
Remember: Python allows a variable to be assigned a value without declaring its data type in advance. The type is determined by the value assigned.

3. Common Data Types

Data type: A data type describes the kind of value a variable or expression represents and helps determine the operations that can be performed on it.

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

Operator: An operator is a symbol or keyword that performs an operation on one or more values.

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.

Important difference: The single equals sign (=) 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

An expression is a combination of values, variables and operators that Python evaluates to produce a value.

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

A statement is an instruction that Python can execute.

Examples include assignment statements and output statements.

price = 500
quantity = 3
total = price * quantity
print(total)

In this program:

  • price = 500 assigns a value to a variable.
  • quantity = 3 assigns another value.
  • total = price * quantity calculates 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:

  1. The program stores the student's name, test score and examination score in variables.
  2. The arithmetic operator + calculates the total score.
  3. The comparison operator >= checks whether the total is at least 50.
  4. The logical operator and checks whether the score is both at least 70 and at most 100.
  5. The print() function displays the results.
Practical task: Change the test score to 20 and the examination score to 25. Run the program again and observe how the total and Boolean results change.

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

  1. What is a variable, and state three uses of variables in programming.
  2. List four common Python data types and give one example of each.
  3. State five arithmetic operators and explain their uses.
  4. What is the difference between = and ==?
  5. What result does a comparison operator produce in Python?
  6. Explain the uses of and, or and not.
  7. Differentiate between an expression and a statement.
  8. 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:

  1. Store a student's name in a string variable.
  2. Store the quantity of exercise books purchased in an integer variable.
  3. Store the price per exercise book in a numeric variable.
  4. Calculate and display the total cost.
  5. Use a comparison operator to check whether the total cost is greater than ₦1,000.
  6. 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.

1. What is a variable in programming?

2. Which of the following is a Python string?

3. Which data type represents a whole number such as 25?

4. What is the result of 6 * 4?

5. Which operator checks whether two values are equal?

6. Which logical operator requires both conditions to be True for the combined result to be True?

7. What is the result of 10 > 7 in Python?

8. Which of the following is a valid Python variable assignment?

9. What is an expression?

10. Which Python function can display a result on the screen?

Comments

Popular posts from this blog

Complete Computer Studies/ICT Curriculum for JSS 1 to SSS 3

90 Objective Examination Questions in Major Subjects

Number Base System