0% found this document useful (0 votes)
4 views26 pages

S3 Programming Note

The document provides an introduction to programming, covering various programming languages, the concept of programs and statements, and the importance of algorithms. It includes examples of algorithms for daily tasks and calculations, as well as an overview of Python, its uses, and basic syntax. Additionally, it discusses pseudo-code, data types, input/output operations, and control structures like if statements.

Uploaded by

yinminaye.quinn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

S3 Programming Note

The document provides an introduction to programming, covering various programming languages, the concept of programs and statements, and the importance of algorithms. It includes examples of algorithms for daily tasks and calculations, as well as an overview of Python, its uses, and basic syntax. Additionally, it discusses pseudo-code, data types, input/output operations, and control structures like if statements.

Uploaded by

yinminaye.quinn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Programming

There are many different programming languages, some more complicated and complex than others.
Among the most popular languages are:

• Python
• Java
• C++
• Small Basic
• Scratch

Different languages work in different ways.

What is a program?

Programs are made up of statements that the programming language knows and understands.

Just as words are put together to form a sentence, a program puts one or more statements together to
form an instruction. Each statement tells the computer to perform a specific task, and the instructions
tell a computer what to do.

Statements

Different programming languages use different statements. A few of these are listed in this table:

1
Introduction to programming

Programming is writing computer code to create a program, in order to solve a problem. Programs
consist of a series of instructions to tell a computer exactly what to do and how to do it.

Glossary:
breaking down a complex problem or system
Decomposing into smaller parts that are more manageable and
easier to understand.
A sequence of logical instructions for carrying
Algorithm
out a task.

Algorithms are used for many different things including calculations, data processing and automation.

2
Consider this simple problem. Getting Ready for School Algorithm. Decomposing this problem, gives this
algorithm:

Getting Ready for School Algorithm (Daily Task Example)


1. Wake up at the same time every day.
2. Get out of bed and get dressed in clean clothes.
3. Eat a healthy breakfast, such as cereal, fruit, or toast.
4. Brush your teeth and wash your face.
5. Pack your backpack with all the materials you need for the day.
6. Put on your shoes and coat.
7. Head out the door to go to school.

Write a one simple algorithm for daily tasks.


• Brushing Teeth Algorithm
Activity 1: • Washing Hands Algorithm
Class • Making a Bed Algorithm
Notebook • Reading a Book Algorithm
• Putting Away Books Algorithm
• Setting the Table for meal Algorithm

Algorithm for adding two numbers (Calculation Example)


1. Begin the program.
2. Prompt the user to input the first number.
3. Store the first number in a variable called "num1".
4. Prompt the user to input the second number.
5. Store the second number in a variable called "num2".
6. Add "num1" and "num2" together, and store the result in a variable called "sum".
7. Output a message to the user that displays the sum of the two numbers.
8. End the program.

Activity 2: Write a one simple algorithm for calculation.


Class • Subtraction Algorithm
Notebook • Multiplication Algorithm

3
Pseudo-code

Most programs are developed using programming languages. These languages have specific syntax that
must be used so that the program will run properly.

Pseudo-code is not a programming language. It is a simple way of describing a set of programming


instructions in a manner that resembles a programming language. Pseudo-code has its own syntax, some
of which is very similar to many actual programming languages. Any algorithms designed using pseudo-
code will not run unless they are converted into an actual programming language.
Here we are going to use Edexcel Pearson Pseudo-code command set

4
5
Adding two numbers
Prompt the user to input the first number.
Store the first number in a variable called "num1".
Prompt the user to input the second number.
Algorithm
Store the second number in a variable called "num2".
Add "num1" and "num2" together, and store the result in a variable called "sum".
Output a message to the user that displays the sum of the two numbers.
SEND ‘Enter the first number’ TO DISPLSY
RECEIVE num1 FROM KEYBOARD
SEND ‘Enter the second number’ TO DISPLSY
Pseudo-code
RECEIVE num2 FROM KEYBOARD
SET sum TO num1 + num2
SEND sum TO DISPLSY

Activity 3: Write a Pseudo-Code for Activity 2.


Class • SEND, TO, RECEIVE, SET, DISPLAY
Notebook

Advantages and disadvantages of pseudo-code

Advantages:

• it can be quickly and easily converted into an actual programming language as it is similar to a
programming language
• it is fairly easy to understand, even for non-programmers
• it does not matter if there are errors in the syntax - it is usually still obvious what is intended
• changes to the design can be incorporated quite easily

Disadvantages:

• It can be hard to see how a program flows.


