0% found this document useful (0 votes)
3 views69 pages

Programming

This document provides an overview of programming languages, specifically focusing on Python, its syntax, data types, and programming constructs such as selection and iteration. It covers key concepts like variable declaration, assignment, arithmetic operations, and built-in functions, as well as the importance of parameters in functions and procedures. The document serves as a foundational guide for understanding programming principles and Python's unique features.
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)
3 views69 pages

Programming

This document provides an overview of programming languages, specifically focusing on Python, its syntax, data types, and programming constructs such as selection and iteration. It covers key concepts like variable declaration, assignment, arithmetic operations, and built-in functions, as well as the importance of parameters in functions and procedures. The document serves as a foundational guide for understanding programming principles and Python's unique features.
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

Chapter 1

4
Programmin
g Language

A LEVEL COMPUTER
Prepared and taught by James Gan
James Gan SCIENCE
14.1 14.2 14.3 14.4 14.14 14.13
Programming Programming Data Boolean Text files Arrays
Language basics Types Expressions

14.12
14.5 Passing
Selection parameters
to
procedures

Chapter 14.6
14.11
Passing

Outline
Iteration parameters
to functions

14.10
14.7 Passing
14.8 14.9
Built-in parameters to
Procedures Functions subroutines
functions
14.1 Programming Languages

Why are programming languages needed?


Programming languages are necessary as they enable the translation of abstract algorithmic concepts
represented by pseudocode and flowcharts into executable instructions that a computer can understand and
execute.
14.1 Programming Languages

Recap on construct
When writing an algorithm, we use four basic types of construct:

Iteration
Assignment Sequence Selection
Iterating or looping through a
Assigning a value to a variable Execution of instructions in a Making decisions in a program set of instructions multiple
in a programming language. predetermined order, one after based on conditions (e.g., times until a certain condition
another. if-else statements). is met (e.g., for loops, while
loops).

Data is involved in almost every problem we are trying to solve, hence we also need the input and output
statements.

Input Output
14.1 Programming Languages

Python
Python is a multi-paradigm programming language that supports procedural (this chapter), object-oriented
(c27), and functional (c29) programming approaches, allowing diverse coding styles within a single
language.
14.1 Programming Languages

Download python here!


14.1 Programming Languages

Key characteristics of Python:


• Case Sensitive: Python is case-sensitive, meaning it distinguishes between uppercase and lowercase letters
in variable names and identifiers.
• Mandatory indentation: Python uses indentation to define code blocks, making it mandatory for
maintaining readability and determining the structure of the code.
• Keywords in Lower Case: Python's keywords (e.g., if, else, while) are written in lowercase and form the
fundamental syntax elements of the language.
• Slicing: Python supports slicing, a technique to extract specific elements from sequences like lists, strings,
or tuples using a concise syntax (e.g., list[start:stop:step]).
• Interpreted Language: Python is an interpreted language, executing instructions directly without a
separate compilation step, allowing for easier debugging and rapid development.
Subtopic
14.2 Programming
1 basics

Declaration of variable
The majority of programming languages necessitate specifying the data type for variables to enable the compiler
to reserve the appropriate memory space.

This is how we declare a variable in pseudocode:

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Python
Python utilises dynamic typing, allowing variables to be created without explicit declaration of their data
type, as it infers types dynamically at runtime.

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Constants and variables


A named data store that contains a value that MAY CHANGE (vary) during the execution
of a program.
Eg. High score of a game
Variables

A named data store than contains a value that DOES NOT CHANGE during the execution
of a program.
Constant
Eg. Number of days in a week

Declaration of Assignment of constants and Arithmetic Operators


Output Input
Jamesvariables
Gan variables
Subtopic
14.2 Programming
1 basics

Assignment of constants
The customary practice in Python is to employ UPPERCASE letters exclusively for constant identifiers.
Although the values can be altered, it's recommended to regard constants as unchangeable.

Pseudocode Python

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Assignments of variables
The customary practice in Python is to employ uppercase letters exclusively for constant identifiers. Although the
values can be altered, it's recommended to regard constants as unchangeable.

Pseudocode Python

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Assignments of variables
Python allows you to increment a variable in the following ways:

Python

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Action Pseudocode Python

Add + +

Arithmetic operators Subtract - -


