Introduction to Programming | Knowledge Hub Introduction to Programming | Knowledge Hub
Unlock Premium - notes, past papers & AI tutoring for as low as KSh 199/month. Subscribe Now →
Computer Science

Introduction to Programming

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.

Was this helpful?

Get new Computer Science notes by email

Join thousands of Kenyan students. We'll send fresh Computer Science notes, past papers and revision tips - free, no spam.

By subscribing you agree to our Privacy Policy.

Comments

Log in to leave a comment.

No comments yet. Be the first to comment!

Join free to unlock more

Bookmark articles, download past papers, track 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

Related Articles
Learning Tools
MCQs: 0
Flashcards: 0
Practice Problems: 0
Browse formula sheets
Study Assistant

Instant help with course questions

Hi there! I'm your YnetStudyHub assistant. How can I help with your studies today?