A computer is just a machine that gives the result based on the
instructions or inputs given to it. These step by step instructions
written in any programming language to do a specific task is known
as program. Algorithms and flowchart are powerful tools for learning
programming.
Steps involved in computer problem solving
1. Understanding the problem
2. Analysing the problem
3. Developing the solution
4. Coding and implementation
Control structure
Sequential flow—simple statements
Selection flow—branching control –if condition
Repetition flow—loops
Advantage of an algorithm
It is a step by step representation of a solution to a given problem.
It uses simple English like statement
Problem is broken down into smaller pices.
Disadvantage of an algorithm
There is no standard rule for writing an algorithm, it varies drastically.
The process of creating an algorithm can be time consuming.
Difficult to represent branching and looping in an algorithm.
Flowchart
Drawing a flow chart
Programming language
It is a formal language that specifies a set of instructions that can be
used to produce various kind of output. Eg: c, c++, java, ruby, python
What is program
What is python?
Python is a general purpose, object oriented easy to learn and high
level programming language. Python as a language supports simple
English like statements, making it easy for programmers to learn and
maintain.
Features of python
High level language
Easy to learn and use
Expressive language
Free and Open source
Cross platform language
Interpreted language
Object oriented language
Supports GUI
Large standard library
Portability and compatibility
Database and scalable
Role of python
Built in libraries
Platform independence
extensible
Availability of data Analysis Tools
Community Support
Application of python
Used for web and internet application development
Scientific and Numeric Calculations
Game development
General purpose s/w development
Database access
Business application
Working in python
Interactive mode(IDLE shell mode):- Command prompt >>>, you
can’t save the program
Script mode:- we can save all commands together with extension .py.
to run a file we can use the shortcut command F5 or click on the RUN
menu and click on Run module.
Python character set
Valid character for python
Digits llike 0, 1,2,3………
Letters in upper or lowercase like A-Z, a-z
Special symbols like $ % ^ & * () @ ! etc
Whitespaces like spacebar, tab key or enter key
ASCII, UNICODE characters like emoji symbol or other symbol
Statements in python
Simple statement :- a=5
Multiline statements:- a=4+5+\
6+7+\
8+9
Multiple statements:-a-4; b=6; c=a+b; print(c)
Tokens
Keyword
Identifier
Literals
Punctuators
Operators:-
arithmetic operator:- + - * / % ** //
Relational operator:- ==, !=, >,<,>=,<=
Logical operator:- AND, OR, NOT
Assignment operator :- =
Augmented assignment operator:- +=,-+,*=,/=,//=,
%=,**=
Operator precedence: BODMAS, AND,OR,NOT
Variables:- name :- case sensitive
A=5
a=5
Data Types:-
Number:- int, float, complex
None
Boolean
Sequences:- string, list , tuple
Sets
Maps:- dictionary
Data type conversion:-
implicit type conversion:- when python encounter mixed data
types and convert automatically.
Eg a=5
b=3.8
c=a+b
print (c)
8.8
Type(c)
float
Explicit type conversion:- explicit type conversion, also known as
type casting,using pre defined functions to convert one data type to
another.
int() eg:- int(15.5)
15
float() eg:- float(12)
12.0
str() eg str(12)
‘12’
bool() eg:- bool(0)
false
Comments in python:-
Single line comment:- start with #
Eg:-#assigning a value to a variable
Num1=10
Multiple line comments:- start with 3 single quotes’’’ or 3 double
quotes”””
Eg:- “””this program calculate
The average”””
The print ():- It is used to print the output on the
screen.
The input function():- it is used to take the input
from the user.
Errors in python:- syntax error:- syntax refers to the rule in
python.
a+b=c
cannot assign to operator
Logical error:- hard to find this error because program will run
correctly but the desired output is not achieved.
P=m1 + m2 / 2 instead of p=(m1+m20)/2
Runtime error:- it occur during the execution of a program.
A=5
B=0
C=a/b
ZeroDivisionError: division by zero