0% found this document useful (0 votes)
85 views

Problem Solving and Programming CSE1001: Prof. Rabindra Kumar Singh

The document discusses programming languages and Python. It provides an overview of the need for programming languages to communicate with computers to solve problems. It describes what programming is, examples of programming languages, and the history and need of programming languages. It also discusses Python as a programming language for problem solving, its ranking, users, history, introduction, properties, and applications.

Uploaded by

Keer
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)
85 views

Problem Solving and Programming CSE1001: Prof. Rabindra Kumar Singh

The document discusses programming languages and Python. It provides an overview of the need for programming languages to communicate with computers to solve problems. It describes what programming is, examples of programming languages, and the history and need of programming languages. It also discusses Python as a programming language for problem solving, its ranking, users, history, introduction, properties, and applications.

Uploaded by

Keer
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/ 64

Problem Solving and Programming

CSE1001
Prof. Rabindra Kumar Singh

August 2, 2016

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

1 / 43

Need Of Programming Language


Computers can execute tasks very rapidly.
They can handle a greater amount of input data than human being.
But they can not design a strategy to solve problems for you.
Need one programming language to communicate with computer to
solve the problem.
What is a Programming?
Usually, one or more algorithms written in a programming language
that can be translated to run on a real machine. sometimes called
as software.
A programming language is somewhat like a natural language, but with
a very limited set of statements and strict syntax rules.
Has statements to implement sequential, conditional and iterative
processing algorithms.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

2 / 43

Examples of Programming Language


History of Programming Languages

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

3 / 43

Need of Programming Language

Algorithm to Hardware

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

4 / 43

Need of Programming Language

Program Development Process (Data Flow)

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

5 / 43

Need of Programming Language

Program Development Process (Control Flow)

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

8 / 43

Types of Error in Program Languages


Syntax Errors:
Statement does not obey the rules of programming language.
It refers to the structure of a program and the rules about that
structure.
Some statement in the program is not a legal statement in the
language.
Example: INT c; declaration in C language

Run Time Errors


These error does not appear until after the program has started
running.
These errors are also called exceptions.
An error occurs while the program is executing, causing the program to
terminate (divide by zero, etc.)
Due to these error, program will terminate abnormally.

Logical Errors/Semantic Errors:


The program executes to completion, but gives incorrect results.
We need to change the logic of the program to get correct results.
Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

9 / 43

Python for Problem Solving


Python Ranking

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

10 / 43

Python for Problem Solving


Python Ranking according to IEEE

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

11 / 43

Python Users

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

12 / 43

Python for Problem Solving


Python has a simple syntax and very few keywords.
Python programs are clear and easy to read and Understand.
It has Powerful programming features and highly portable and
extensible
Python is a High Level Language.
Machine Languages or Assembly Languages are referred as Low Level
Languages
High Level Languages have to be processed before they can run.
extra time.
Two types of programs translators to convert High Level Program to
Low Level program
Compiler
Interpreter

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

13 / 43

Brief History of Python


Invented in the Netherlands, early 90s by Guido van Rossum.
Named after Monty Python.
Open sourced from the beginning.
Considered a scripting language, but is much more Scalable, object
oriented and functional from the beginning.
It is a object oriented programming language. everything is object.
Genealogy:
Setl (NYU, J.Schwartz et al. 1969-1980).
ABC (Amsterdam, Meertens et al. 1980-).
Python (Van Rossum et all. 1996-).

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

14 / 43

Time Line of Python

Python born, name picked - Dec 1989.


First public release (USENET) - Feb 1991
python.org website - 1996 or 1997
2.0 released - 2000
Python Software Foundation - 2001
...
...
2.4 released - 2004
2.5 released 2006
Current version: 3.5.2 and 2.7.12

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

15 / 43

Introduction to Python

Object oriented language

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type
Independent from platforms

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type
Independent from platforms
Focused on development time

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type
Independent from platforms
Focused on development time
Simple and easy grammar

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type
Independent from platforms
Focused on development time
Simple and easy grammar
Its free Invalid Identifiers(open source)

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type
Independent from platforms
Focused on development time
Simple and easy grammar
Its free Invalid Identifiers(open source)
Automatic memory management

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Introduction to Python

