Object Oriented Programming Using Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 72

OBJECT ORIENTED PROGRAMMING USING PYTHON

Unit-1
Introduction to Object-Oriented Programming:
 Computer Programming and Programming Languages
 Features of Object-Oriented Programming
 Merits and Demerits of OOPs .
 Applications of Object Oriented Programming,
 Differences Between Popular Programming Languages
Basics of Python Programming:
 Features, History, Future, Writing and Executing First Python Program,
 Literal Constants,
 Variables and Identifiers, Data Types, Input Operation, Comments, Reserved Words,
Indentation, Operators and Expressions,
 Expressions in Python, Operations on Strings, Other Data Types, Type Conversion

IT CBIT
Computer programming and programming languages

• Computer program is a sequence of instructions written in some computer language


to perform a particular task.
• The act of writing computer programs is called computer programming.
• Programming Languages: language is designed to implement an algorithm that can
be performed by a computer to solve a problem.
• Programming languages are also called high level languages such as
BASIC(Beginners All purpose Symbolic Instruction Code), C, C++,
COBOL(Common Business Oriented Language), FORTRAN(Formula Translator),
Python,Ada, and Pascal etc.
• Machine language: language which can understand only by computer consists of
only numbers 1’s and 0’s.
• Assembly language: it is a low level language for the micro processor and other
programmable devices. Symbolic names are used to represent the machine
language.
• High level language: every human being can read this language like english
statements. Ex: all programming languages.
IT CBIT
Generations of programming languages

Programming languages

High levelProgramming
Low level progrmming languages
languages

First Second Third Fouth Fifth


Generation(1G) Generation(2G) Generation(3G) Generation(4G) Generation(3G)

IT CBIT
First Generation: Machine Language

All the commands and data values are expressed using 0s and 1s, corresponding to the off and on electrical states in a
computer.

© Oxford University Press 2017. All rights 8reserved.


Second Generation: Assembly Language
It used symbolic codes, also known as mnemonic codes, which are easy-to-remember abbreviations, rather than numbers.
Examples of these codes include ADD for add, CMP for compare, and MUL for multiply.
An assembly language statement consists of a label, an operation code, and one or more operands.
Labels are used to identify and refer instructions in the program.

© Oxford University Press 2017. All rights 9reserved.


Third Generation: High-level Language
The third generation was introduced to make the languages more programmer-friendly.
A translator is needed to translate the instructions written in a high-level language into the computer-executable machine
language. Such translators are commonly known as interpreters and compilers.
The 3GLs make it easy to write and debug a program and give a programmer more time to think about its overall logic.
Ex: BASIC,C,COBAL,Fortran,Java, and pascal.

© Oxford University Press 2017. All rights10reserved.


Fourth Generation: Very High-level Languages
4GLs are a little different from their prior generation because they are non-procedural.
While using a non-procedural language, programmers define what they want the computer to do but they do not supply
all the details of how it has to be done.
Ex:Perl,PHP,Python,Ruby and SQL
Although there is no standard rule that defines a 4GL, certain characteristics include the following:
• The instructions of the code are written in English-like sentences.
• They are non-procedural, so users concentrate on the ‘what’ instead of the ‘how’ aspect of the task.
• The code written in a 4GL is easy to maintain.
• The code written in a 4GL enhances the productivity of programmers, as they have to type fewer lines of code to get
something done. A programmer supposedly becomes 10 times more productive when he/she writes the code using a 4GL
than using a 3GL.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


11
Fifth-generation Programming Language

Fifth-generation programming languages (5GLs) are centered on solving problems using the constraints given to a
program rather than using an algorithm written by a programmer.

These languages are widely used in artificial intelligence research.

Typical examples of 5GLs include Prolog(programming with logic), OPS5(official production system), Mercury,
Used for creation of reliable, fast, large programs.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