• It can be time consuming to produce.

• Introduction to Programming
Assignment
• Attach in the Assignment Tab

6
What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:

• web development (server-side),


• software development,
• mathematics,
• system scripting.

What can Python do?

• Python can be used on a server to create web applications.


• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used to handle big data and perform complex mathematics.

Why Python?

• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English language.

Good to know

• The most recent major version of Python is Python 3.

Download Python
Python IDLE [Link]
PyCharm [Link]
Thonny [Link]
Visual Studio Code [Link]

Python can be used in two ways, Shell (interactive mode) and Editor (script mode). Where Interactive
Mode, as the name suggests, allows us to interact with OS; script mode let us create and edit python
source file. Now, we will first start with interactive mode. Here, we type a Python statement and the
interpreter displays the result(s) immediately.

7
• This is Python shell.

Execute Python Syntax ( File Extenstion )


.py

Python Turtle Graphic

Objectives
• Learn about the main commands of Turtle graphics
• Understand the x,y coordinates for mapping a drawing
• Draw simple shapes using turtle graphic

8
Turtle graphics is a popular way for introducing programming to studnets.

• Every program first line must be import turtle


• The turtle appears as an icon
• Initial position: (0, 0)
• Initial direction: East (0°)
• Colour: black
• Line width: 1 pixel
• Pen: down (ready to draw)

9
Turtle methods

Example : Draw a square


python code output

10
Activity – 4
Draw these shapes changing
[Link](4)

Example : Left angled triangle

python code output

11
Activity – 5
Draw Right angled triangle

Python Comments

• Comments can be used to explain Python code.


• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
• Comments starts with a #, and Python will ignore them.

Change the color of the ink of the turtle drawing

• The default color is black.

Syntax: [Link](‘red’)

Avaible Colors "white", "black", "red", "green", "blue", "cyan", "yellow", and "magenta"

Activity – 6
Draw Equallateral triangle with your favorite color.
Write a comment.

12
Output to the screen ( Python Syntax )

Syntax Output
print("Hello, World!") Hello, World!"

Activity – 7 ( a)

Write the program so it says Pleased to meet you.

Get new Line Character

the new line character “\n” is used to create a new line.

Syntax Output
Hello,
print("Hello, \n World!")
World!

Activity – 7 (b)
Write a program that says:

This is
a computer program
that prints on several lines

Activity – 7 (c)
Write a program that says:

This is John's first program

13
Data Type

Every variable has a given data type. The most common data types are:

String - Text made up of numbers, letters and characters.

Integer - Whole numbers. (e.g., 1, 78, 0 and -54)

Float / Real - Decimal numbers (e.g., 3.5683, 98.74634, -6.3)

Boolean - True or False

Check the data type

To determine the type of a variable in Python, use the built-in type () function.

Syntax Output
name='Bob'
<class 'str'>
print(type(name))

Activity – 7 (d )
Check the data type of a variable in Python:

14
Storing Data in Variables

• We use variables in our programs to store data.


• Each variable has a name and stores data of a certain type (string, integer, real etc.)
• Joining and printing variables using (+) sign.

Syntax Output
name='Bob'
Hello Bob
print('Hello '+ name)

Activity – 8 (a )
Complete the program:

