Python Notes
Python Notes
Elements of Python
4. Python Manual : This component include various documents related to Python such as :
installation cum setup guide, tutorials, Python API , FAQs etc.
Python Interpreter
Python is a high level language. We know that computer understand machine language or binary
language which is also known as low level language. The job to translate programming code
written in high level language to low level language is done with the help of two type of
softwares : compilers and interpreters.
Working of Compiler
Compiler is a special type of software it first check the source code of the complete program for
the syntax errors, if no error found in the program code then the source code of the program is
converted to the machine code and finally the executive file of the program is created. The
executive file can be executed by the CPU and can produce desired result as per the program
requirements.
Working of Interpreter
Interpreter check the code of program line by line if no error is found it is converted into the
machine code and simultaneously gets executed by the CPU.
An interpreter is a computer program, which coverts each high-level program statement into the
machine code. This includes source code, pre-compiled code, and scripts. Both compiler and
interpreters do the same job which is converting higher level programming language to machine
code. However, a compiler will convert the code into machine code (create an exe) before
program run. Interpreters convert code into machine code when the program is run.
Atoms
Atoms are the most basic elements of expressions. The simplest atoms are identifiers or literals.
Forms enclosed in reverse quotes or in parentheses, brackets or braces are also categorized
syntactically as atoms. The syntax for atoms is:
Indentation
Indentation in Python refers to the (spaces and tabs) that are used at the beginning of a statement. The
statements with the same indentation belong to the same group.
For Example
a=2
print(a)
if a==3 :
print(“hello world”)
print(“HELLO WORLD”)
else :
print(“ bye world”)
print(“ BYE WORLD”)
Import statements
// import statements are used to include library files to the python program
Function definitions
//This section include the definitions of various functions written in a Python Program
Program statements
// This section include the set of statements for solving the given problem.
Identifier
Identifier is a name given to various programming elements such as a variable, function, class,
module or any other object. Following are the rules to create an identifier.
1. The allowed characters are a-z, A-Z, 0-9 and underscore (_)
2. It should begin with an alphabet or underscore
3. It should not be a keyword
4. It is case sensitive
5. No blank spaces are allowed.
6. It can be of any size
Valid identifiers examples : si, rate_of_interest, student1, ageStudent
Invalid identifier examples : rate of interest, 1student, @age
Keywords
Keywords are the identifiers which have a specific meaning in python, there are 33 keywords in
python. These may vary from version to version.
B. A type or datatype which specify the nature of variable (what type of values can be stored in
that variable and what operations can be performed)
We can check the type of the variable by using type command
>>> type(variable_name)
C. A value
1.1 Integers are the whole numbers consisting of + or – sign like 100000, -99, 0, 17. While
writing a large integer value
e.g. age=19
salary=20000
1.2 Floating Point: Numbers with fractions or decimal point are called floating point numbers.
A floating point number will consist of sign (+,-) and a decimal sign(.)
1.3 Complex: Complex number is made up of two floating point values, one each for real and
imaginary part. For accessing different parts of a variable x we will use x.real and x.imag.
Imaginary part of the number is represented by j instead of i, so 1+0j denotes zero imaginary
part.
Example
>>> x = 1+0j
>>> print x.real,x.imag
1.0 0.0
2. None
This is special data type with single value. It is used to signify the absence of value/false in a
situation. It is represented by None.
3. Sequence
A sequence is an ordered collection of items, indexed by positive integers. Three types of
sequence data type available in Python are Strings, Lists & Tuples.
3.1 String: is an ordered sequence of letters/characters. They are enclosed in single quotes (‘’) or
double (“”).
Example
>>> a = 'Ram'
>>>a=”Ram”
3.2 Lists: List is also a sequence of values of any type. Values in the list are called elements /
items. The items of list are accessed with the help of index (index start from 0). List is enclosed
in square brackets.
Example
Student = [“Ajay”, 567, “CS”]
3.3 Tuples: Tuples are a sequence of values of any type, and are indexed by integers. They are
immutable i.e. we cannot change the value of items of tuples . Tuples are enclosed in ().
Student = (“Ajay”, 567, “CS”)
4. Sets
Set is an unordered collection of values, of any type, with no duplicate entry. Sets are immutable.
Example
s = set ([1,2,3,4])
5. Dictionaries: Dictionaries store a key – value pairs, which are accessed using key. Dictionary
is enclosed in curly brackets.
Example
d = {1:'a', 2:'b', 3:'c'}
Literals
A literal is a constant that appear directly in a program and this value does not change during the
program execution i.e. remain fixed.
Example:
Num1=5
Principle_amount= 5000.00
Here 5 and 5000.00 are literals. Python support the following literals
Numeric literals
String Literals
Special Literals
Collection Literals
Membership Operator
Assignment Operators
Assignment operators are used to assign a value to the variable.
Identity Operator
Operator Precedence :
Control statements
Control structureis used to control the sequence of execution of statements written in a program.
Without using Control structure sequence of execution of statements in a program is line line by
and every statement is executed once .
ii) if.... else: In this control structure for true and false result of condition different-
different set of statements will execute.
flowchart syntax
if (condition) :
Statement 1
eFals eTru Statement 2
onconditi …………
Statement n
statements sstatement else :
Exit Statement A
Statement Z
…………
Statement N
iii) if.......elif....... else: This control structure is used in situations where on getting false
result of condition we want to check if some other condition is meeting. We can
increase the levels of elif according to the salutation of given problem. This control
structure is also called laddered elif.
flowchart syntax
if (condition) :
Statement 1
true
Statement 2
false
condition …………
false true statements
elif (condition) :
condition
Statement i
true
condition statements Statement ii
…………
statements
elif (condition) :
Statement a
Statement b
…………
else
Statement A
Statement B
…………
Looping Control Structure : This control structure is used to execute a set of statements multiple
times on the basis of condition. Loop is controlled by the loop counter. We need to initialize,
update and use the counter in loop termination condition.
Syntax 2 :
while (condition):
statements to be executed till the loop condition
is true
else :
statements to be executed when the loop
condition become false
FOR loop : For loops are used for sequential traversal. This loop is
used when you know in advance that how many times the loop will
execute.
Syntax:
for counter in sequence:
statements(s)
Example
Program to find the sum of all numbers stored in a list
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)
Break Statement
Break statement is used to force fully terminate a loop. The use of this statement can be easily
understood with the help of following example.
Write a program to demonstrate the use of break
a=1
while a<=10:
print(a)
a=a+1
if a==5:
break
output: 1,2,3,4
Continue Statement
Continue statement is used to skip the execution of loop statements. The use of this statement
can be easily understood with the help of following example.
Write a program to demonstrate the use of continue
a=1
while a<=10:
if a==5:
a=a+1
continue
print(a)
a=a+1
output: 1,2,3,4,6,7,8,9,10
Pass Statement
Pass statement is used in situations where there are no statements for execution in a particular
section of program but for following syntax rules the inclusion of section is must. The use of this
statement can be easily understood with the help of following example.
output: 1,2,3,4,6,7,8,9
Computer Programs
Formula Based Simple Programs
1. Write a program to check whether a person is eligible for voting or note by checking his
age.
3. Write a program to find the grade of a student based upon his percentage based upon following
conditions.
Percentage >= 80 A+ grade
Percentage>= 60 A grade
Percentage >= 50 B grade
Percentage >=40 C grade
Percentage>=35 D grade
Percentage>=0 F grade
Other : invalid percentage entered
num=1
sum1=0
while num<=100 :
sum1=sum1+num
num=num+1
print(“Sum from 1 to 100 = ” sum1)
num=1
product=1
while num<=100 :
product=product*num
num=num+1
print(“ Product from 1 to 100 = ” product)
Q8. Write a program to check whether a number is palindrome (same as its reverse) or not
num=int(input("Enter a number "))
num1=0
num2=num
while num2>0:
num1=num1*10
num1=num1+(num2%10)
num2=num2//10
if num1==num:
print(num ," is a palindrome")
else:
print(num, " is not a palindrome")