12
Monolithic Programming
Programs written using monolithic programming languages such as assembly
language and BASIC consist of global data and sequential code. The global data
can be accessed and modified (knowingly or mistakenly) from any part of the
program, thereby, posing a serious threat to its integrity.
Monolithic programs have just one program module as such programming
languages do not support the concept of subroutines. Therefore, all the actions
required to complete a particular task are embedded within the same application
itself. This not only makes the size of the program large but also makes it difficult
to debug and maintain.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


13
Procedural Programming
In procedural languages, a program is divided into n number of subroutines that access global
data. To avoid repetition of code, each subroutine performs a well-defined task. A subroutine
that needs the service provided by another subroutine can call that subroutine. Therefore, with
‘jump’, ‘Go To’, and ‘call’ instructions, the sequence of execution of instructions can be altered.
Advantages
• The only goal is to write correct programs.
• Programs were easier to write as compared to monolithic programming.
Disadvantages
• Writing programs is complex.
• No concept of reusability.
• Requires more time and effort to write programs.
• Programs are difficult to maintain.
• Global data is shared and therefore may get altered (mistakenly).
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
14
Structured Programming
Structured programming, also referred to as modular programming.
Advantages
Efficient, correct programs that are easy to understand, debug and change.
• Modules enhance programmer’s productivity.
Many programmers can work on a single, large program
• A structured program takes less time to be written than other programs.
• Each module performs a specific task.
• Each module has its own local data.
First to introduce the concept of functional abstraction.
Disadvantages
• Not data-centered.
• Global data is shared and therefore may get inadvertently modified.
• Main focus on functions. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
15
Object Oriented Programming (OOP)
In the object oriented paradigm, the list and the associated operations are treated as one entity known as an object.

The striking features of OOP include the following:


• The programs are data centered.
• Programs are divided in terms of objects and not procedures.
• Functions that operate on data are tied together with the data.
• Data is hidden and not accessible by external functions.
• New data and functions can be easily added as and when required.
• Follows a bottom-up approach for problem solving.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


16
4. Object oriented programming paradigm:
 Models real world objects
 Uses bottom up approach
 Problem is decomposed into objects and build the
data and functions around these objects.
 Program organized around the objects , grouped
into classes.
 Data of the objects can be accessed only with the
function associated with that objects.
 Objects can communicate with each other
 Empasize more on data.
Ex: c++,java, python, ruby and php.
Advantages:
- Provides security to data
- Reusability.
- Models the real world. Object oriented paradigm Structure

IT CBIT
Features of Oop
1. Class
2. Object
3. Data hiding
4. Data abstraction
5. Data encapsulation
6. Inheritance
7. Polymorphism
8. Containership or composition
9. Delegation
10. Method and message passing

IT CBIT
Classes, Objects, and Methods
A class is used to describe something in the world, such as occurrences, things, external
entities, and so on.
A class provides a template or a blueprint that describes the structure and behavior of a
set of similar objects.
Once we have the definition for a class, a specific instance of the class can be easily
created.
A class can have multiple instances or objects. Every object contains some data and
procedures. They are also called methods.
A method is a function associated with a class. It defines the operations that the object
can execute when it receives a message. In OOP language, only methods of the class can
access and manipulate the data stored in an instance of the class (or object).
Two objects can communicate with each other through messages. An object asks
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
19
another object to invoke one of its methods by sending it a message.
Inheritance
Inheritance is a concept of OOP in which a new class is created from an existing class. The new
class, often known as a sub-class, contains the attributes and methods of the parent class
The new class, known as sub-class or derived class, inherits the attributes and behavior of the
pre-existing class, which is referred to as super-class or parent class.
The inheritance relationship of sub- and super classes generates a hierarchy. Therefore,
inheritance relation is also called ‘is-a’ relation.
A sub-class not only has all the states and behaviors associated with the super-class but has
other specialized features (additional data or methods) as well.
The main advantage of inheritance is the ability to reuse the code. When we want a specialized
class, we do not have to write the entire code for that class from scratch. We can inherit a class
from a general class and add the specialized code for the sub-class.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


