You are on page 1of 20

ශ්‍රී ලංකා තාක්ෂණික විශ්වවිද්‍යාලය - පර්යේෂණ

SLTC Research University


Bachelor of Applied Information Technology
Degree Programme – (DAIT)
Module: CIT101 DAIT

Fundamentals of Programming

3
DAIT

Representing the Real World in the Computer

Lesson 4 - DK 4
Lesson 4 – Representingthe Real World
in the Computer

Content  The Real World

 Entities & Attributes

 Real World Objects

 Representing Real World Objects in a Computer

 Software Objects

 Elements of a Python Program

 Identifiers in Programming

 Naming Conventions in Python

 Rules of using Variable Names in Python

5
Learning Outcomes

Students should be able to:

 Summarize real-world things, their behaviours and their


interactions.

 Relate entities & attributes.

 Generalize real-world things as objects.

 Relate real-world objects to Software Objects.

 Identify the basic elements of a Python program.

 Assign legal and meaningful names to Program Elements


6
The Real World …

 The real-world is the


world that we are living.

 It contains living things (Basically Humans, Animals &


Plants) and non-living things.
 Non-living things are mainly physical objects.
 Living things are also physical (Tangible)
 Non-physical things are virtual (Intangible).
 It is very complex.
7
Entities and Attributes

Entity: Entity is the general term we use to describe


anything that exists in the real world. Entities are
distinct & independent.
Therefore everything in the real world is considered to
be an entity.

Attribute: An attribute is a quality or characteristic of an


entity. Simply it is data that describes something.

Object: In Python Programming, we call any entity an object.


Object Oriented Technology considers everything in
the real-world as an object.

8
Real World Objects

9
Objects
Data that
An object has: describes the
object.

E.g. Object: 3-Wheeler


1. Attributes Number of wheels, Colour, Engine, etc.
2. Behaviours Can be driven, reversed, stopped etc.
3. A unique identity Vehicle registration number
4. A reference No.10 in the parking lot
5. A type Vehicle

10
Representing Real World Objects
in a Computer
Real World Objects are represented as
Software Objects in the computer's
memory.

Animal : Rabbit
Color: White
Legs: 4

Software Objects will have the same


attributes and behaviours as that of
Real World Objects. They are models
of the Real World Objects. 11
Software Objects
Variables
Attributes refer to data (also called states of objects). Most of
the time they vary. Therefore we need one type of memory
location to store varying references of attributes of objects.
We simply call them variables.
A variable is a memory location having varying references.
Example:
 Consider the attribute: colour of a 3-wheeler.
 Colour changes from 3-wheeler to 3-wheeler (red, green, blue etc.)

 Therefore if we name a memory location as colour, we can use it


to store different references of colors. Therefore we call this memory
location a variable.
 This memory location does not store a color directly, but it stores 12
the
reference (address in the memory) of a color in question.
Constants
Memory locations where the stored references are unchanged are
referred to as constant locations and they are simply called constants.
Python does not promote the use of constants unlike other languages.
But you may use it at module levels.

Basic Building Blocks (Elements) of a Python Program


A python program consists of the following elements

1. Package
2. Module "Code is
3. Variable read much
more often
4. Function/Method than it is
5. Class written."
- Guido van Rossum
6. Constant
13
Identifiers in Programming
Identifier => a name given to any element in a program.

Harry

Naming Convention in Python


Object Type Naming Convention Example
Package All lowercase mypackage
Module Lowercase (with _) arithmetic.py
classic_music.py
Variable Lowercase (with _) total
number_list
Function/Method Lowercase (with _) area
add_num
class CapWords Fruit
BigAnimals
Constant All capitals (with _) PI
14
MAX_VALUE
Rules of using Variable Names in Python

Rules are specifications to be strictly followed.


Conventions are best practices proposed by experienced programmers.

General: Avoid names that are too general or wordy. Maintain a


good balance between the two.
Rule 1: Identifier names can have only digits (0..9), alphabetic letters
(A..Z)/(a..z) or underscore character (_)
Rule 2: The identifier name cannot start with a digit.
Rule 3: Identifier name cannot be a 'keyword' (Words reserved by
Python)
Python Keywords
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 15
'while', 'with', 'yield']
Examples of Identifier Usage

Identifier Correct/Incorrect Comments


class Incorrect class is a keyword in Python
3_apples Incorrect Cannot start with a digit
price_in_$ Incorrect $ is a special character
emp.salary Incorrect . is a special character
skin_colour Correct _ is allowed
net salary Incorrect Spaces are not allowed
MAX_TEMP Correct (A..Z) & _ characters are allowed

16
QUIZ
Q1. Which one of the following is a keyword in Python?

a) fail
b) pass
c) True
d) int

Q2. Which of the following is used to obtain the reference of an


object?

a) loc()
b) obj()
c) id()
d) ref()

17
QUIZ
Q3. What is the maximum length recommended for an identifier in
Python?

​a) 79
​ b) 31
​ c) 8
​ d) Depends on the Operating System

Q4. Which of the following represents a real world entity in Python?

a) an object
b) a function
c) an identifier
d) a class

18
QUIZ
Q5. Which of the​ following is not an implementation of Python?

a) CPython
b) PyPy
c) Jython
d) .NET

Q6. ​To translate and run a source code, Python uses:



a) an interpreter
​ b) a compiler
​ c) both a compiler and an interpreter
d) a data dictionary

19
End of Lesson 4

20

You might also like