Grade 12 Computer Science: Introduction to Programming Notes (Kenya) | YNetStudyHub

Introduction to Programming

Grade 12 · Computer Science 3 min read

Introduction

In computer science, programming is the process of designing and building computer programs to solve specific problems. It involves writing instructions that a computer can follow to perform a certain task. Programming languages like Python, Java, and C++ are used to write these instructions. Understanding the basics of programming is essential for anyone looking to pursue a career in software development or any field related to technology.

Variables

Variables are used to store data in a program. They have a name and a value, which can be changed during the program's execution. In Python, variables are created by assigning a value to a name. For example:

x = 5

In this example, x is a variable that stores the value 5.

Conditional Statements

Conditional statements are used to make decisions in a program based on certain conditions. The if statement is a common conditional statement that executes a block of code if a specified condition is true. For example:

age = 18
if age >= 18:
    print("You are an adult")

In this example, the program will print "You are an adult" because the condition age >= 18 is true.

Loops

Loops are used to repeat a block of code multiple times. There are two main types of loops: for loops and while loops. A for loop is used when the number of iterations is known, while a while loop is used when the number of iterations is not known in advance. For example:

for i in range(5):
    print(i)

This for loop will print the numbers 0 to 4.

Functions

Functions are reusable blocks of code that perform a specific task. They help break down a program into smaller, more manageable parts. Functions take inputs, called parameters, and return outputs. For example:

def add_numbers(a, b):
    return a + b

result = add_numbers(3, 5)
print(result)

In this example, the add_numbers function takes two parameters a and b, adds them together, and returns the result.

Common Mistakes

  • Forgetting to initialize variables before using them can lead to errors in the program.
  • Misunderstanding the syntax of conditional statements and loops can cause unexpected behavior.
  • Not properly indenting code within loops and functions can result in syntax errors.

Key Points

  • Variables are used to store data in a program.
  • Conditional statements are used to make decisions based on certain conditions.
  • Loops are used to repeat a block of code multiple times.
  • Functions are reusable blocks of code that perform a specific task.

Practice Questions

  1. Write a Python program that checks if a number is positive, negative, or zero. (Answer: See below)
  2. Write a Python program to find the sum of all numbers in a list. (Answer: See below)
  3. What is the output of the following code snippet?
x = 10
if x > 5:
    print("Greater than 5")
elif x == 5:
    print("Equal to 5")
else:
    print("Less than 5")

(Answer: "Greater than 5") 4. Explain the difference between a for loop and a while loop in programming. 5. Define a function in Python that takes a list of numbers as input and returns the sum of all even numbers in the list.

Practice Questions Answers

num = 10 if num > 0: print("Positive") elif num < 0: print("Negative") else: print("Zero")

2. ```python
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
    total += num
print(total)

These revision notes cover the basics of programming, including variables, conditional statements, loops, and functions. Practice the provided questions to solidify your understanding of these concepts.

Want to save these Introduction to Programming notes?

Create a free account to bookmark notes, download past papers, track your revision and get AI study help - free for Kenyan students.

Save & bookmark notes Download past papers Track revision progress AI study help
Create free account

Already have one? Log in

Frequently Asked Questions

Introduction to Programming is a Grade 12 Computer Science topic. This page gathers clear, exam-focused notes and revision material for it, all free to read online.

Yes - every note on this page is free to read online on YNetStudyHub, with no sign-up required.

Read the notes below, write a short summary of each in your own words, then practise related questions from the Computer Science past papers to check your understanding.

Other Grade 12 Computer Science topics

Get free notes & past papers by email

Join our list and we'll send fresh study notes and past papers straight to your inbox.