0% found this document useful (0 votes)
4 views54 pages

Unit I PPS

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)
4 views54 pages

Unit I PPS

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/ 54

Introduction to Programming

and Problem Solving


By
Mr. Rishikesh G. Tambe
Assistant Professor, Computer Department,
AVCOE, Sangamner
Introduction to Programming and Problem Solving
• Total Units: 4
• Exam Type: Online MCQ based Test
• Number of Lectures per week: 4 hrs
• Credits: 4
• Practical's: 4
• Number of Assignments: 4 (One on each Unit)
• Extra Lectures: Yes, if required.
Objectives of PPS
• To understand problem solving, problem solving aspects,
programming and to know about various program design tools.

• To learn problem solving with computers

• To learn basics, features and future of Python programming.

• To acquaint with data types, input output statements, decision


making, looping and functions in Python

• To learn features of Strings in Python


Outcomes of PPS
• Inculcate and apply various skills in problem solving.
• Choose most appropriate programming constructs and features to
solve the problems in diversified domains.
• Exhibit the programming skills for the problems those require the
writing of well-documented programs including use of the logical
constructs of language, Python.
• Demonstrate significant experience with the Python program
development environment.
Units in PPS
Units Name of Unit

Problem Solving, Programming and Python Programming


1

2 Decision Control Statements

Functions and Modules


3

4 Strings in Python
Unit One
• Problem Solving, Programming and Python Programming

• Contents:
1. General Problem Solving
2. Program Design Tools
3. Basics of Python Programming
Problem and Problem Solving
• Problem:
A problem exists for an individual when he has a definite goal he can not reach.

• Problem Solving:
It is a cognitive processing directed at achieving a goal where no solution method
is obvious to the problem solver.

• Problem Solving Steps:


• Identification of the problem
• Understanding the problem
• Finding all possible solution
• Selecting best solution among all possible solution.
• Implementation
• Evaluation/Testing
Difficulties and Types of Problem Solving
• Difficulties in Problem Solving:
1. Identifying wrong problem
2. Understanding problem wrongly
3. Selecting wrong possible solution
4. Lack of resources
5. Making wrong decision
• Types of Problems:
1. Heuristic Problems
Heuristic problems are those problems which are solved using trial and error basis. Problems
which do not have any specific method to get a solution. Example: Solving a maze.
2. Algorithmic Problems
Algorithmic problems are those problems which have specific steps for getting a solution.
Example: Solving problems using computers.
Top Down Design Approach
• Top Down Design Approach:
 Top-down design is a process that moves from a high-level design concept towards a lower
level.
 A high-level design concept is larger, broader and more general; a low-level design concept is
smaller, more specific and detailed.

High-Level
Large
Broader
General

Low-Level
Small
Specific
Detailed
Top Down Design Approach
• Top Down Approach for Dividing Problem into sub problem: Top-Down Approach

Problem

Problem A Problem B

Sub-Problem A1 Sub-Problem A2 Sub-Problem B1 Sub-Problem B2

Sub-Problem B11 Sub-Problem B12


Top Down Design Approach
• Example 1:

+ *
Top Down Design Approach
• Top Down Approach for Getting Information at different Levels: Top-Down Approach
Top Down Design Approach
• Example 2:
Program Design Tools
• Program:
Program is a set or collection of Instructions.

• Programming Language:
Programming Language is a way for communicating with computer through
programs.
Through programming language we can describe designing tools.
Example: Python, Java, C, C++, .Net, R, HTML, Javascript and so on.

• Program Design Tools:


• Algorithm
• Flowchart
• Pseudo code
Program Design Tools
• Algorithm:
An algorithm is a set of well-defined instructions in sequence to solve a
problem.

• Characteristics of Algorithm:
• Minimum one Input
• Minimum one Output
• Finiteness (Finite number of Instructions )
• Effectiveness (Algorithms should be most effective among many
different ways to solve a problem)
• Definiteness (Each step in the algorithm should be clear and unambiguous.)
• Example 1: Algorithm for Making Tea Program Design Tools
Step 1: Start

