0% found this document useful (0 votes)
10 views39 pages

PSPP Theory Unit 2@

Uploaded by

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

PSPP Theory Unit 2@

Uploaded by

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

GE3151 Problem Solving and Python Programming

ALOK KUMAR
Assistant Professor/CSE
Government College of Engineering, Dharmapuri
OBJECTIVES
● To understand the basics of algorithmic problem solving.
● To learn to solve problems using Python conditionals and loops.
● To define Python functions and use function calls to solve problems.
● To use Python data structures - lists, tuples, dictionaries to represent
complex data.
● To do input/output with files in Python.

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


SYLLABUS

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Practical Syllabus

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
BOOKS

1. https://drive.google.com/file/d/1R3-xjos7u_MqPPY8Dy_oyIMz32nn9ExD/view?usp=sharing

2. https://drive.google.com/file/d/1pRs9tiNcxqqluZIk1FR-3OYAh1Biug51/view?usp=sharing

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Unit 2: Data, Expressions, Statements
Python:
● It is a programming language
● developed by Guido Van Rossum in 1990

Features of Python:
1. Open Source
2. Easy to Learn
3. High Level Language
4. Portable
5. Object Oriented

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Compiler vs Interpreter:

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Uses of Python:
● Web Development
● Graphical user Interface
● Game Programming
● Database access and Programming
● Web scripting (to create dynamic web pages)

Python IDLE (Integrated Development Environment):


● Download Python 3.9.0 (https://www.python.org/downloads/) and install
OR
● Use online python interpreter (https://www.onlinegdb.com/online_python_interpreter)

Python IDLE Modes:


1. Interactive Mode
2. Development Mode

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


1. Interactive Mode
● With this mode we can interact with IDLE directly
● this mode we can search in computer by typing IDLE
● In this we can execute python command directly and easily

(1) (2)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
2. Development Mode
● for executing lengthy programs we can use this mode
● In this mode for making program firstly open New File and Type program in opened window

(1) (2)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
● In this mode for saving program we have to click on Save As and we have to give any
file name (for example here hello) and extension as .py

(3) (4)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
● In this mode for executing program we have to click on Run or F5 key and in new
window automatically output will come

(5) (6)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Debugging in Python:
● Debugging is the process of detecting and removing of existing and potential errors (also called as 'bugs')
in a program
● Debugging in Python is facilitated by pdb module (python debugger) which comes in-built to the Python
standard library.
● To start debugging within the program just insert import pdb, pdb.set_trace() command

import pdb import pdb


while debugging, found error
pdb.set_trace( ) pdb.set_trace( )
-> print(c)
a=10 a=10
-> c=a+d (Pdb) next
b=20 b=20
(Pdb) next 30
c=a+d c=a+b
NameError: name 'd' is not defined
print(c) print(c)

detection of error after removing error


Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Python Data Types:
● Data types are the classification or categorization of data items
● It represents the kind of value that tells what operations can be performed on a particular data

There are various data types in Python:

1. Number (int, float, complex)


2. String
3. Boolean
4. List
5. Tuple
6. Dictionary
7. Set

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


1. Number:
● Number data type represent the data which has numeric value
● Numeric value can be integer, floating number, complex number
● These values are defined as int, float and complex class in Python

Int:
● It contains positive or negative whole numbers (without fraction or decimal)
For example: a=5
Float:
● It is a real number with floating point representation. It is specified by a decimal point
For example: a = 5.0

Complex:
● It is specified as (real part) + (imaginary part)j
For example: 2+3j

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


2. String:

● String is a sequence of characters


● we use single quotes or double quotes to represent strings

4th

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


3. Boolean:

● Data type with one of the two built-in values, True or False
● It is denoted by the class bool

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


4. List:

● List is an ordered sequence of items


● It is one of the most used data type in Python and is very flexible
● All the items in a list do not need to be of the same type
● Lists are mutable

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


5. Tuple:

● Tuple is an ordered sequence of items same as a list


● The only difference is that tuples are immutable (not mutable)
● Tuples once created cannot be modified

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


6. Dictionary:

● Dictionary is an unordered collection of key-value pairs


● It is generally used when we have a huge amount of data
● dictionaries are defined within braces { } with each item being a pair in the form key:value
● Key and value can be of any type

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


7. Set:

● Set is an unordered collection of unique items


● Set is defined by values separated by comma inside braces { }
● Items in a set are not ordered

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Variables:
● Variables are reserved memory locations to store values
● Based on the data type of a variable interpreter allocates memory

Rules for creating variables:

1. Must begin with a letter (a - z, A - Z) or underscore ( _ )


2. Other characters can be letters, numbers or _
3. Variable names are Case Sensitive
4. There are some reserved words which you cannot use as a variable name because
Python uses them for other things

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Keywords:
● keywords are the reserved words
● We cannot use a keyword as a variable name, function name
● keywords are case sensitive

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Expressions:

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Statements:

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Comments:
● Comments can be used to explain Python code
● Comments can be used to make the code more readable

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Operators:
● Operators are special symbols in Python that carry out arithmetic or logical computation
● The value that the operator operates on is called the operand

For example:
>>> 2+3
5

Here,
● + is the operator that performs addition

● 2 and 3 are the operands and 5 is the output of the operation

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Python has a number of operators:

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Precedence of Operators:
● Python uses a rule known as PEMDAS

P – Parentheses
E – Exponentiation
M – Multiplication
D – Division
A – Addition
S – Subtraction
● The precedence of operators is listed from High to low

For Example: ((((6+4)*2)-10)//2)-4*2

Output: -3
Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)
Illustrative Problems:
1) Exchange the value of two variables:
● In this program, we use the t variable to hold the value of variable a temporarily
● We then put the value of variable b in variable a and later variable t in variable b
● In this way, the values get exchanged

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


2) Distance between two points:
● The formula for distance between two point (x1, y1) and (x2, y2) is:

● distance formula derived from Pythagorean theorem

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


3) Circulate the values of n variables:
● for circulating the value of n variables we can take input list having n values
● for circulating in positive direction (clockwise direction) we use value of x as positive and for
circulating in negative direction (anticlockwise direction) we use value of x as negative

list=[10,20,30,40,50]
x=2 #Shift 2 location in positive direction (clockwise direction)
print(list[-x : ]+list[ : -x])

Output: [40,50,10,20,30]

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)


Thanks !

Government College of Engineering, Dharmapuri (ALOK KUMAR, A.P/CSE)

You might also like