CS25C02 Computer Programming: Python
Introduction to Python: Problem Solving, Problem Analysis
Chart, Developing an Algorithm, Flowchart and Pseudocode,
Interactive and Script Mode, Indentation, Comments, Error
messages, Variables, Reserved Words, Data Types,
Arithmetic operators and expressions, Built-in Functions,
Importing from Packages.
Practical: Problem Analysis Chart, Flowchart and Pseudocode
Practices. (Minimum three)
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
PROGRAM
set of instructions which is run by the computer to perform
specific task. The task of developing program is called
programming.
COMPUTATIONAL PROBLEM SOLVING
providing logic for solving a problem
1. Algorithms
2. Flowcharts.
3. Pseudo codes.
4. Programs
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
ALGORITHM
Sequence of instructions
Method for solving a problem
Step by step procedure for solving a problem
Ordered, Unambiguous and Finite in number
1.3.1 Properties of Algorithms
Written in simple English
Accurate and explicit
Instructions should not be repeated infinitely.
Conclude after a finite number of steps.
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
Derived results should be obtained only after the algorithm
terminates.
Qualities of a good algorithm
Time –lesser time is required then it is the better one
Memory –lesser memory is required then it is the better
Accuracy – Multiple algorithms may provide suitable or
correct solutions to a given problem, suitable.
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
Algorithm Example:
Write an algorithm to print, “Welcome to python world”
• Step 1: Start
• Step 2: Print “Welcome to python world”
• Step 3: Stop
• Step 1: Start Step 2: get a,b
• Step 3: calculate c=a+b Step 4: Print c
• Step 5: Stop
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
FLOW CHART
The solution of any problem in picture form is
called flowchart. It is the one of the most
important technique to depict an
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE,
Flow Chart
• Example: Write an algorithm to check whether the person
is eligible to vote? Step 1: Start
• Step 2: Get age
• Step 3: if age >= 18 print “Eligible to vote” Step 4: else print
“Not eligible to vote”
• Step 6: Stop
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Example: Algorithm for addition of two numbers using function
Main function()
• Step 1: Start
• Step 2: Call the function add()
• Step 3: Stop
sub function add()
• Step 1: Function start
• Step 2: Get a, b Values
• Step 3: add c=a+b
• Step 4: Print c Step 5: Return
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Advantages of flowchart:
1. Easy Communication: - Flowcharts are better way
of communicating the logic of a system to all
involved.
2. Effective analysis: - With the help of flowchart, problem
can be analyzed in more effective way.
3. Efficient Coding: - The flowcharts act as a guide or
blueprint during the systems analysis and program
development phase.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
4. Efficient Program Maintenance: - The maintenance of
operating program becomes easy with the help of flowchart.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Advantages of flowchart:
• It helps the programmer to put efforts more efficiently on
that part.
• Documentation: - Program flowcharts serve as a
good program documentation, which is needed for
various purposes.
• Debugging: - The flowchart helps in debugging process.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Disadvantages of flow chart:
• Complex logic: - Sometimes, the program logic is quite
complicated. In that case, flowchart becomes complex
and uncoordinated.
• Alterations and Modifications: - If alterations are required
the flowchart may require re-drawing completely.
• Reproduction: - As the flowchart symbols cannot be typed,
reproduction of flowchart becomes a problem.
• Cost: For large application the time and cost of
flowchart drawing becomes costly.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
PSEUDO
consists of short, readable and English languages used by an
algorithm.
does not include details like variable declaration, subroutines.
It is easier for the programmer or non programmer to
understand the general working of the program, because it is
not based on any programming language.
It gives us the sketch of the program before actual coding.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
PSEUDO
It is not a machine readable
Pseudo code can’t be compiled and executed.
There is no standard syntax for pseudo code.
Guidelines for writing pseudo code
• Write one statement per line
• Capitalize initial keyword
• Indent to hierarchy
• End multiline structure
• Keep statements language independent
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Syntax for if else:
IF (condition)THEN statement
... ELSE
statement
... ENDIF
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Advantages:
Pseudo is independent of any language; it can be used by most
programmers.
It is easy to translate pseudo code into a programming
language.
It can be easily modified as compared to flowchart.
Converting a pseudo code to programming language is very
easy as compared with converting a flowchart to
programming language.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Disadvantages
It does not provide visual representation of the program’s
logic.
There are no accepted standards for writing pseudo codes.
It cannot be compiled nor executed.
For a beginner, it is more difficult to follow the logic or write
pseudo code as compared to flowchart.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
COMPARE: ALGORITHM,
FLOWCHART AND PSEUDO CODE
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Interactive Mode:
• Direct Interaction:
• In interactive mode, users interact directly with the Python
interpreter, entering one command or a small block of code
at a time.
Script Mode:
File-Based Execution:
In script mode, Python code is written and saved in a text file
with a .py extension (e.g., my_script.py).
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Indentation:
• which statements belong to a particular code block, such as
the body of a function, a loop (e.g., for or while), a
conditional statement (e.g., if, elif, else), or a class definition.
• Syntax Requirement
• Code Block Definition
• Consistency
• Readability
• Convention
R. SUDHA, ASSISTANT PROFESSOR,
SRM
R. SUDHA, ASSISTANT PROFESSOR, SRM
TRP ENGINEERING COLLEGE, TRICHY
Comments
Single-line Comments
Multi-line Comments (using multiple single-line comments)
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Uses of
• Code Explanation:
To clarify complex logic, algorithms, or the purpose of specific
code sections.
• Documentation:
To describe the functionality of functions, classes, modules, and
methods (especially using docstrings).
• Debugging:
To temporarily disable sections of code during testing or
troubleshooting without deleting them.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Uses of
• Readability:
To make code more understandable for yourself and other
developers who might read or maintain it in the future.
• Collaboration:
To facilitate understanding and communication within a
development team.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Error
• Exceptions, provide information about issues
encountered during program execution
• crucial for debugging and identifying problems in code
• Syntax Error: Occurs when the Python interpreter
encounters invalid syntax (e.g., missing colons, unclosed
parentheses,
incorrect indentation)
• Indentation Error: A specific type of Syntax Error
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Error
• Name Error: Raised when a variable or function is used
before it has been defined.
• Type Error: inappropriate data type (e.g., trying to add a string
and an integer directly).
• Value Error: Raised when a function receives an argument
of the correct type but an inappropriate value (e.g., trying to
convert a non-numeric string to an integer).
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Error
• Python error messages, also known as exceptions,
provide information about issues encountered during
program execution. These messages are crucial for
debugging and identifying problems in code.
• Common Types of Python Errors:
• SyntaxError: Occurs when the Python interpreter
encounters invalid syntax (e.g., missing colons, unclosed
parentheses, incorrect indentation).
• IndentationError: A specific type of SyntaxError related to
incorrect or inconsistent indentation, which is significant
in Python.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Error
• Value Error: Raised when a function receives an argument
of the correct type but an inappropriate value (e.g., trying to
convert a non-numeric string to an integer).
• Index Error: Occurs when attempting to access an index
that is out of range for a sequence like a list or tuple.
• Key Error: Raised when trying to access a key in a
dictionary that does not exist.
• Attribute Error: Occurs when attempting to access
an attribute or method that does not exist for an
object.
• File Not Found Error: Raised when a program tries to open
a file that does not exist at the specified path.
R. SUDHA, ASSISTANT PROFESSOR,
number by SRM
Error
• Zero Division Error: Occurs when attempting to divide a
R. SUDHA, ASSISTANT PROFESSOR,
number by SRM
Built-in
Type Conversion:
• int(): Converts a value to an integer.
• float(): Converts a value to a floating-point number.
• str(): Converts a value to a string.
• list(), tuple(), dict(), set(): Convert or create
respective collection types.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in
• Mathematical Operations:
• abs(): Returns the absolute value of a number.
• sum(): Returns the sum of all items in an iterable.
• min(), max(): Return the minimum and maximum values
from an iterable.
• round(): Rounds a number to a specified number of decimal
places.
• pow(): Returns the result of a number raised to a power.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in
Iterables and Iterators:
• len(): Returns the length (number of items) of an object.
• range(): Generates a sequence of numbers.
• enumerate(): Adds a counter to an iterable.
• sorted(): Returns a new sorted list from an iterable.
• reversed(): Returns a reversed iterator.
• all(), any(): Check if all or any elements in an iterable are true.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in
Input/Output and System:
• print(): Prints objects to the console.
• input(): Takes input from the user.
• help(): Invokes the built-in help system for objects.
• type(): Returns the type of an object.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Variables
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Reserved
• Also nown as keywords, are special words that have
predefined meanings and functionalities within the
language. These words cannot be used as identifiers (e.g.,
variable names, function names, class names) because doing
so would conflict with their inherent purpose in Python's
syntax.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Reserved
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Common keywords used
in
• //: This keyword used to represent a comment.
• BEGIN, END: Begin is the first statement and end is the
last statement.
• INPUT, GET, READ: The keyword is used to inputting data.
• COMPUTE, CALCULATE: used for calculation of the result
of the given expression.
• ADD, SUBTRACT, INITIALIZE used for addition, subtraction and
initialization.
• OUTPUT, PRINT, DISPLAY: It is used to display the output of
the program.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Common keywords used
in
• IF, ELSE, ENDIF: used to make decision.
• WHILE, ENDWHILE: used for iterative statements.
• FOR, ENDFOR: Used for iterations (
incremented/decremented automatically)
Example: Greatest of two numbers
BEGIN READ a,b
IF (a>b) THEN
DISPLAY a is greater ELSE
DISPLAY b is greater END IF
END
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Primary Data Types
categorized into five types:
1) Integer type (int)
2) Floating point type (float)
3) Double precision floating point type (double)
4) Character type (char)
5) void type (void)
The words in parentheses indicate the keyword used in C to
indicate the data type.
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators
expressions
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators
1-Arithmetic Operators: expressions
2- Assignment Operators:
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators
3- expression
Comparison Operators:
4- Logical Operators:
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators
5- Bitwise Operators: expressions
6- Identity Operators:
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Arithmetic operators
expressions
7. Membership Operators:
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Built-in functions
• collection of pre-defined functions that are always available
for use without needing to import any modules. They provide
fundamental functionalities for common operations and tasks
within the language
Importing from Packages
• Allows for the organization and reuse of code across multiple
files and projects
R. SUDHA, ASSISTANT PROFESSOR,
SRM
Importing from Packages
Importing the entire package
entire package, making its modules and sub-packages accessible
using dot notation
(e.g., package_name.module_name.function())
R. SUDHA, ASSISTANT PROFESSOR,
SRM