Arithmetic operators in Python are Multiply * *
necessary for performing
Divide / /
mathematical computations and
Raise to the power of
manipulating numerical values ^ **
within the code. Remainder division
MOD %
Integer
DIV //
division

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Output
Data can also be output to the console screen. Data can be a mixture of output strings and variable values.

Pseudocode

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Output
Data can also be output to the console screen. Data can be a mixture of output strings and variable values.

Python

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Output
Placeholder method for output:

Python

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Input
Input statements in programming are necessary to enable interaction with users by receiving external data or
information during the execution of a program.

Pseudocode Python

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables variables
Subtopic
14.2 Programming
1 basics

Comments
Adding comments is a good practice
in programming as it enhances code
readability and comprehension for
both oneself and others who might
review or work with the code.

Declaration of Assignment of constants and Arithmetic Operators


Output Input
variables
James Gan variables
14.3 Data types

Integer Real/Float Char String Boolean


A positive or negative A positive or negative A variable or A variable or constant A variable or
whole number that can number with a fractional constant that is a that is several constant that can
be used with part. Real numbers can single character characters in length. have only two
mathematical be used with Strings values TRUE or
operators mathematical operators vary in length and may FALSE.
even have no
characters (known as
an empty string)
14.3 Data types

Integer Real/Float Char String Boolean

"ccc" TRUE
25 25.0 'c'
"" FALSE
14.4 Boolean
expressions

Definition
A boolean expression is a
logical statement that evaluates
to either true or false, typically
involving logical operators or
conditions, used for
decision-making in
programming. True and False
are known as boolean values.
14.4 Boolean
expressions
Comparison Operator

Operator Pseudocode Python

More than > >

Less than < <

Equal = ==
Greater than or
>= >=
equal to
Less than or
<= <=
equal to

Not Equal <> !=


14.4 Boolean
expressions
Boolean Operator

Operator Description Pseudocode Python

AND Both True AND and

OR Either True OR or

NOT Not True NOT not


14.5 Selection

Selection
Selection in programming is necessary to enable decision-making based on specific conditions, directing the
flow of execution to different code paths.

Code Path Code Path


1 2

condition
14.5 Selection

If ... THEN statement

Pseudocode Python
14.5 Selection

IF ... THEN ... ELSE statements

Pseudocode Python
14.5 Selection

Nested IF statements

Pseudocode Python
14.5 Selection

CASE statement

Python
Pseudocode

James Gan
Subtopic
14.6 Iteration
1

Count-controlled loop

Syntax
Subtopic
14.6 Iteration
1
Pseudocode Python Output

12345
Subtopic
14.6 Iteration
1
Pseudocode Python Output

12345

3 6 9 12 15 18
Subtopic
14.6 Iteration
1
Pseudocode Python Output

12345

3 6 9 12 15 18

Countdown timer: 10
Countdown timer: 9
Countdown timer: 8
Countdown timer: 7
Countdown timer: 6
Countdown timer: 5
Countdown timer: 4
Countdown timer: 3
Countdown timer: 2
Countdown timer: 1
Countdown timer: 0
Subtopic
14.6 Iteration
1

Pre-condition loop
A pre-condition loop is a control flow structure where the condition is checked before entering the loop's
body, allowing the body to execute only if the condition is initially satisfied.
Subtopic
14.6 Iteration
1
Pseudocode Python
Subtopic
14.6 Iteration
1

Post-condition loop
A post-condition loop is a control flow structure where the condition is checked after executing the loop's body,
ensuring the body is executed at least once.

Not available in Python.


Subtopic
14.6 Iteration
1

Which loop to use?


Use a count-controlled loop when the number of iterations is known beforehand, a post-condition loop
when the loop must execute at least once with condition evaluation afterward, and a pre-condition loop when
the condition needs to be checked before entering the loop body.
14.7 Built-in functions

String manipulation functions

0 1 2 3 4 5 6 7
Sample String: A L E V E L C S

Access a single character using


its position 3 in a string

Output: V
14.7 Built-in functions

String manipulation functions - chr( )

Return the character whose


ascii value is i

Output: “a”

James Gan
14.7 Built-in functions

String manipulation functions -ord ( )

Return the ASCII Value of


the character

Output: 65
14.7 Built-in functions

String manipulation functions -len ( )

0 1 2 3 4 5 6 7
Sample String: A L E V E L C S

