0% found this document useful (0 votes)
17 views31 pages

T1 Programming Fundamentals

The document outlines programming fundamentals, focusing on data types, variables, constants, input/output statements, and arithmetic operators. It includes definitions and examples of various data types such as integer, real, Boolean, character, and string, as well as the use of arithmetic operators like MOD and DIV. Additionally, it covers string handling functions, type conversion, and the importance of comments in programming.

Uploaded by

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

T1 Programming Fundamentals

The document outlines programming fundamentals, focusing on data types, variables, constants, input/output statements, and arithmetic operators. It includes definitions and examples of various data types such as integer, real, Boolean, character, and string, as well as the use of arithmetic operators like MOD and DIV. Additionally, it covers string handling functions, type conversion, and the importance of comments in programming.

Uploaded by

hardevpawar2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Objectives

• Understand and use data types: integer, real,


Boolean, character and string
• Declare and use constants and variables
• Use input, output and assignment statements
• Use arithmetic operators including MOD and DIV
• Use string handling and conversion functions
Programming fundamentals
Unit 7 Programming

Starter
• What is a data type?
• How many can you name?

• An arithmetic operator is a symbol


that will perform an operation on numbers
• + is an example of an arithmetic operator
that uses two numbers, e.g. 5 + 2
• How many arithmetic operators can you name?
Programming fundamentals
Unit 7 Programming

Starter
• What is a data type?
The kind of values that can be used in a data item
• How many can you name?
integer, float / real, character, string, Boolean
• An arithmetic operator is a symbol that will perform
an operation on numbers
• + is an example of an arithmetic operator that uses
two numbers, e.g. 5 + 2
• How many arithmetic operators can you name?
+, -, *, /, ^, MOD, DIV
Programming fundamentals
Unit 7 Programming

OCR Reference Language


• Throughout these lessons, computer code will be
written using OCR Exam Reference Language
• This is the language that examination
questions will be written in
• Sometimes the syntax may be different
or not possible in the programming
language you are studying
• Sometimes examples will demonstrate
a concept in a real language –
in which case, the language
used will be stated
Programming fundamentals
Unit 7 Programming

Using variables in a program


• You have probably used variables in many different
ways in programs you have already written
numberOfStudents = numberOfStudents + 1
circleArea = 3.142 * radius^2
while NOT found…
answer = 'Y'
print(studentName)
• What are the data types of each of the
above variables?
Programming fundamentals
Unit 7 Programming

Data types
• Variables will typically be one of the following types:
• Integer, Boolean, real / float, character or string

• For the variables given, the data types would be:


• numberOfStudents Integer
• circleArea Real / Floating point number
• found Boolean
• answer Character
• studentName String
• The data type used will determine the amount of
memory that needs to be allocated for the variable
Programming fundamentals
Unit 7 Programming

Definitions of data types


Typical amount of
Data type Type of data
memory
Whole number such as 156, 0 -
integer 2 bytes
54
Number with a fractional part
float or real 4 bytes
such as 1.5276, -68.4, 20.0
A single ASCII character such
char 1 byte
as A, b, 3, ! or space
1 byte per character in the
string Zero or more characters
string
Theoretically just one bit,
Can only take the values True
Boolean but in high level languages
or False
often one byte
Programming fundamentals
Unit 7 Programming

Constants
• As well as variables, you can define constants
in a program
const PI = 3.14157926535
const VAT = 0.2
const MAX_PLAYERS = 6

• Constants are typically shown in uppercase


• Words are separated with an underscore, e.g. MIN_AGE
• This is known as snake case

• Why declare a constant instead of a variable?


• Can a constant ever change its value?
Programming fundamentals
Unit 7 Programming

Constants
• Why declare a constant instead of a variable?
• This prevents the value from being changed accidentally by a
part of code
• It shows a programmer that the value should stay the same
throughout the program
• Can a constant ever change its value?
• A constant cannot be changed when the program is running
• A constant can be changed by a programmer before the
program is compiled or translated
Programming fundamentals
Unit 7 Programming

Input and output statements