20
Polymorphism, Containership and Reusability
Polymorphism refers to having several different forms. It is related to methods. Polymorphism is a concept that enables the
programmers to assign a different meaning or usage to a method in different contexts. Polymorphism can also be applied to
operators. For example, we know that operators can be applied only on basic data types that the programming language
supports. Therefore, a + b will give the result of adding a and b. If a = 2 and b = 3, then a + b = 5. When we overload the +
operator to be used with strings, then Fraction1 + Fraction2 adds two fractional numbers and returns the result.
Containership is the ability of a class to contain object(s) of one or more classes as member data. For example, class One can
have an object of class Two as its data member. This would allow the object of class One to call the public functions of class Two.
Here, class One becomes the container, whereas class Two becomes the contained class. Containership is also called
composition.
Reusability means developing codes that can be reused either in the same program or in different programs. Python gives due
importance to building programs that are reusable. Reusability is attained through inheritance, containership, and
polymorphism.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
21
Data Abstraction and Encapsulation

Data abstraction refers to the process by which data and functions are defined in such a way that only essential details are
revealed and the implementation details are hidden. The main focus of data abstraction is to separate the interface and
the implementation of a program.

Data encapsulation, also called data hiding, is the technique of packing data and functions into a single component (class)
to hide implementation details of a class from the users. Users are allowed to execute only a restricted set of operations
(class methods) on the data members of the class. Therefore, encapsulation organizes the data and methods into a
structure that prevents data access by any function (or method) that is not specified in the class. This ensures the integrity
of the data contained in the object.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


22
Merits of OOP Languages
• Elimination of redundant code through inheritance (by extending existing classes).
• Higher productivity and reduced development time due to reusability of the existing modules.
• Secure programs as data cannot be modified or accessed by any code outside the class.
• Real world objects in the problem domain can be easily mapped objects in the program.
• A program can be easily divided into parts based on objects.
• The data-centered design approach captures more details of a model in a form that can be easily implemented.
• Programs designed using OOP are expandable as they can be easily upgraded from small to large systems.
• Message passing between objects simplifies the interface descriptions with external systems.
• Software complexity becomes easily manageable.
• With polymorphism, behavior of functions, operators, or objects may vary depending upon the circumstances.
• Data abstraction and encapsulation hides implementation details from the external world.
• OOP enables programmers to write easily extendable and maintainable programs.
• OOP supports code reusability to a great extent. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
23
Demerits of OOP Languages

Programs written using object oriented languages have greater processing overhead as they demand more resources.
• Requires more skills to learn and implement the concepts.
• Beneficial only for large and complicated programs.
• Even an easy to use software when developed using OOP is hard to be build.
• OOP cannot work with existing systems.
• Programmers must have a good command in software engineering and programming methodology.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


24
Applications of OOP Languages
• Designing user interfaces such as work screens, menus, windows, and so on.
• Real-time systems • Simulation and modelling
• Compiler design • Client server system
• Object oriented databases • Object oriented distributed database
• Parallel programming • Decision control systems
• Office automation systems • Hypertext and hypermedia
• Computer-aided design (CAD) systems • Computer-aided manufacturing (CAM) systems
• Computer animation • Developing computer games
• Artificial intelligence—expert systems and neural networks
• Networks for programming routers, firewalls, and other devices

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


25
Applications of Python
1. Desktop applications ( calculators, Excel etc) standalone applications
2. WebApplications (Django in python to develop web applications)
3. Database applications.
4. Machine learning
5. Networking applications
6. Games
7. Data analysis
8. Artificial Intelligence
9. IOT applications
10. Mobile based applications.

Currently following companies using python


- Google, Youtube, Dropbox, NASA, stock exchange, national security.

IT CBIT
1

Python Programming
Using Problem Solving Approach

Reema Thareja

© Oxford University Press 2017. All rights27reserved.


CHAPTER 3
Basics of Python Programming

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