Returns the integer


value representing the
length of string S.

Output: 8
14.7 Built-in functions

String manipulation functions -lower( )

0 1 2 3 4 5 6 7
Sample String: A L E V E L C S

Output: “alevelcs”
14.7 Built-in functions

String manipulation functions -concatenation

Sample String: a l e v + e l c s

Output: “ALEVELCS”
14.7 Built-in functions

String manipulation functions -leftmost L characters


0 1 2 3 4 5 6 7
Sample String: A L E V E L C S

Returns the leftmost 3


characters.

0 1 2 3 4 5 6 7
A L E V E L C S

Output: “ALE”
14.7 Built-in functions

String manipulation functions -rightmost L characters


-8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7
Sample String: A L E V E L C S

Returns the rightmost 3


characters.

0 1 2 3 4 5 6 7
A L E V E L C S

Output: “LCS”
14.7 Built-in functions

Slicing in Python
A subsequence of any sequence type (list / string) can be created using slicing.

-8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7
Sample String: A L E V E L C S
14.7 Built-in functions

Truncating numbers
In Python, truncating an integer refers to the process of removing the decimal part (if any) and retaining
only the integer portion of a floating-point number.
14.7 Built-in functions

Converting a string to a number


14.7 Built-in functions

Random number generator


The random function in Python is useful for generating pseudo-random numbers, enabling various applications
such as simulations, games, statistical sampling, and cryptographic operations.

James Gan
14.8 Procedures

Pseudocode vs Python

Pseudocode Python
14.8 Procedures

Pseudocode vs Python

Pseudocode Python
14.9 Functions

Pseudocode vs Python
Recap: Functions and procedures are quite similar, except that function will return a value.

Pseudocode Python
14.9 Functions

Pseudocode vs Python
Recap: Functions and procedures are quite similar, except that function will return a value.

Pseudocode Python
Subtopic
14.10 1
Passing parameters to
subroutines

Why are parameters needed and how to pass them?


Parameters are needed in procedures and functions to provide input data or information necessary for the
execution of the code block.

Arguments are passed to a function or procedure in programming by specifying them within parentheses after
the function or procedure name during its invocation or call.
14.11 Passing parameters to
functions

Python
Pseudocode
14.12 Passing parameters to
procedures

Two ways to pass parameters


Passing parameters by value involves creating a copy of the actual value, while passing parameters by
reference involves passing a reference (memory address) to the original value, allowing modifications to
affect the original data.

Value Reference

Any changes that are applied to the


The value of the variable will not be
variable’s contents will be effective
affected by what happens in the
outside the procedure in the calling
subroutine.
program / module.
14.12 Passing parameters to
procedures

Passing parameters by value

Python
Pseudocode
14.12 Passing parameters to
procedures

Passing parameters by reference


Python does not have a facility to pass parameters by reference. Instead the subroutines behave like a
function and returns multiple values. Eg:

Python
Pseudocode
14.3 Arrays (List in Python)

Pseudocode to declare an array

James Gan
14.3 Arrays (List in Python)

Python code to create a list, appending to a list


A dynamic list in Python refers to a mutable data structure that can grow or
shrink dynamically in size during program execution to accommodate
varying numbers of elements.

Python code ...

Ali ...

Ali Bush ...

Ali Bush Catherine


14.3 Arrays (List in Python)

Accessing an item in the list


0 1 2

Ali Bush Catherine

Python code
14.3 Arrays (List in Python)

Pseudocode to declare a 2D array

Number of rows

Number of columns
14.3 Arrays (List in Python)

Pseudocode to declare a 2D array

Visualisation
Codes
[0] [1] [2] [3]
[0] 1 2 3 4
1

[1] 5 6 7 8

[2] 9 10 11 12
14.3 Arrays (List in Python)

Pseudocode to declare a 2D array

Codes Visualisation

[0] [1] [2] [3]


[0] 1 2 3 4
1

[1] 5 6 7 8

[2] 9 10 11 12
Subtopic
14.14 Text1files

Writing to a text file

Python
Pseudocode
Subtopic
14.14 Text1files

Reading from a text file


Python

Pseudocode
Subtopic
14.14 Text1files

Appending to a text file

Python
Pseudocode
Subtopic
14.14 Text1files

Appending to a text file

Pseudocode Python

You might also like