• Most programs accept data from the user, process it
in some way, and output a result
print("How many hours a night do you
sleep?")
hoursPerNight = input()
hoursPerWeek = hoursPerNight * 7
print(hoursPerWeek)
Programming fundamentals
Unit 7 Programming

INPUT statement
• An input statement can have a prompt for the user:
firstName = input("What is your name?
")
• This statement first displays the message “What is
your name?” and then waits for the user to enter
some text and press Enter
• The response is then assigned to the
variable firstName
Programming fundamentals
Unit 7 Programming

Arithmetic operators
• The operators +, -, * and / are used for addition,
subtraction, multiplication and division
• ^ is used for an exponent (power of)

• The operator DIV is used for integer division, also


known as quotient
• MOD (modulus) is used to find the remainder when
dividing one integer by another
• What is the result of the following operations?
• weeks = 31 DIV 7
• daysLeft = 31 MOD 7
Programming fundamentals
Unit 7 Programming

MOD and DIV


• weeks = 31 DIV 7
• The variable weeks is assigned the value 4
• This is because 7 goes into 31 four times (remainder 3)

• daysLeft = 31 MOD 7
• The variable daysLeft is assigned the value 3
• This is because the remainder after the division is 3
Programming fundamentals
Unit 7 Programming

Orders of precedence
• Remember BIDMAS
• Brackets
• Indices
• Division
• Multiplication
• Addition
• Subtraction
• Calculate: x = (5 – 2) + (16 – 6 / 2)
y = 7 * 3 + 10 / 2
Are brackets needed in the first expression?
Programming fundamentals
Unit 7 Programming

Worksheet 1
• Now complete Task 1 on Worksheet 1
Programming fundamentals
Unit 7 Programming

Strings and numbers


• Strings and numbers used in calculations are
represented differently in binary
• Remember that a string is anything held within quote
marks
• The string "17" is represented in binary as
• 0011000100110111
• The integer 17 is held as in binary as
• 00010001
Programming fundamentals
Unit 7 Programming

Conversions and casting


• Functions are used to convert data types
• This is also known as casting

• Examples of functions (written in pseudocode) :


• int(s) converts a string s to an integer
• float(s) converts a string s to a number
with a decimal point
• str(x) converts an integer or floating point number x to
a string
• bool(s) converts a string (e.g. "True") to a Boolean
• ASC('a') evaluates to 97, using ASCII
• CHR(97) evaluates to 'a'
Programming fundamentals
Unit 7 Programming

Inputting numeric variables


• Inputs from the user are strings
• Therefore if you are inputting an integer or real
number, you have to convert it before it can be used
in a calculation
tickets = input("Please enter number of tickets required:
")
tickets = int(tickets)

• Or alternatively, in a single statement:


tickets = int(input("Please enter number of tickets
required: "))
Programming fundamentals
Unit 7 Programming

Concatenating strings
• Concatenating means joining together
• The + concatenate operator is used to join together strings
firstname = "Rose"
surname = "Chan"
fullname = firstname + " " + surname
print(fullname)
• What will be output?
• What will be output by the program?
x = "6" + "3"
print(x)
Programming fundamentals
Unit 7 Programming

Concatenating strings
firstname = "Rose"
surname = "Chan"
fullname = firstname + " " + surname
print(fullname)
• What will be output? "Rose Chan"
• What will be output by the program
x = "6" + "3"
print(x)
"63"
Programming fundamentals
Unit 7 Programming

String handling functions


Function Example Result
word = "Algorithm"
str.length 9
print(word.length)
str.substring(start, print(word.substring(3,6)
"orit"
end) )
str.left(n) Print(word.left(3)) "Alg"
str.right(n) Print(word.right(4)) "ithm"

• What will be the values of a, b


and c below?
zooName = "London Zoo"
a = zooName.length
b = zooName.substring(1,4)
c = zooName.left(8)
d = zooName.right(5)
Programming fundamentals
Unit 7 Programming

String handling functions


Function Example Result
word = "Algorithm"
str.length 9
print(word.length)
str.substring(start, print(word.substring(3,6)
"orit"
end) )
str.left(n) Print(word.left(3)) "Alg"
str.right(n) Print(word.right(4)) "ithm"

• What will be the values of a, b, c and d below?


zooName = "London Zoo"
a = zooName.length 10
b = zooName.substring(1,4)
"ondo"
c = zooName.left(8)
"London Z"
d = zooName.right(5) "n Zoo"
Programming fundamentals
Unit 7 Programming

Character conversion functions


• Characters may be converted to their ASCII value
and vice versa using the functions:
ASC(character)
and
CHR(integer)
• For example:
x = ASC('a') // sets x = 97
y = CHR(97) // sets y to 'a'
• Evaluate num and letter where
num = ASC('c')
letter = CHR(102)
Programming fundamentals
Unit 7 Programming

Uppercase and lowercase


• A string can be converted to uppercase or lowercase
letters as follows:
phrase1 = "Good morning"
phrase2 = "HAPPY BIRTHDAY"
print(phrase1.upper) //"GOOD MORNING"
print(phrase2.lower) //"happy
birthday"
• What is the output of the following program?
a = "The quality of mercy is not strained."
b = a.length – 30
c = a.substring(0,b)
d = c.upper
print(d)
Programming fundamentals
Unit 7 Programming

Uppercase and lowercase


• What is the output of the following program?
a = "The quality of mercy is not strained."

b = a.length – 10 b = 37 - 30
b = 7
c = a.substring(0,b) c =
a.substring(0,7)
c = "The qual"
d = c.upper d = "THE QUAL"

print(d) "THE QUAL"


Programming fundamentals
Unit 7 Programming

Using comments
• You should use comments in your programs:
• to describe the purpose of the program
• to state the author of the program
• to explain what the code does

• Pseudocode comments start with a double slash //


• In Python, comments start with #
• In VB, comments start with '

• Comments are ignored when your program is


translated to machine code and executed
Programming fundamentals
Unit 7 Programming

Worksheet 1
• Now complete Task 2 and Task 3 on Worksheet 1
Programming fundamentals
Unit 7 Programming

Plenary
• In pairs give:
• 5 different data types
• 4 arithmetic operators
• 3 functions used for type conversion / casting
• 2 arithmetic operators used for integer division and remainder
• 1 concatenate operator
Programming fundamentals
Unit 7 Programming

Plenary
• 5 different data types
• Integer, real / float, character, string, Boolean

• 4 arithmetic operators
• +, -, *, /, ^

• 3 functions used for type conversion / casting


• str(), int(), float() / real(), bool()

• 2 arithmetic operators used for integer division and


remainder
• DIV (integer division), MOD (remainder)

• 1 concatenate operator +
Programming fundamentals
Unit 7 Programming

Copyright

© 2020 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.

Licence agreement

This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.

The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.

You might also like