28
Features of Python
• Simple • Embeddable
• Easy to Learn • Extensive
• Versatile • Easy maintenance
• Free and Open Source • Secure
• High-level Language • Robust
• Interactive • Multi-threaded
• Portable • Garbage Collection
• Object Oriented
• Interpreted
• Dynamic
• Extensible
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
29
Limitations of Python
Parallel processing can be done in Python but not as elegantly as done in some other languages (like JavaScript and Go
Lang).
• Being an interpreted language, Python is slow as compared to C/C++. Python is not a very good choice for those
developing a high-graphic 3d game that takes up a lot of CPU.
• As compared to other languages, Python is evolving continuously and there is little substantial documentation available
for the language.
• As of now, there are few users of Python as compared to those using C, C++ or Java.
• It lacks true multiprocessor support.
• It has very limited commercial support point.
• Python is slower than C or C++ when it comes to computation heavy tasks and desktop applications.
• It is difficult to pack up a big Python application into a single executable file. This makes it difficult to distribute Python
to non-technical. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
30
Applications of Python
• Embedded scripting language: Python is used as an embedded scripting language for various testing/ building/
deployment/ monitoring frameworks, scientific apps, and quick scripts.
• 3D Software: 3D software like Maya uses Python for automating small user tasks, or for doing more complex
integration such as talking to databases and asset management systems.
• Web development: Python is an easily extensible language that provides good integration with database and other
web standards.
GUI-based desktop applications: Simple syntax, modular architecture, rich text processing tools and the ability to work
on multiple operating systems makes Python a preferred choice for developing desktop-based applications.
• Image processing and graphic design applications: Python is used to make 2D imaging software such as Inkscape,
GIMP, Paint Shop Pro and Scribus. It is also used to make 3D animation packages, like Blender, 3ds Max, Cinema 4D,
Houdini, Lightwave and Maya.
3
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
1
Applications of Python
• Scientific and computational applications: Features like high speed, productivity and availability of tools, such as
Scientific Python and Numeric Python, have made Python a preferred language to perform computation and processing
of scientific data. 3D modeling software, such as FreeCAD, and finite element method software, like Abaqus, are coded
in Python.
Games: Python has various modules, libraries, and platforms that support development of games. Games like
Civilization-IV, Disney's Toontown Online, Vega Strike, etc. are coded using Python.
• Enterprise and business applications: Simple and reliable syntax, modules and libraries, extensibility, scalability
together make Python a suitable coding language for customizing larger applications. For example, Reddit which was
originally written in Common Lips, was rewritten in Python in 2005. A large part of Youtube code is also written in
Python.
• Operating Systems: Python forms an integral part of Linux distributions.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
32
Writing and Executing First Python Program
Step 1: Open an editor.
Step 2: Write the instructions
Step 3: Save it as a file with the filename having the extension .py.
Step 4: Run the interpreter with the command python program_name.py or use IDLE to run the programs.
To execute the program at the command prompt, simply change your working directory to C:\Python34 (or move to
the directory where you have saved Python) then type python program_name.py.
If you want to execute the program in Python shell, then just press F5 key or click on Run Menu and then select Run
Module.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


33
Python
3

 Python is an interpreted high-level


programming language for general-purpose
programming.
 Created by Guido van Rossum and first released in
1991
 Latest Python version available for windows is 3.7.0
as on date

U Sairam, Assistant Professor, Dept. of IT, CBIT 34


Python
 Written in Python
 Libraries are written mostly in Python itself, with some
performance critical sections written in C.
 There are other implementations:
 CPython and is written in C.
 IronPython (Python running on .NET)
 Jython (Python running on the Java Virtual Machine)
 PyPy (A fast python implementation with a JIT compiler)
 Stackless Python (Branch of CPython supporting
microthreads)

U Sairam, Assistant Professor, Dept. of IT, CBIT 35


Using Python
5

 Download and Install Python


 Python Interpreter
 Interactive Mode

1) PyCharm 2) Spyder 3) Jupyter

U Sairam, Assistant Professor, Dept. of IT, CBIT 36


Installation
Instructions
6

 www.python.org
 Choose your OS
 Download
 Install
 Once installation is over you will find
 Python IDLE

 Python command prompt

U Sairam, Assistant Professor, Dept. of IT, CBIT 37


The Python Interpreter 7

 When you install the Python language on your computer, one of the
