Object Oriented
Programming II
Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects.
Conceptually, objects are like the components of a system.
An object could represent a person with properties like a name, age, and address and behaviors such as walking, talking, breathing, and running.
OOP Concept
Python
Python is a general-purpose interpreted, interactive, language. It was created by Guido van Rossum during 1985- object-oriented, and high-level programming 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL).
Python is designed to be highly readable. It uses English keywords
frequently where as other languages use punctuation, and it has fewer
syntactical constructions than other languages.
Users vs. Programmers
• Users see computers as a set of tools - word processor, spreadsheet, map, to-do list, etc.
• Programmers learn the computer “ways” and the computer language
• Programmers have some tools that allow them to build new tools
• Programmers sometimes write tools for lots of users and sometimes
programmers write little “helpers” for themselves to automate a task
Python as a Language
Early Learner: Syntax Errors
• We need to learn the Python language so we can communicate our
instructions to Python. In the beginning we will make lots of mistakes and speak gibberish like small children.
• When you make a mistake, the computer does not think you are “cute”. It says “syntax error” - given that it knows the language and you are just
learning it. It seems like Python is cruel and unfeeling.
• You must remember that you are intelligent and can learn. The computer is
simple and very fast, but cannot learn. So it is easier for you to learn Python
than for the computer to learn English...
Talking to Python
Elements of Python
• Vocabulary / Words - Variables and Reserved words (Chapter 2)
• Sentence structure - valid syntax patterns (Chapters 3-5)
• Story structure - constructing a program for a purpose
Reserved Words
You cannot use reserved words as variable names / identifiers
False class return is finally None if for lambda continue
True def from while nonlocal and del global not with
as elif try or yield assert else import pass
break except in raise
Sentences or Lines
x = 2
x = x + 2 print(x)
Variable Operator Constant Function
Assignment statement
Assignment with expression
Print statement
Programming Paragraphs
Python Scripts
• Interactive Python is good for experiments and programs of 3-4 lines long.
• Most programs are much longer, so we type them into a file and tell Python to run the commands in the file.
• In a sense, we are “giving Python a script”.
• As a convention, we add “.py” as the suffix on the end of these files
to indicate they contain Python.
Sequential Steps
Program:
x = 2
print(x) x = x + 2 print(x)
Output:
2 4 x = 2
print(x) x = x + 2
print(x)
When a program is running, it flows from one step to the next. As
programmers, we set up “paths” for the program to follow.
Conditional Steps
Output:
Smaller Finis Program:
x = 5
if x < 10:
print('Smaller') if x > 20:
print('Bigger') print('Finis')
x = 5
x < 10 ?
print('Smaller')
x > 20 ?
print('Bigger')
print('Finis')
Yes
No
No
Yes
Repeated Steps
Output:
5 4 3 2 1
Blastoff!
Program:
n = 5
while n > 0 : print(n) n = n – 1
print('Blastoff!')
n > 0 ?
Loops (repeated steps) have iteration variables that change each time through a loop.
No
print('Blastoff')
Yes n = 5
print(n)
n = n -1
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
Why Python?
•Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, (etc.)
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
•Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
• Python can be treated in a procedural way, an object-orientated way or a
functional way.
Python Syntax
>>> print("Hello, World!")
Hello, World!
Python Variables
Variables are containers for storing data values.
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it .
Example
x = 50
y = “Bangladesh"
print(x) print(y)
x = 40 # x is of type int
x = ”shifat” # x is now of type str print(x)
Print(type(x))
Built-in Data Types
Text Type: str Numeric
Types: int, float, complex Sequence
Types: list, tuple, range Mapping
Type: dict
Set Types: set, frozenset Boolean
Type: bool
Binary
Types: bytes, bytearray, memoryview
Arithmetic operators
OPERATOR DESCRIPTION SYNTAX
+ Addition: adds two operands x + y
- Subtraction: subtracts two operands x - y
* Multiplication: multiplies two
operands x * y
/ Division (float): divides the first
operand by the second x / y
// Division (floor): divides the first
operand by the second x // y
% Modulus: returns the remainder when
first operand is divided by the second x % y
** Power : Returns first raised to power
second x ** y
Relational Operators
OPERATOR DESCRIPTION SYNTAX
> Greater than: True if left operand is greater than the
right x > y
< Less than: True if left operand
is less than the right x < y
== Equal to: True if both
operands are equal x == y
!= Not equal to - True if
operands are not equal x != y
>= Greater than or equal to: True if left operand is greater than
or equal to the right x >= y
<= Less than or equal to: True if left operand is less than or
equal to the right x <= y
Assignment operators
OPERATOR DESCRIPTION SYNTAX
=
Assign value of right side of expression to left sideoperand x = y + z
+=
Add AND: Add right side operand with left sideoperand and then assign to left operand a+=b a=a+b
-=
Subtract AND: Subtract right operand from leftoperand and then assign to left operand a-=b a=a-b
*=
Multiply AND: Multiply right operand with leftoperand and then assign to left operand a*=b a=a*b
/=
Divide AND: Divide left operand with right operandand then assign to left operand a/=b a=a/b
%=
Modulus AND: Takes modulus using left and rightoperands and assign result to left operand a%=b a=a%b
//=
Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to leftoperand a//=b a=a//b
**=
Exponent AND: Calculate exponent(raise power)value using operands and assign value to left operand a**=b a=a**b