Step 2:Take water in the kettle.

Step 3: Add sugar in kettle.

Step 4: Add tea powder in the kettle.

Step 5: Boil for few minutes.

Step 6: Add milk in the kettle.

Step 7: Boil for few minutes.

Step 8: Pour in the Cup.

Step 9: Stop
Program Design Tools
• Example 2: Algorithm for Adding Two Integer Numbers
Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

Step 5: Display sum

Step 6: Stop
• Example 3: Algorithm for Even/Odd Number Program Design Tools
Step 1: Start

Step 2: Declare variables num1.

Step 3: Read values num1.

Step 4: Divide the num1 by 2 and check the remainder is 0 or 1.

Step 5: If remainder is zero then num1 is even and if remainder is


non-zero value then num1 is odd.

Step 6: Display Even or Odd.

Step 7: Stop
Program Design Tools
• Flowchart: Flowchart is the pictorial presentation of algorithm.
Program Design Tools
• Example 2: Algorithm for Adding Two
Integer Numbers
Step 1: Start

Step 2: Declare variables num1, num2


and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and


assign the result to sum.

Step 5: Display sum

Step 6: Stop
Program Design Tools
• Example 3: Algorithm for Even/Odd Number

Step 1: Start

Step 2: Declare variables no.

Step 3: Read values no.

Step 4: Divide the no by 2 and check the remainder is


0 or 1.

Step 5: If remainder is zero then num1 is even and if


remainder is non-zero value then num1 is odd.

Step 6: Display Even or Odd.

Step 7: Stop
Programming Language
• Definition:
 A programming language is a notation designed to connect instructions to a
machine or a computer.
 Programming languages are mainly used to control the performance of a
machine or to express algorithms.

• Program:
A computer program is a collection of instruction that can be executed by
a computer to perform a specific task
Types of Programming Language

Python C Java FORTRAN

High-Level Language

Assembly Language (Mnemonic Symbols)

Machine Language (0s and 1s) Low Level Language

Hardware (Switches and Gates)


• Procedural Programming Language (Imperative Programming Language) :
The procedural programming language is used to execute a sequence of statements which lead to a result.
Example: COBOL, ADA,C, FORTRAN

• Functional Programming Language


Functional programming language typically uses stored data, frequently avoiding loops in favor of recursive functions.
Example: LISP, APL

• Object-oriented Programming Language


This programming language views the world as a group of objects that have internal data and external accessing parts of
that data.
Example: Python, JAVA, C++

• Scripting Programming Language


These programming languages are often procedural and may comprise object-oriented language elements, but they fall
into their own category as they are normally not full-fledged programming languages with support for development of large
systems.
Example: PHP, JAVASCRIPT

• Logic Programming Language


These types of languages let programmers make declarative statements and then allow the machine to reason about the
consequences of those statements.
Example: Prolog
Introduction to Python
Features of Python Use of Python
• General Purpose Language • Google
• Free and Open Source • Facebook
• High Level Programming Language • NASA
• Simple and Easy • Instagram
• Portable • YouTube (scripted Python)
• Programs are Interpreted • Nokia
• Object-oriented • Top companies for business
• Embedded with C or C++ application development.
programs
• Rich set of standard Libraries
Installing and Execution of Python Programming Language
• Official Website www.python.org

• Go in download section and choose python for Windows, Mac OS or


Linux operating system.

• Two Popular versions: Python 2.X and Python 3.X

• Two modes of writing programs in python:

 Interactive Mode

 Script Mode (Normal Mode)


Variable in Python
Definition: Variable is name given to memory space where data is stored.
Rules for Variable Name
• It should be meaningful.
Example: number, name, rollno, division, pincode etc

• It should not start with number and symbol (except underscore “_”).
Example: 1number, 45name, #division, *pincode are not valid
Example: number1, name45 are valid

• Keywords should not be used as variable name.