items that is installed is the Python interpreter.
 The Python interpreter is a program that can read Python programming
statements and execute them.
 You can use the interpreter in two modes:
 interactive mode and
 script mode.

 In interactive mode, the interpreter waits for you to type Python


statements on the keyboard.
 Once you type a statement, the interpreter executes it and then waits for
you to type another statement.

U Sairam, Assistant Professor, Dept. of IT, CBIT 38


The Python Interpreter
 In interactive mode, the interpreter waits for you to type Python
statements on the keyboard.
 Once you type a statement, the interpreter executes it and then waits for
you to type another statement.
 In script mode, the interpreter reads the contents of a file that contains
Python statements.
 Such a file is known as a Python program or a Python script.
 The interpreter executes each statement in the Python program as it
reads it.

U Sairam, Assistant Professor, Dept. of IT, CBIT 39


Interactive
Mode 9

 When the Python interpreter is running in interactive


mode, it is commonly called the Python shell.
 When the Python interpreter starts in interactive mode,
you will see something like the following displayed in a
console window:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013,
00:06:53)
 [MSC v.1600 64 bit (AMD64)] on win32
 Type "help", "copyright", "credits" or "license“ for more
information.
>>>
 The >>> that you see is a prompt that indicates the
interpreter is waiting for you to type a Python statement.

U Sairam, Assistant Professor, Dept. of IT, CBIT 40


Examples
10

>>> print('To be or not to be')


To be or not to be

>>> print('That is the question.')


That is the question.

>>>

U Sairam, Assistant Professor, Dept. of IT, CBIT 41


Input, Processing, and
Output 11

 Computer programs typically perform the following


three-step process:
 Input is received.
 Some process is performed on the input.
 Output is produced.

U Sairam, Assistant Professor, Dept. of IT, CBIT 42


Displaying Output with the print Function
12

 A function is a piece of prewritten code that performs


an operation.
 Python has numerous built-in functions that perform
various operations.
 When programmers execute a function, they say that
they are calling the function
 Example – print() is a function
>>> print('Hello world')
Hello world
>>>

U Sairam, Assistant Professor, Dept. of IT, CBIT 43


print() function
contd… 13

 When you call the print function, you type the word
print, followed by a set of parentheses.
 Inside the parentheses, you type an argument, which is
the data that you want displayed on the screen.
print('Kate Austen')
print('123 Full Circle Drive')
print('Asheville, NC 28899')
Program Output
Kate Austen
123 Full Circle Drive
Asheville, NC 28899

U Sairam, Assistant Professor, Dept. of IT, CBIT 44


Literal Constants
The value of a literal constant can be used directly in programs. For example, 7, 3.9, 'A', and "Hello" are literal constants.
Numbers refers to a numeric value. You can use four types of numbers in Python program- integers, long integers, floating
point and complex numbers.
• Numbers like 5 or other whole numbers are referred to as integers. Bigger whole numbers are called long integers. For
example, 535633629843L is a long integer.
• Numbers like are 3.23 and 91.5E-2 are termed as floating point numbers.
• Numbers of a + bi form (like -3 + 7i) are complex numbers.

Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