Object oriented language


Interpreted language
Supports dynamic data type
Independent from platforms
Focused on development time
Simple and easy grammar
Its free Invalid Identifiers(open source)
Automatic memory management
Glue language Interactive front-end for FORTRAN/C/C++ code

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

16 / 43

Python Language Properties

Everything is an object

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Python Language Properties

Everything is an object
Modules, classes, functions

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Python Language Properties

Everything is an object
Modules, classes, functions
Exception handling

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Python Language Properties

Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Python Language Properties

Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
Static scoping

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Python Language Properties

Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
Static scoping
Operator overloading

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Python Language Properties

Everything is an object
Modules, classes, functions
Exception handling
Dynamic typing, polymorphism
Static scoping
Operator overloading
Indentation for block structure

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

17 / 43

Where to use Python

System management (i.e., scripting)

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)
Internet programming

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)
Internet programming
Database (DB) programming

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)
Internet programming
Database (DB) programming
Text data processing

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)
Internet programming
Database (DB) programming
Text data processing
Distributed processing

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)
Internet programming
Database (DB) programming
Text data processing
Distributed processing
Numerical operations

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

18 / 43

Where to use Python

System management (i.e., scripting)


Graphic User Interface (GUI)
Internet programming
Database (DB) programming
Text data processing
Distributed processing
Numerical operations
Graphics so on...

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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.

More number of lines of code Script Mode


We are able to save the code
Editing and Execution of the code will be support in future also.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

23 / 43

Introduction to Python
Flow Chart for the problem:

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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.

Rules for Identifier


Python is Case Sensitive. Good is different from good.
Identifiers may contains Letters and Digits. can not start with digit
The special underscore character can also be used. readability of
long identifier names.
Spaces are not allowed as part of an identifier
The underscore characters not be used as the first character.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

25 / 43

Identifiers in Python

Valid Identifiers
totalsales
totalsales
salesfor2010
sales for 2010

Invalid Identifiers
totalsales
total sales
2010sales
2010sales

Prof. Rabindra Kumar Singh

Reasons for Invalid


Quotes are not allowed
Space is not allowed
Can not begin with a digit
Should not begin with underscore

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

27 / 43

Values and Types


Value:
It is one of the basic things a program works with, like a letter or a
number.
Examples: 1, 2 and Hello World
1 and 2 are belongs to Integer Category and Hello World is a String.
If we want to know the type of the values:
>>> type(Hello, World) <class,str>.

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Problem Solving and Programming CSE1001

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

Problem Solving and Programming CSE1001

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

Problem Solving and Programming CSE1001

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 )

This i s a l s o Rabindra Singh


dfd

p r i n t (16+16)
Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

A string consisting of a single character


A string consisting of non-letter characters
A string consisting of a single quote characters
A string containing a single blank character
the empty string

Problem Solving and Programming CSE1001

August 2, 2016

35 / 43

Introduction to Python

>>> print(Hello) check output


>>> print(Hello) check output
>>> print(Lets Go) Check output
>>> print(Hello) check output
>>> print(Lets Go!) check output
>>> print(Lets Go!) check output

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

2. Floating point Literals:


It contains decimal point.
the integer and exponent parts are always interpreted using radix 10.
Example: 3.18, 1e200, 0e0

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

Problem Solving and Programming CSE1001

August 2, 2016

37 / 43

Introduction to Python
Examples on Numerical Literals

>>> 1024 check output


>>> -1024 check output
>>> .1024 check output
>>> 1,024 check output
>>> 0.1024 check output
>>> 1,024.56 check output
Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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.

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

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

Problem Solving and Programming CSE1001

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???

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

42 / 43

Introduction to Python

Types of Numbers supported by Python


1. Integers
2. Floating Point Numbers
3. Complex Numbers
4. Fractional Numbers
Integers:
Integers have no fractional part in the number.
Integer type automatically provides extra precision for large numbers
like this when needed (different in Python 2.X)
>>> a = 10

Prof. Rabindra Kumar Singh

Problem Solving and Programming CSE1001

August 2, 2016

43 / 43

You might also like