Example: if, else, for, while, class, return are not valid

• Underscore symbol is allowed.


Example: _number, first_name, roll_no, pin_code etc
Variable in Python
Definition: Variable is name given to memory space where data is stored.

Address of Memory Cell

Value Stored in
Memory the Memory
Cell

Variable Name

Memory Cell
Variable in Python

Variables a = 10 b = 25.50 c = “AVCOE”


Address of Memory Cell FFF001 FFF002 FFF003

Value Stored in
the Memory 10 25.50 AVCOE
Cell

Variable Name a b c
Assigning values to Variables in Python
Declaring and assigning single value to a variable
a = 10 -----> Declared variable a and assigned value 10.

b = 25.50 -----> Declared variable b and assigned value 25.50

c = “AVCOE” -----> Declared variable c and assigned value AVCOE.


Declaring and assigning multiple values

a, b, c = 10, 25.50, “AVCOE”


Declared variable a, b and c and assigned value 10, 25.50 and AVCOE, respectively at the
same time.

a, b, c = “AVCOE”
Declared variable a, b and c and assigning same value AVCOE to all variables.
Assigning values to Variables in Python
Changing value of a variable in same program

Program 1 Output 1
a = 10
10
print(a)

Program 2 Output 2
a = 10
10
print(a)
AVCOE
a = “AVCOE”
print(a)
Constant in Python
Definition:
A constant is a type of variable whose value cannot be changed.
PI = 3.14

• For our own understanding, we represent constants in upper case


alphabets to differentiate from variables.
• Same rules for naming conventions as variables.
Literals in Python
Definition: Literal is a raw data given in a variable or constant.

• Numeric Literals

• String literals

• Boolean literals

• Special literals

• Literal Collections
Numeric Literals in Python
Definition: Numeric Literals are immutable (unchangeable). Numeric literals can
belong to 3 different numerical types: Integer, Float and Complex
Binary Literals:
Binary literals begin with a leading 0b or 0B, followed by binary digits (0-1).
a = 0b1100 -------> 12, a = 0B1111 ------> 15

Decimal Literals:
Decimal literals are specified as signed or unsigned numbers of 1 to 38.
a = 100 -------> 100, a = 200 ------> 200
Numeric Literals in Python
Definition: Numeric Literals are immutable (unchangeable). Numeric literals can
belong to 3 different numerical types: Integer, Float and Complex

Octal Literals:
Octal literals start with a leading 0o or 0O (zero and lower- or uppercase letter o),
followed by a string of digits (0-7).
a = 0o14 -------> 12, a = 0O24 ------> 20

Hexadecimal Literals:
Hexadecimals start with a leading 0x or 0X, followed by a string of hexadecimal
digits (0-9 and A-F).
a = 0x11 -------> 17, a = 0X1a ------> 26
String Literals in Python
Definition: A string literal is a sequence of characters surrounded by quotes. We
can use both single, double, or triple quotes for a string. And, a character literal is a
single character surrounded by single or double quotes.

String Literals:

a = ‘H’ -------> H, a = “ H ” ------> H

a = ‘ AVCOE ’ ------> AVCOE, a = “AVCOE” ------> AVCOE,

a = “““ I am Student of H Division””” ------> I am Student of H Division


Boolean Literals in Python
Definition: A Boolean literal can have any of the two values: True and False
Boolean Literals:
a = True -------> True
a = False ------> False

Program 1 Output 1 Program 2 Output 2 Program 3 Output 3


a = True a = False a = True
b=4 b=4 b = True
c=a+b
5 c=a+b
4 c=a+b
2
print(c) print(c) print(c)
Special Literals in Python
Definition: Python contains one special literal i.e. None

Program 1 Output 1
a = None
print(a)
None
Literal Collections in Python
Definition: There are four different literal collections
1. List
2. Tuple
3. Dictionary
4. Set
5. String

fruits = ["apple", "mango", "orange"] --------> List

numbers = (1, 2, 3) --------> Tuple

alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} --------> Dictionary

vowels = {'a', 'e', 'i' , 'o', 'u'} --------> Set


Operators in Python

1. Arithmetic Operators
2. Relational/Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Identity Operators
7. Membership Operators
Arithmetic Operators
1. Addition (+)
2. Substraction (-)
3. Multiplication (*)
4. Division (/, forward slash)
5. Floor division (//, two forward slash)
6. Power/Exponential Operator (**)
7. Modulus or Mod Operator (%)

Difference between division , floor division and modulus operator.


1. Division operator returns Quotient.
2. Floor division also returns Quotient but discards digits after decimal
points.
3. Modulus operator returns 0 if remainder is zero otherwise returns 1.
Relational Operators
1. Greater then(>)
2. Less then (<)
3. Equal to (==)
4. Greater then equal to (=>)
5. Less then equal to (=<)
6. Not Equal to (!=)
a = 10 a = 10 a = 10 a = 10
b = 20 b = 20 b = 20 b = 20
c = a<b c = a>b c = (a==b) c = (a!=b)
print(c) print(c) print(c) print(c)
True False False True
Logical Operators
1. Logical AND Operator (and)
2. Logical OR Operator (or)
3. Logical NOT Operator (not)
Logical Operators
a = 20 a = 20
b = 10 b = 10
c=5 c = 50
d = a>b and a>c d = a>b and a>c a = 20
print(d) print(d) b = 30
True False c = 50
a = 20 a = 20 d = not(a>b or a>c)
b = 10 b = 30 print(d)
c=5 c = 50 True
d = a>b or a>c d = a>b or a>c
print(d) print(d)
True False
Bitwise Operators
1. Bitwise AND Operator (&)
2. Bitwise OR Operator (|)
3. Bitwise XOR Operator (^)
4. Bitwise NOT Operator (~)
5. Left Shift Operator (<<)
6. Right Shift Operator (>>)
Bitwise Operators

a=2 a=2 a=2


b=3 b=3 b=3
c=a&b c=a|b c=a^b
print(c) print(c) print(c)
2 3 1

a=8 a=8 a=8


b=6 b=6 b=6
c=a&b c=a|b c=a^b
print(c) print(c) print(c)
? ? ?
Bitwise Shift Operators

a=7 a=6
b = a<<1 c = a >>1
print(b) print(c)
14 ?

a=7 a=7
b = a<<2 c = a>>3
print(b) print(c)
28 ?
Assignment Operators
• Assignment operators are used in Python to assign values to variables.

Operator Example Equivalent to


= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x=x&5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Special Operators in Python
Python language offers some special types of operators like the identity operator or the
membership operator.

Identity Operators:

• is and is not are the identity operators in Python.


• is will return True if the operands are identical.
• is not will return True if the operands are not identical.

Remember:
• Identity operators check the memory location of the variable and not contents.
• If two variables have same values that doesn’t mean that they are identical.
a, b a b
a = 10 a = 10
b = 10 10 b = 20 10 20
FFF0001 FFF0001 FFF0002
Special Operators in Python
Membership Operators:

• in and not in are the membership operators in Python.


• in will return True if the value is found in the sequence.
• not in will return True if the value is not found in the sequence.

a = "AVCOE" a = "AVCOE" a = "AVCOE" a = "AVCOE"


c = 'A' in a c = ‘X' in a c = ‘X' not in a c = ‘A' not in a
print(c) print(c) print(c) print(c)
True False True False
Indentation in Python

• Python uses indentation to define a block of code

• A code block start with indentation and ends with the first unindented line.

• Generally four whitespaces are used for indentation.

• Indentation makes code look neat, clean and readable.

• Incorrect indentation result into Indentation error.


Keywords/Reserved Words in Python
False class finally is return

None continue for lambda try

True def from nonlocal while

and del global not with

as elif if or yield

assert else import pass

break except in raise


Operator Precedence in Python
Operator Precedence
() Parentheses
** Exponent
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR

You might also like