Problem Solving and Programming CSE1001: Prof. Rabindra Kumar Singh
Problem Solving and Programming CSE1001: Prof. Rabindra Kumar Singh
CSE1001
Prof. Rabindra Kumar Singh
August 2, 2016
August 2, 2016
1 / 43
August 2, 2016
2 / 43
August 2, 2016
3 / 43
Algorithm to Hardware
August 2, 2016
4 / 43
August 2, 2016
5 / 43
August 2, 2016
6 / 43
Compiler
It is a program that converts a program written in a programming
language into a program in the native language, called machine
language, of the machine that is to execute the program.
It reads the program and translates it completely before the program
starts running.
High Level Program is called Source Code
Translated Program is called Object Code or Executable Code
August 2, 2016
7 / 43
Interpreter
Interpreter
It takes our programs statements at a time (one by one) and executes
a corresponding set of machine instructions.
It is alternative to compiler.
The processing is slow.
August 2, 2016
8 / 43
August 2, 2016
9 / 43
August 2, 2016
10 / 43
August 2, 2016
11 / 43
Python Users
August 2, 2016
12 / 43
August 2, 2016
13 / 43
August 2, 2016
14 / 43
August 2, 2016
15 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Introduction to Python
August 2, 2016
16 / 43
Everything is an object
August 2, 2016
17 / 43
Everything is an object
Modules, classes, functions
August 2, 2016
17 / 43
Everything is an object
Modules, classes, functions
Exception handling
August 2, 2016
17 / 43
Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
August 2, 2016
17 / 43
Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
Static scoping
August 2, 2016
17 / 43
Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
Static scoping
Operator overloading
August 2, 2016
17 / 43
Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
Static scoping
Operator overloading
Indentation for block structure
August 2, 2016
17 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
August 2, 2016
18 / 43
Introduction to Python
Two ways to use python interpreter
1. Interactive Mode
2. Script Mode
Interactive Mode:
we have to type python programs Interpreter displays the result.
>>> 16+16
32
The shell prompt, >>>, is the prompt the interpreter uses to indicate
that it is ready.
August 2, 2016
19 / 43
Introduction to Python
Script Mode:
We can store code in a file and we can use interpreter to execute the
content of the file script.
Every python script saved with extension .py
Testing small pieces of code Interactive Mode is good
Type and execute the piece of code immediately.
August 2, 2016
20 / 43
Introduction to Python
In Python, string literals may be delimited (surrounded) by a
matching pair of either single () or double () quotes.
Problems that can be solved by Sequential Algorithms
Example:
Little Bob loves chocolate, and he goes to a store with Rs. N in his pocket.
The price of each chocolate is Rs. C. The store offers a discount: for every
M wrappers he gives to the store, he gets one chocolate for free. This offer
shall be availed only once. How many chocolates does Bob get to eat?
PAC Chart:
Input
MoneyInHand,
CostOfChocolate, WrapperOffer
Processing
Output
Numbero fChocolates
=
NumberOfChocolate
Chocolategotbymoney +
Chocolategotbyreturningwrapper
Alternative Solutions
-
August 2, 2016
21 / 43
Introduction to Python
same Example with values
Bob has Rs. 100 in his pocket. The price of each chocolate is Rs. 5. The
store offers a free chocolate for every 4 wrappers he gives to the store, and
he gives all wrappers. This offer is available only once. How many
chocolates does Bob get to eat?
simple procedure
n = 100
c=5
m=4
p = n/c
f = p/m
print(Number of chocolates::, p+f)
Type above procedure in python console check output
so we need to write generalized procedure for the above problem
Prof. Rabindra Kumar Singh
August 2, 2016
22 / 43
Introduction to Python
Pseudo Code
STEP -1: READ MoneyInHand and CostOfChocolate
STEP -2: COMPUTE NumberOfChocolate as MoneyInHand or
CostOfChocolate
STEP -3: NumberOfChocolate = NumberOfChocolate
+(NumberOfChocolate or WrapperOffer)
STEP -4: PRINT NumOfChocolates
August 2, 2016
23 / 43
Introduction to Python
Flow Chart for the problem:
August 2, 2016
24 / 43
Identifiers in Python
Identifier:
It is a sequence of one or more characters used to name a given
program element.
Example: line, salary, ragala10, VIT UNIVERSITY.
August 2, 2016
25 / 43
Identifiers in Python
Valid Identifiers
totalsales
totalsales
salesfor2010
sales for 2010
Invalid Identifiers
totalsales
total sales
2010sales
2010sales
August 2, 2016
26 / 43
Keywords in Python
Keywords:
It is an identifier that has pre-defined meaning in a programming
language.
So Keywords can not be used for regular Identifiers.
If we use Keywords as identifiers in program syntax errors
Example: >>> and = 10
SyntaxError: invalid Syntax
August 2, 2016
27 / 43
Examples
>>>type(17) result ???
>>>type(3.2) output ???
>>>type(17) output ???
>>>type(17) Output ??
>>>type(3.2) Output??
>>>1,00,000 What would be the Answer Semantic Error.
result is (1,0,0) interpreter as comma separated sequence of integers
August 2, 2016
28 / 43
Variables in Python
Variable:
It a name or identifier that refers to a value.
An Assignment Statement creates new variables and gives them new
values:
>>> ram = VIT University Chennai Campus is near Kandigai
>>> n = 16
>>> pi = 3.1415
State Diagrams or variables are discuss in class
The type of a variable is the type of the value it refers to.
>>>type(ram) result???
Meaningful names can be chosen to describe variables.
Variable names can be arbitrarily long.
Variable names contain both letters and numbers, but they have to
begin with a letter.
August 2, 2016
29 / 43
Variables Examples
Example
a = 0
b = 2
p r i n t ( a+b )
# o t h e r code
m =0
j= 2
p r i n t (m+j )
# o t h e r Code
k = 0
l =2
p r i n t ( i n t ( k)+ l )
Prof. Rabindra Kumar Singh
August 2, 2016
30 / 43
Variables Examples
Example
s t r = My Name i s R a b i n d r a
print ( str )
OUTPUT:??
Example
s t r 1 = How z l i f e
print ( str1 )
OUTPUT:??
Example
s t r 2 = How z l i f e
print ( str2 )
Prof. Rabindra Kumar Singh
August 2, 2016
31 / 43
Comments in Python
Adding notes to our program is good idea. more readability
These Notes are called Comments.
Comments in Python are two types: 1. Single Line Comments
2. Multiline Comments
Single-line comments begin with the hash character (#) and are
terminated by the end of line.
Python ignores all text that comes after # to end of line
Comments are most useful when they document non-obvious features
of the code.
Example-1
# compute the percentage of the hour that has elapsed
>>>percentage = (minute * 100) /60
Example-2
>>>percentage = (minute * 100) /60 # Percentage of an Hour
Prof. Rabindra Kumar Singh
August 2, 2016
32 / 43
Comments in Python
Multiline Comments:
It can be specified with triple-quoted strings.
Example
T h i s i s Rk S i n g h
Working i n VIT U n i v e r s i t y
p r i n t ( GHK )
p r i n t (16+16)
Prof. Rabindra Kumar Singh
August 2, 2016
33 / 43
Literals in Python
Literal:
Literals are notations for constant values of some built-in types.
It is a sequence of one or more characters that stands for itself
Different types of Literals used in Python:
String and Bytes Literals
Numerical Literals
1 Integer Literals
2 Floating Point Literals
3 Imaginary Literals
August 2, 2016
34 / 43
Literals
String Literal:
It represents a sequence of characters.
Example: Hello, VIT, Chennai, Vellore-600127
In Python, string literals may be delimited (surrounded) by a matching
pair of either single () or double () quotes.
Example
p r i n t ( Welcome t o Python )
A
abc@vit.ac.in
howz life
August 2, 2016
35 / 43
Introduction to Python
August 2, 2016
36 / 43
Literals
Numeric Literals:
There are three types.
1. Integer Literals:
A numeric literal is a literal containing only the digits 0-9, an optional
sign character (1 or 2),and a possible decimal point. (e exponential
notation)
leading zeros in a non-zero decimal number are not allowed.
There is no limit for the length of integer literals apart from what can
be stored in available memory.
Examples: 7 , 3 , 2147483647 etc
3.Imaginary Literals:
An imaginary literal yields a complex number with a real part of 0.0.
Complex numbers are represented as a pair of floating point numbers
and have the same restrictions on their range.
Examples: 3.14j, 3.14e+10j
Prof. Rabindra Kumar Singh
August 2, 2016
37 / 43
Introduction to Python
Examples on Numerical Literals
August 2, 2016
38 / 43
Introduction to Python
Control Characters:
Special characters that are not displayed on the screen. Control the
display of output.
Control characters do not have a corresponding keyboard character and
represented by a combination of characters called an escape sequence.
The backslash (\) serves as the escape character in Python.
For example, the escape sequence the newline control character,
used to begin a new screen line.
August 2, 2016
39 / 43
Introduction to Python
Data Types:
Pythons data types are built in the core of the language.
They are easy to use and straightforward.
Data types supported by Python
1. Boolean Values
2. None
3. Numbers
4. Strings
5. Tuples
6. Lists
7. Sets
August 2, 2016
40 / 43
Introduction to Python
Boolean values:
Primitive datatype having one of two values: True or False.
Some common values that are considered to be True or False.
Find the outputs for the following
Examples
p r i n t ( bool ( True ) )
p r i n t ( bool ( F a l s e ) )
p r i n t ( bool ( ramesh ) )
p r i n t ( bool ( ) )
p r i n t ( bool ( ) )
p r i n t ( bool ( 0 ) )
p r i n t ( bool ( 3 ) )
p r i n t ( bool ( None ) )
p r i n t ( True + 2 5 )
p r i n t ( F a l s e + 25)
p r i n t ( bool . b a s e s )
Prof. Rabindra Kumar Singh
August 2, 2016
41 / 43
Introduction to Python
None:
None is a special value.
It is a value that indicates no value.
Can be used to check for emptiness.
Try it: type(None) Output???
>>> x = None
>>> help(x) Output???
August 2, 2016
42 / 43
Introduction to Python
August 2, 2016
43 / 43