Class 11
Class 11
feb 1991.
it is influenced by two programming language
ABC language
Modula-3
Adv
1. Easy to use and compact and very simply syntax rules.
2. it is more capable to expressing code's purpose than many other language.
3.python is an interpreted language so it is easy to debug.
4. python come with everything we need to do real work.
5. python is a cross platform it can run on windows, linux, smart phones etc.
6. python language is freely available without any cost and its source code is also
available.
7. python is being used in many fields.
disadvantages of python.
1. it is not the fastest language.
2. lesser liabraries than c,java etc.
3. python interpreter is not very strong on catching type mismatch issues.
4. the translation from python to another language in not easy to convert.
interpreter
this language processor convets a HLL program into machine language by converting
and executing it line by line.
compiler
it also converts the HLL program into machine language in one go.
working in python
we can work in python in two ways.
1. in interactive mode(immediate mode)-in iteractive mode instructions are given in
front of python prompt in python shell. Python carries out the given instruction
and shows the result there itself.
2. Script mode- in this mode , python instruction are stored in a file generally
with .py extention and are executed together in one go as a unit. The saved
instruction are known as python script or python program.
character set- set of valid character that a language can recognize . python
supports unicode character set.
identifiers- fundamental building blocks of a program and are used for the name
given to different parts of the program like variable , objects, functions, lists,
dictionaries etc.
operator- are token that trigger some computation / action when applied to
variables and other objects in an expression.
Arithmetic, relational, logical operator.
comments
are the additional readable information, which is read by the programmers but
ignored by python interpreter.
In python comments begin with symbol # and end with physical line.
1. single line -
2. multiline
i)add a # symbol in the beginning of every physical line part of the mulitiline
comment.
ii)type comment as a triple quoted multiline string.
3. those comment which start after python instruction are called inline comment.
variables
A variable in python represents named location that refers to a value and whose
values can be used and precessed during program run.
creating a variable
In python , to create a variable, just assign to its name the value of appropriate
type.
Lvalues- These are the objects to which you can assign a value or expression
Rvalues- These are the literals or expressions that assigned to Lvalues.
Multiple assigment
1. we can assign same value to multiple variables in a sigle statement
a=b=c=10
2. Assigning multiple value to multiple variable.
x,y,z=10,20,30
it will assign the values order wise, i.e first variable is given first value,
second variable the second value and so on.
Variable defination
a variable is definde/created only when we assign some value to it. Using an
undefined variable in an expression /statement cause an error called name error.
Dynamic Typing
A variable pointing to a value of a certain type,can be made point to a
value/object of different type .This is called dynamic typing.
a=20
a="agcs"
type of operator
1. unary operator
those operators that require one operand to operate upon.
+,-
a=2,
a=-4
2.Binary operator
Those operators that require two operands to operate upon
Arithmetic operator
+,-,*,/,**(Exponent),%(modulus),//floor division
Relational operator
>,<,>=,<=,==,!=
it returns True,False.
Assignment operators
=,/=,+=,*=,%=,-=,**=,//=
a=a+5 a+=5
a=a+1 a+=1
a=a+b-c a+=b-c
Logical operator
and
or
not
t f f t
f t f t
t t t t
f f f f
not
t f
f t
print()
Reading Numbers
python offers two function int() and float() to used with input() to convert the
values received through input() into int and float types.
a=int(input("enter any number"))
print() function
is a way to send output to standard output device normally in a monitor.
print(a)
print('a')
print('the sum is',c)
print("agcs","purulia",sep="@@@@")
print("agcs",end="%")
write a program to enter a number and find the square and cube.
s=a*a
c=a*a*a
write a program to enter two number and find the quotient and remainder.
a=int(inpu%t("enter the 1st number"))
b=int(input("enter the 2nd number"))
q=a/b
r=a%b
print("the quotient is ",q)
print("The remainder is",r)
DataType
To represent various types of date, programming language provides ways and
facilities to handles them, which are known as datatype
boolean
These represent the values False and True. The booleam type is a subtype of plain
integers and boolean values False and True behave like the value 0 and 1
respectively.
complex number
python represent number in the form A+Bj
python use j to represent imaginary numbers. Composite number make of two parts the
real part and the imaginary part.
a=0+4.5j
a.real 0
a.imag 4.5
String
A string data type lest us hold string data that is any number of valid character
int a set of quotation marks.
example a="agcs"
list
A list in python represents a list of comma separated values of any datatype
between square brackets and it is mutable(change value in place).
example
a=[10,30,'agcs',12.5]
to access
a[valid index]
tuples
are represented as list of comma-seperated values of any data type within
parenthesis and it is immutable.
example
a=(1,2,4,5)
dictionary
is an unordered set of comma seperated key:value pairs, withen { } no two keys can
be same and it is mutable
a={'a':1,'b':2,'c':3}
mutable types
are those whose value can be changed in place
mutable types-list,dictionaries,sets
immutable type- immutable means unchangeable. in python, immutable types are those
whose value cannot be changed in place.
immutable types - integer, floating, booleans, string , tuples.
operators and operands
the symbols that trigger the operation on data are called operator. and the data on
which operation is being carried out .i.e the object of the operation are reffered
to as operand.
operator precedence
()
** RTL
+a,-a
*,/,//,% LTR
+,-
<,<=,>,>=
is, is not
not
and
or
2**(2**2)
=2**4
16
Atom
an atom is something that has a value.Identifiers,literals, strings, lists, tuple ,
sets dictionary etc are all atoms.
expression
An expressing in python is any valid combination of operetors and atoms. An
expression is composed of one or more operation, arithmetic , relational ,logical ,
string
type of statements
1. empty statement - A statement which does nothing .
pass
2) Simple statement - any single executable statement
a=input("enter your name")
iteration(looping)
the iteration means repetition of a set of statement depending upon a codition
test.
example - for , while
if statement
An if Statement tests a particular condition.If the condition evalautes to true , a
course of action is followed otherwise the course of action is ignored.
if else statement
if statement tests a condition and if the condition evalutes to true, it carries
out statements indented below 'if' and in case condition evalutes to false it
carries out statements indented below else
write a program to find a number is single digit , double digit number ,three
digit number or not.
a=int(input("enter any number"))
if a>=0 and a<10:
print("single digit")
if a>=10 and a<100:
print("double digit")
if a>=100 and a<1000:
print("three digit")
write a program to find the sum of two numbers are odd or even.
a=int(input("enter the 1st number"))
b=int(input("enter the 2nd number"))
c=a+b
if c%2==0:
print("even")
else:
print("odd")
write a program to enter two numbers,if the 1st number is greater than find double
of both the numbers.or if 2nd number is greater the find the square of both the
numbers.
conditional loop- the loop keep repeating as long as some condition is true
while loop is a conditional loop.
for loop- is designed to process the items of any sequence . such as list or
string one by one.
for i in range(1,11):
print(i)
write a program to print even number from 2 to 20.
for i in range(2,21,2):
print(i)
Write a program to print the sum of 1 to 10.
s=0
for i in range(1,11):
s=s+i
print("the sum is",s)
i s=s+i
1 s=0+1=1
2 s=1+2=3
3 s=3+3=6
4 s=6+4=10
write a program to print the table of 5 till 10 terms.
for i in range(1,11):
print("5X",i,"=",i*5)
list
creating lists
to create a list , put a number of expression in square brackets and separate the
items by commas.
example
to create a empty list
a=list()
nested list
a=[2,3,[4,5],9]
creating list from existing sequences.
l=list('agcs')
enter the value of the list during run time
a=list(input("enter the list"))
b=eval(input("enter the value of the list"))
eval() function
this function of python is used to evalute and return the result of an expression
given as string.
traversing a list
traversal of a sequence means accessing and processing each element of list it can
be easily done using for loop.
a=['a',1,10.4,30]
for i in a:
print(i)
or
a=[1,2,3,4,6,7]
b=len(a)
for i in range(b):
print(a[i])
comparing list
we can compair two list using comparison operator <,>,==,!= etc and the two
sequence must be of comparable type of values.
example
a=[1,2,3]
b=[2,3,4]
a==b
list operation
joining - the concatenation operator + when used with two lists it will join them.
repeating of list
like string we can use * operator to replicate a list specified number of time.
exam
a*2
updating element
to update or change an element on the list in palce we have to assign new value to
the elements index.
a=[10,20,30,40]
a[0]=24
[24,20,30,40]
deleting elements
the del statement can be used to remove an individual item or to remove all items
or by slice.
del statement without index it will delete all element
example
del a[1]
del a[0:3]
pop() method remove single element not list slice. The pop() method remove
individual item and return it. if no index is specified pop() remove and return the
last item in the list.
example
a.pop(index)
index method
This function return the index of first matched item from the list.
example
a.index(3)
append() method
to add item to an existing sequence append() method add a single item to the end of
the list and it does not return the new list
extend method
is used for adding multiple element to a list
insert method
to insert an element somewhere in between or any position.
example
a.insert(2,'a')
remove() method
remove the first occurrence of given item from the list . it will report an error
if there is no such item in the list.
a.remove('a')
clear method
removes all the items from the list and the list becomes empty
example
a.clear()
count method
this function return the count of the item that is passed as argument . if the
given item is not in the list it return zero
reverse method
reverses the items of the list . This is done inplace it does not create a new
list.
example
a.reverse()
sort method
the sort() method sorts the items of the list by default in increasing order.
a.sort()
decreasing order
a.sort(reverse=True)
creating a dictionary
to create a dictionary we need to include the key:values pairs in curly braces
example
d={'a':'apple','b':'ball','c':'cat'}
note - keys immutable
traversing a dictionary
The for loop is use to traverse or loop over the items in a dictionary
example
for i in d:
print(d[i])
characterstics of a dictionary
1) A dictionary is a unordered set of key:value pairs
2) Dictionary is not a sequence because it is unordered set of elements.
3)Each key within a dictionary must be unique.
4)Dictionary are indexed by keys.
5) Dictionaries is mutable we can change the value of certain key in place.
6)The key:value pairs of a dictionary are associated with one another with some
internd function.This way of linking is called mapping.
split() function
the split() work on string type data and break up a string into words and creates a
list.
by default it break up the text based on white space.
a="The Assembly of God Church"
b=a.split()
len() method
this method return length of the dictionary
i.e it count the element (key:value pairs) in the dictionary
example
c={'a':'apple','b':'ball'}
>>> len(c)
clear() method
this method removes all items from the dictionary and the dictionary become empty
c.clear()
get() method
with this method we can get the item with the given key. If the key is not present
it will display error.
example
d.get('d','not found')
item() method
this method return all of the items in the dictionary as a sequence of tuples
keys() method
this method return all of the keys in the dictionary as a sequence od key in list
form
values () method
this method returns all the values from the dictionary as a sequence in list form
ex
c.values()
update() method
this method merges key:values pairs from the new dictionary into the original
dictionary.
a={'a':'apple','b':'ball','c':'cat'}
b={'a':'ant','d':'delhi','c':'computer'}
a.update(b)