0% found this document useful (0 votes)
12 views14 pages

Class 11

Computer

Uploaded by

ratnadipkundu33
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
12 views14 pages

Class 11

Computer

Uploaded by

ratnadipkundu33
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 14

python is an object oriented programming language developed by Guido Van Rossum in

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.

token- the smallest individual unit in a program is known as a token or a lexical


unit.
python has following token
1. keywords
2. indentifiers
3. literals
4. operators
5. punctuators

keywords-are the words that convey a special meaning to the language


compiler/interpreter. These are reserved for special purpose and must not be used
as normal identifier names
example- if,False,while,class for etc

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.

rules for naming identifier


1. the first character must be a letter, underscore.
2. upper and lower case letter are different
3. an identifier must not be a keyword of python

Literals-Literals are date items that have fixed value.referred to as constant


values.
1. String literals
single line string example-"agcs"
multiline string example-"agcs\purulia"
2. Numeric literals. int ex=10,999,float 9.40,977.9
3. Boolean literals True,False
4.special literal None
5.Literal collection.

operator- are token that trigger some computation / action when applied to
variables and other objects in an expression.
Arithmetic, relational, logical operator.

punctuators- are symbols that are used in programming languages to organize


sentence structure.
',",#,\,(),[],{},@, ,,:,.,=

expression- An expression is any legal combination of symbols that represents a


value.
a+b,a>b, 15,2.8

Statement - is a programming instruction that does something or take some action.


a=15
c=a-b
print(a+3)
if b>5:

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

input and output


To get input from user interactively we can use use built in function input().it
return a value of string type.
a=input("enter your name")

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 programm to find the area of a triangle.


area=1/2*base*height

b=int(input("enter the base"))


h=int(input("enter the height"))
a=0.5*b*h
print("the area is:",a)

write a program to enter a number and find the square and cube.
s=a*a
c=a*a*a

a=int(input("enter any number"))


s=a**2
c=a**3
print("the square is",s)
print("the cube is",c)

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)

write a program to find s.i.


si=(p*r*t)/100

DataType
To represent various types of date, programming language provides ways and
facilities to handles them, which are known as datatype

core data types are


numbers(integers, floating-point, complex number, booleans)
String
list
tuple
dictionary

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.

floating point numbers.


A number having fractional part is a floating point number.
The fractional number can be written in two forms
1. fractional form- 35.05
2. Exponent Notation - 5e01

adv over integer


1. they can represent greater range of values
dis
1. floating point operation are useallly slower then integer operation.

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"

String as a sequence of characters


String is a sequence of characters and each character can be individually accessed
using it s two way index.String are immutable(we can't change value in place).
a='Assembly'
>>>a[0]

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

compound expression- it involes multiple types of operator


example- a+b>c**d

implicit type conversion(coercien)- is a conversion performed by the compiler


without programmer's intervention.
4+5.5= 9.5

if either argument is a complex number, the other is converted to complex

otherwise if either argument is a floating point number the other is converted to


floating point.

no conversion if both operands are integers.

Explicit type conversion


is user defined conversion that forces an expression to be of specific type .The
explicit type conversion is also known as type casting.
int()
float()
complex()
str()

write a program to swap two numbers.

write a program to enter two number and find the addition,sub,divi,multi


a=int(input("enter the 1st number"))
b=int(input("enter the 2nd number"))
c,s,m,d=a+b,a-b,a*b,a/b
print(c)
print(s)
print(m)
print(d)

type of statements
1. empty statement - A statement which does nothing .
pass
2) Simple statement - any single executable statement
a=input("enter your name")

3) compound statement - represents a group of statement executed as a unit. A


compound statement has header ending with colon and a body containing a sequence of
statement.
if a>b:
print(a)
print('agcs')
print('school')
else:
print('purulia')

statement flow control


sequence- are being executed sequentially (default)

selection- means the execution of statement depending upon a condition test. if a


condition evalutes to True a couse of action is followed otherwise another course
of action is followed.
example if

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 the greatest to two numbers.


a=int(input("enter the 1st number"))
b=int(input("enter the 2nd number"))
if a>b:
print("the greater number is ",a)
else:
print("the greater number is ",b)

write a program to find a number is odd or even.


a=int(input("enter a number"))
if a%2==0:
print("even")
else:
print("odd")

Write a program to find a number divisible by 5 or not.


n=int(input("enter a number"))
if n%5==0:
print("divisible")
else:
print("not divisible")

write a program to find a number is divisible by 5 and ends with 5 or not


n=int(input("enter any number"))
if n%5==0 and n%10==5:
print('yes')
else:
print('no')

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.

a=int(input("enter the 1st number"))


b=int(input("enter the 2nd number"))
if a>b:
r1=a*2
r2=b*2
else:
r1=a**2
r2=b**2
print(r1)
print(r2)

two categories of loop


counting loops- the loops that repeat a certain number of times.
for loop is a counting loop

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"))

list and strings similarity


lists are sequences just like strings. List also index their individual elements
just like strings.

list and string difference


1. list are mutable sequences while strings are immutable
2. String store single type of elements all characters while list can store
elements belonging to differents type.

eval() function
this function of python is used to evalute and return the result of an expression
given as string.

accessing individual element


through index number we can access the element.
a[1]

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

slicing the lists


list slices are the sub part of list extracted out.
exam
a[1:4:2]

modification using slice


slice can be used to overwrite one or more list elements with one or more other
elements.
a=[1,2,3]
a[0:2]='a'

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')

difference between pop and remove method


the pop method remove an individual item and return it.while remove searches for an
item and remove the first matching item from the list.

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)

dictionaries are mutable,unordered collection with elements in the form of


key:values pairs that associate keys to values. Dictionaries are also called
associative arrays or mapping or hashes.

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

accessing element of a dictionary


in dictionary the element are accessed through the keys defined in key:value pair
example
d['a']
In dictionary operation that takes a key and finds the corresponding value is
called lookup.

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.

multiple ways of creating dictionaries.


1) c={'a':'apple','b':'ball'}
2) create empty dictionary c={}, c=dict()
3) using dict() constructor to create new dictionary
example
c=dict(a='apple',b='ball')
d1=dict({'a': 'apple', 'b': 'ball'})
d2=dict(zip(('a','b'),('apple','ball')))
in the form of sequence
d3=dict([['a','apple'],['b','ball']])

Adding elements of dictionary


To add new elements to a dictionary type a new key with its values
d['c']='cat'

update element in a dictionary


to change value on an existing key useing assignment operator
d['c']='computer'

deleting element from dictionary


The two way for deleting element
1) by using del command
ex- del d['c']
2) delete element from dictionary by using pop() method
ex- d.pop('c','not found')

in and not in operator


the in and not in check fro existence of keys
example
'a' in c- True

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()

difference between del and clear


the clear() removes all the elements of a dictionary and makes it empty dictionary
while del statement removes the complete dictionary as an objects.

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)

You might also like