45
Literal Constants
Strings
A string is a group of characters.
• Using Single Quotes ('): For example, a string can be written as 'HELLO'.
• Using Double Quotes ("): Strings in double quotes are exactly same as those in single quotes. Therefore, 'HELLO' is same
as "HELLO".
• Using Triple Quotes (''' '''): You can specify multi-line strings using triple quotes. You can use as many single quotes and
double quotes as you want in a string within triple quotes.

Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


46
Escape Sequences
Some characters (like ", \) cannot be directly included in a string. Such characters must be escaped by placing a backslash
before them.

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


47
Raw Strings
If you want to specify a string that should not handle any escape sequences and want to display exactly as specified then
you need to specify that string as a raw string. A raw string is specified by prefixing r or R to the string.

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


48
Variables and Identifiers
Variable means its value can vary. You can store any piece of information in a variable. Variables are nothing but just
parts of your computer’s memory where information is stored. To be identified easily, each variable is given an
appropriate name.
Identifiers are names given to identify something. This something can be a variable, function, class, module or other
object. For naming any identifier, there are some basic rules like:
• The first character of an identifier must be an underscore ('_') or a letter (upper or lowercase).
• The rest of the identifier name can be underscores ('_'), letters (upper or lowercase), or digits (0-9).
• Identifier names are case-sensitive. For example, myvar and myVar are not the same.
• Punctuation characters such as @, $, and % are not allowed within identifiers.
Examples of valid identifier names are sum, __my_var, num1, r, var_20, First, etc.
Examples of invalid identifier names are 1num, my-var, %check, Basic Sal, H#R&A, etc.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
49
Assigning or Initializing Values to Variables
In Python, programmers need not explicitly declare variables to reserve memory space. The declaration is done
automatically when a value is assigned to the variable using the equal sign (=). The operand on the left side of equal sign
is the name of the variable and the operand on its right side is the value to be stored in that variable.

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


50
Data Type Boolean
Boolean is another data type in Python. A variable of Boolean type can have one of the two values- True or False. Similar
to other variables, the Boolean variables are also created while we assign a value to them or when we use a relational
operator on them.

Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


51
Input Operation
To take input from the users, Python makes use of the input() function. The input() function prompts the user to
provide some information on which the program can work and give the result. However, we must always remember
that the input function takes user’s input as a string.

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


52
Comments
Comments are the non-executable statements in a program. They are just added to describe the statements in the
program code. Comments make the program easily readable and understandable by the programmer as well as other
users who are seeing the code. The interpreter simply ignores the comments.
In Python, a hash sign (#) that is not inside a string literal begins a comment. All characters following the # and up to
the end of the line are part of the comment

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


53
reserved words or key words

 Words which are defined by python has some specific meaning and
functionality are called reserved words or key words.
 Python provides total 33 keywords
 To find total keywords present in python run the following statements
import keyword
keyword.kwlist

IT CBIT
Indentation
Whitespace at the beginning of the line is called indentation. These whitespaces or the indentation are very important
in Python. In a Python program, the leading whitespace including spaces and tabs at the beginning of the logical line
determines the indentation level of that logical line.

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


55
Operators and expressions

Operator is symbol that performs certain


operations Types of operators:
1. Arithmetic operators
2. Relational or comparison operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Special operators

IT CBIT
Arithmetic Operators

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


57
Comparison Operators

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


58
Unary Operators
Unary operators act on single operands. Python supports unary minus operator. Unary minus operator is strikingly
different from the arithmetic operator that operates on two operands and subtracts the second operand from the first
operand. When an operand is preceded by a minus sign, the unary operator negates its value.
For example, if a number is positive, it becomes negative when preceded with a unary minus operator. Similarly, if the
number is negative, it becomes positive after applying the unary minus operator. Consider the given example.
b = 10 a = -(b)
The result of this expression, is a = -10, because variable b has a positive value. After applying unary minus operator (-)
on the operand b, the value becomes -10, which indicates it as a negative value.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


59
Bitwise Operators
As the name suggests, bitwise operators perform operations at the bit level. These operators include bitwise AND,
bitwise OR, bitwise XOR, and shift operators. Bitwise operators expect their operands to be of integers and treat them
as a sequence of bits.
The truth tables of these bitwise operators are given below.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


60
Shift Operators
Python supports two bitwise shift operators. They are shift left (<<) and shift right (>>). These operations are used to
shift bits to the left or to the right. The syntax for a shift operation can be given as follows:

Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


61
Logical Operators
Logical AND (&&) operator is used to simultaneously evaluate two conditions or expressions with relational operators. If
expressions on both the sides (left and right side) of the logical operator are true, then the whole expression is true. For
example, If we have an expression (a>b) && (b>c), then the whole expression is true only if both expressions are true. That
is, if b is greater than a and c.
Logical OR (||) operator is used to simultaneously evaluate two conditions or expressions with relational operators. If one
or both the expressions of the logical operator is true, then the whole expression is true. For example, If we have an
expression (a>b) || (b>c), then the whole expression is true if either b is greater than a or b is greater than c.
Logical not (!) operator takes a single expression and negates the value of the expression. Logical NOT produces a zero if the
expression evaluates to a non-zero value and produces a 1 if the expression produces a zero. In other words, it just reverses
the value of the expression. For example, a = 10, b b = !a; Now, the value of b = 0. The value of a is not zero, therefore, !a =
0. The value of !a is assigned to b, hence, the result.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


62
Membership and Identity Operators
Python supports two types of membership operators–in and not in. These operators, test for membership in a sequence
such as strings, lists, or tuples.
in Operator: The operator returns true if a variable is found in the specified sequence and false otherwise. For example,
a in nums returns 1, if a is a member of nums.
not in Operator: The operator returns true if a variable is not found in the specified sequence and false otherwise. For
example, a not in nums returns 1, if a is not a member of nums.

Identity Operators
is Operator: Returns true if operands or values on both sides of the operator point to the same object and false
otherwise. For example, if a is b returns 1, if id(a) is same as id(b).
is not Operator: Returns true if operands or values on both sides of the operator does not point to the same object and
false otherwise. For example, if a is not b returns 1, if id(a) is not same as id(b).
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
63
Expressions
An expression is any legal combination of symbols (like variables, constants and operators) that represents a value. In
Python, an expression must have at least one operand (variable or constant) and can have one or more operators. On
evaluating an expression, we get a value. Operand is the value on which operator is applied.
Constant Expressions: One that involves only constants. Example: 8 + 9 – 2
Integral Expressions: One that produces an integer result after evaluating the expression. Example:
a = 10
• Floating Point Expressions: One that produces floating point results. Example: a * b / 2
• Relational Expressions: One that returns either true or false value. Example: c = a>b
• Logical Expressions: One that combines two or more relational expressions and returns a value as True or False.
Example: a>b && y! = 0
• Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z
• Assignment Expressions: One that assigns a value to a variable. Example: c = a + b or c = 10
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
64
Operations on Strings

Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


65
Slice Operations on Strings
You can extract subsets of strings by using the slice operator ([ ] and [:]). You need to specify index or the range of index
of characters to be extracted. The index of the first character is 0 and the index of the last character is n-1, where n is the
number of characters in the string.
If you want to extract characters starting from the end of the string, then you must specify the index as a negative
number. For example, the index of the last character is -1.
Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


66
Lists
Lists are the most versatile data type of Python language. A list consist of items separated by commas and enclosed within
square brackets The values stored in a list are accessed using indexes. The index of the first element being 0 and n-1 as that
of the last element, where n is the total number of elements in the list. Like strings, you can also use the slice,
concatenation and repetition operations on lists.
Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


67
Tuples
A tuple is similar to the list as it also consists of a number of values separated by commas and enclosed within
parentheses. The main difference between lists and tuples is that you can change the values in a list but not in a tuple.
This means that while tuple is a read only data type, the list is not.

Examples:

© Oxford University Press 68


2017. All rights re
Dictionary
Python’s dictionaries stores data in key-value pairs. The key values are usually strings and value can be of any data type.
The key value pairs are enclosed with curly braces ({ }). Each key value pair separated from the other using a colon (:). To
access any value in the dictionary, you just need to specify its key in square braces ([]).Basically dictionaries are used for
fast retrieval of data

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


69
Type Conversion
In Python, it is just not possible to complete certain operations that involves different types of data. For example, it is not
possible to perform "2" + 4 since one operand is an integer and the other is of string type.
Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


70
Type Casting vs Type Coercion
In the last slide, we have done explicit conversion of a value from one data type to another. This is known as type
casting.
However, in most of the programming languages including Python, there is an implicit conversion of data types either
during compilation or during run-time. This is also known type coercion. For example, in an expression that has integer
and floating point numbers (like 21 + 2.1 gives 23.1), the compiler will automatically convert the integer into floating
point number so that fractional part is not lost.

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


71

You might also like