0% found this document useful (0 votes)
5 views4 pages

Basic Python

The document outlines programming basics including variable declaration, input/output operations, assignment, selection structures (IF statements), and iteration (loops) in pseudocode and Python. It also provides examples of algorithms and pseudocode for various tasks, such as counting odd/even numbers and summing values based on conditions. Additionally, it includes past paper questions for practice on pseudocode writing.

Uploaded by

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

Basic Python

The document outlines programming basics including variable declaration, input/output operations, assignment, selection structures (IF statements), and iteration (loops) in pseudocode and Python. It also provides examples of algorithms and pseudocode for various tasks, such as counting odd/even numbers and summing values based on conditions. Additionally, it includes past paper questions for practice on pseudocode writing.

Uploaded by

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

Unit 2 - Programming Basics

Task Pseudocode Python code


a variable is a storage location paired with an
associated symbolic name (an identifier), which
DECLARE <identifier> : <data type> contains some known or unknown quantity of
1. Declare a information referred to as a value. Python has no
Example: DECLARE Num: INTEGER
variable and a command for declaring a variable. A variable is created
constant the moment you first assign a value to [Link]
CONSTANT NoDays ←30 examnation purpose data type for the variable is
mentioned as a comment.
# MyName: STRING

x = input()
x = input('Enter your name:')
INPUT <identifier> x = int(input('Enter your age:'))
2. Input
Example: INPUT Num x = float(input('Enter your average:'))

default constructor function is ‘str’


<identifier> ← <value> or <expression>
Marks1 = int(input('Enter your name:'))
3. Assignment Example: Num ←30 Average = (Marks1 + Marks2) / 2
Num ← x + y
OUTPUT <string> print("Hello World")
Example: OUTPUT “Hello World”
a=5
4. Output OUTPUT <identifier(s)> print(a)
Example: OUTPUT Num print(“The value of a is”, a)
OUTPUT “Average is”, Num

IF <condition> THEN a = 33
b = 200
<statement(s)> if b > a:
ENDIF print("b is greater than a")
IF <condition> THEN a = 63
<statement(s)> b = 33
if b > a:
ELSE print("b is greater than a")
<statement(s)> else:
ENDIF print("a is greater than b ")
Nested IF:
a = 200
IF <condition> THEN b = 33
<statement(s)> if b > a:
ELSEIF <condition> THEN print("b is greater than a")
3. Selection <statement(s)> elif a == b:
ELSE print("a and b are equal")
else:
<statement(s)> print("a is greater than b")
ENDIF
CASE OF <identifier>
<value 1>: <statement>
Python does not support CASE
<value 2>: <Statement>
...
ENDCASE

CASE OF <identifier>
<value 1>: <statement>
<value 2>: <Statement>
1
...
OTHERWISE <statement>
ENDCASE
Count controlled:
FOR <identifier> ← <value1> TO for x in range(6):
<value2> print(x)
<statement(s)>
NEXT Index for x in range(2, 6):
print(x)
FOR <identifier> ← <value1> TO
<value2> STEP <value3> for x in range(2, 30, 3):
<statement(s)> print(x)
ENDFOR
i=1
while i < 6:
Pre-condition loop: print(i)
i += 1
i=1
WHILE <condition> while i < 6:
<statement(s)> print(i)
4. Iteration ENDWHILE if i == 3:
break
i += 1
Post-condition loop:

REPEAT Python does not support post-condition loop


<statement(s)>
UNTIL <condition>
Nested loops:
FOR <identifier> ← <value1> TO
<value2>
for g in range(1, 6):
FOR <identifier> ← <value1> TO
for k in range(1, 3):
<value2> print (g, "*",k,"=", g*k)
<statement(s)>
ENDFOR
ENDFOR
Equal : = Equal : ==
Not equal: <> Not equal: !=
Greater than: > Greater than: >
Less than: < Less than: <
5. Boolean
Greater than or equal to: >= Greater than or equal to: >=
expressions
Less than or equal to: <= Less than or equal to: <=
And: AND And: and
Or: OR Or: or
Not: NOT Not: not

2
Past paper questions
9618/22/O/N/22

5 Examine the following pseudocode

A programmer wants to re-write the pseudocode as four separate


IF...THEN...ENDIF statements, each containing a single CALL statement. This
involves writing a single, simplified logic expression as the condition in each
statement. Write the amended pseudocode. [4]

9618/22/O/N/21

2 (a) An algorithm will:

1. input an integer value


2. jump to step 6 if the value is zero
3. sum and count the positive values
4. sum and count the negative values
5. repeat from step 1
6. output the two sum values and the two count values.
Write the pseudocode to represent the algorithm given above. [5]

9618/22/O/N/23

2 An algorithm will:

1. input a sequence of integer values, one at a time


2. ignore all values until the value 27 is input, then sum the remaining values in the sequence
3. stop summing values when the value 0 is input and then output the sum of the values.
(a) Write a pseudocode to represent the algorithm.

4 A program will:

1. input a value (all values will be positive integers)


2. count the number of odd values and count the number of even values
3. repeat from step 1 until the value input is 99
4. output the two count values, with a suitable message.
The value 99 must not be counted.

(a) Write pseudocode for the procedure Count(). [6]

3
9618/23/M/J/21

2(c) A program will:

• input 50 unique integer values


• output the largest value
• output the average of the values excluding the largest value.
Write a pseudocode to represent the algorithm. It is not necessary to check that each input value is unique. [6]

9618/23/M/J/23

6 A program will:

1. take two integer values as parameters representing start and end values where both values

are greater than 9 and the end value is greater than the start value

2. output each integer value between the start and the end value (not including the start and

end values), where the sum of the last two digits is 6, for example, 142.

(a) Write pseudocode for the algorithm.

You might also like