language='Python'
print('I am learning to program in' [REMAINDER OF PROGRAM HERE]

We can change the value of a variable at any point. The code:


Syntax Output
name='Bob'
name='Liz' Hello Liz
print('Hello '+ name )

Will print Hello Liz (as the variable name has been overwritten with the value Liz). We can also change
the contents of a variable referring to itself:

Syntax Output
name='Bob'
name=name+'by' Hello Bobby
print('Hello '+name)

This will print Hello Bobby


Whereas
Syntax Output
name='Bob'
name=name+name Hello BobBob
print('Hello '+name)

Will print Hello BobBob

15
We can add spaces just like any other letter:

Syntax Output
name='Bob'
name=name+’ ‘ + name Hello Bob Bob
print('Hello '+name)

Will print Hello Bob Bob

Activity – 8 (b)
Complete the following program so it uses the variables to print out the cat sat on
the mat.
Remember you will need to add spaces.
E.g.

word1='the'
word2='cat'
word3='sat'
word4='on'
word5='mat'
[REMAINDER OF PROGRAM HERE]

A TypeError is a common error in Python that occurs when an operation or function is applied to an
object of an inappropriate type. Here are some examples of common causes of TypeError:

Syntax Output

age = 16
print ('I am ' + age)

16
Inputting Data
We can now look at starting to make our programs interactive. We are going to take in input from the
user using the input function.

Syntax Output

name=input ('What is your name?')


print ('Hello '+name)

Activity – 9 (a)
Write a program that
• asks for the city you live in and then replies I love visiting [city name]

Activity – 9 (b)
Write a program that
• asks for your first name and last name and finally greets you with your full name.

Activity – 9 (c)
Write a program that
• asks for your name then prints it out 5 times

17
Calculations

We can carry out calculations in Python. The arithmetic operators we use to do this are:

+ Addition
- Subtraction
* Multiplication
/ Division

Operators are special symbols which represents computation. Value and variables when used with
operator are known as operands.

Following is the partial list of operators:

Syntax Output
print (1+6)
print (7-5)
print (3*9)
print (7/2)

18
Activity – 10 (a)

• Add Two Numbers:

num1 = 1.5
num2 = 6.3

Then print out the result like

The sum of xxx and xxx is xxx

Activity – 10 (b)

• Ask two numbers from user using input ()


• Add Two Numbers:
• Print out the result like sample below.

The sum of xxx and xxx is xxx

Activity – 10 (c)

• Determine your age in the next year.

19
Activity – 11 (a)
• Ask a user their height (in cm)
• Convert it to feet
• Print a message like (xxxx ft).
• Formula: cm * 0.033

Activity – 11 (b)
• Ask a user currency amount they want to covert.
• Ask a user currency ISO code (US $, Pound):
• Ask a user currency exchange rate:
• Ask a exchange currency ISO code (US $, Pound)
• Formula: currency * exchange rate
• Print a message like
• (Amount ISO code will be xxx in ISO code)

Python String Length

To get the length of a string, use the len () function.


Syntax Output

class = "Secondary 3”
print(len(class))

Activity – 12

• Find out the length of your name then print out the result.

20
Flowchart

[Link]
TvFiqTXVQ/edit?utm_content=DAFTuSEByLI&utm_campaign=designshare&utm_medium=link2&utm_s
ource=sharebutton

Activities in Class Notebook


[Link]
[Link] 1
[Link] 2
[Link] 3

21
Relational Operators

Note: Two values that are of different data type will never be equal to each other.

Selection with IF

• Sometimes we only want a program to execute code under certain circumstances.


• if a condition is true execute some code.
• A boolean condition is one that can have only two values true or false.

The Boolean comparison operators are as follows:

Operator Meaning

== Equal to

!= Not equal to

< Less than


> Greater than

<= Less than or equal to

>= Greater than or equal to

Syntax Output
letter = 'a'
if letter=='a':
print('This prints if letter is a')

22
Activity – 13 (a)
• Ask a user to enter one number
• Check the number is positive
• If the condition TRUE, print out
• The number number is positive.

We can tell the computer to do something different when the condition isn’t true using the
else keyword

Simple If. (True or False)


letter = 'b'
if letter=='a':
print ('This prints if letter is a')

else:
Syntax
print ('The letter is not an a’)

print ('This prints out whatever the letter is')

Output

Activity – 13 (b)

• Ask a user to enter one number


• Check the number is positive or negative
• If the condition TRUE, print out
• The number number is positive.
• If the condition FALSE, print out
• The number number is negative.

23
Nested if
letter = 'c'
if letter=='a':
print('This prints if letter is a')
print('This prints as well')
elif letter=='b':
Syntax
print('This prints if the letter is b')
else:
print('This prints if the letter is neither a nor b')
print('This prints out whatever the letter is')

Output

Activity – 13 (c)

• Ask a user to enter one number


• Check the number is positive or negative or zero
• If the number is positive, print out
• The number number is positive.
• If the number is negative, print out
• The number number is negative.
• Else print out, The number is Zero.

Activity – 14

• Please check the question in Class Notebook.

Activity – 15

• Please check the question in Class Notebook.

24
Logical operators
• There are three types of logical operators: and, or, and not.

Operator Description Example Output

3 < 5 and 5 < 10 True


and Returns True if both statements are true
6 < 5 and 5 < 10 False

or Returns True if one of the statements is true 6 < 5 or 2 < 4 True

not Reverse the result, returns False if the result is true not( 2 < 5 and 1 < 10 ) False

Syntax Output
a = True
b = False

result_and = a and b
print("Logical AND:", result_and)

result_or = a or b
print("Logical OR:", result_or)

result_not = not a
print("Logical NOT:", result_not)

25
Activity – 16

• Please check the question in Class Notebook.

Activity – 17

• Please check the question in Class Notebook.

26

You might also like