Getting started with python
Python is a high level language. It is a free and open source language.
• It is an interpreted language, as Python programs are executed by an
interpreter.
• Python programs are easy to understand as they have a clearly defined syntax
and relatively simple structure.
• Python is case-sensitive. For example, NUMBER and number are not same in
Python.
• Python is portable and platform independent, means it can run on various
operating systems and hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many popular web services and
applications are built using Python.
• Python uses indentation for blocks and nested blocks.
Token- Smallest individual unit of program is called token .Types of tokens
1)Python Keywords-Keywords are reserved words. Each keyword has a specific
meaning to the Python interpreter
2)identifiers are names used to identify a variable
3)Operators
4)Punctuators --- , {},()
5)Constants
---- There are several data types in Python — integer, boolean, float, complex,
string, list, tuple, sets, dictionary, None
----Comments are used to add a remark or a note in the source code. Comments
are not executed by interpreter.Two types
1)Single line - start with #
2) Multiline -dstart with """ .
---Python sequence is an ordered collection of items, where each item is indexed
by an integer.Example list,string ,tuple
--None is a special data type with a single value. It is used to signify the absence
of value in a situation. ex x=None
---- Mutable and Immutable Data Types Sometimes we may require to change or
update the values of certain variables used in a program. However, for certain
data types, Python does not allow us to change the values once a variable of
that type has been created and assigned values.
Variables whose values can be changed after they are created and assigned are
called mutable.Example list,integer
Variables whose values cannot be changed after they are created and assigned
are called immutable.Example String , Tuple
---- Explicit Conversion--Explicit conversion, also called type casting happens
when data type conversion takes place because the programmer forced it in the
program.Ex-
num1 = 10
num2 = 20
num3 = num1 + num2
print(num3)
print(type(num3))
num4 = float(num1 + num2)
print(num4)
print(type(num4))
--- Implicit Conversion Implicit conversion, also known as coercion, happens
when data type conversion is done automatically by Python and is not instructed
by the programmer.Ex-
num1 = 10 #num1 is an integer
num2 = 20.0 #num2 is a float
sum1 = num1 + num2 #sum1 is sum of a float
and an integer
print(sum1)
print(type(sum1))
Output:
30.0
<class 'float'>
-----The process of identifying and removing errors from a computer program is
called debugging.
Errors
occurring in programs can be categorised as:
i) Syntax errors- When rules of language are not followed .Comes at compile
time.
ii) Logical errors--A logical error produces an undesired output but without
abrupt termination of the execution of the program.Ex wrong formula.Comes at
run time.
iii) Runtime errors-A runtime error causes abnormal termination of program
while it is executing. Runtime error is when the statement is correct syntactically,
but the interpreter
cannot execute it. Runtime errors do not appear until after the program starts
running or executing. For example, we have a statement having division
operation in the program. By mistake, if the denominator
entered is zero then it will give a runtime error like “division by zero”.Comes at
run time.
----type is function used to show data type of variable
a=5
type(a)
Decision making
Algorithm-Step by step execution of program
While writing an algorithm, it is required to clearly identify the following:
• The input to be taken from the user
• Processing or computation to be performed to get
the desired result
• The output desired by the user
Example Algorithm to find square of a number.
Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square
Flowchart —
A flowchart is a visual representation of an algorithm. A flowchart is a diagram made up of boxes, diamonds
and other shapes, connected by arrows. Each shape represents a step of the solution process and the arrow
represents the order or link among the steps.
Flowchart of program to add two numbers
Flowchart of program to check number is odd or even
Pseudocode
A pseudocode (pronounced Soo-doh-kohd) is another way of representing an algorithm. It is considered as a
non-formal language that helps programmers to write algorithm.
so “pseudocode” means “not real code”. Following
are some of the frequently used keywords while writing
pseudocode:
• INPUT
• COMPUTE
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
Pseudocode for calculating area and perimeter of
a rectangle.
input length
input breadth
compute Area = length * breadth
print Area
compute Perim = 2 * (length + breadth)
print Perim
FLOW OF CONTROL
Types of statements-- Sequence ,selection,Iterarion
• The looping constructs while and for allow sections of code to be executed repeatedly
under some condition.
• for statement iterates over a range of values or a sequence.
Jump statements in python -Break ,continue,pass
Break Statement The break statement alters the normal flow of execution as it
terminates the current loop and resumes execution of the statement following
that loop.
Program to demonstrate use of break statement.
num = 0
for num in range(10):
num = num + 1
if num == 8:
break
print('Num has value ' + str(num))
print('Encountered break!! Out of loop')
Output:
Num has value 1
Num has value 2
Num has value 3
Num has value 4
Num has value 5
Num has value 6
Num has value 7
Encountered break!! Out of loop
Continue Statement ---When a continue statement is encountered, the control skips the
execution of remaining statements inside the body of the loop for the
current iteration and jumps to the beginning of the loop for the next iteration.
#Prints values from 0 to 6 except 3
num = 0
for num in range(6):
num = num + 1
if num == 3:
continue
print('Num has value ' + str(num))
print('End of loop')
Output:
Num has value 1
Num has value 2
Num has value 4
Num has value 5
Num has value 6
End of loop