Acc Notes
Acc Notes
l=[11,22,44,55,66,11,11,45,45,78,99,45]
l2=[]
for i in l:
if i not in l2:
l2.append(i)
print(l2)
Output :-
[11,22,44,55,66,45,78,99]
Output:-
hai elow
s='hai python'
d={}
for i in s:
if i not in d:
d[i]=1
else:
d[i]+=1
A=max(d.values())
for s in d:
if d[s]==A:
print(s)
Output:-
Output:-
hai
l=[11,22,33,44,11,55,66]
d={}
for i in l:
if i not in d:
d[i]=1
else:
d[i]+=1
A=max(d.values())
for s in d:
if d[s]==A:
print(s)
Output:-
11
Output:-
python
Another way
Output :-
python
Another way
s='hai hello python'
a=s.split()
print(max(a,key=len))
Output:-
python
n=int(input('enter a number'))
if n>1:
for i in range(2,n):
if n%i==0:
print('not prime')
break
else:
print('prime')
else:
print('not prime')
Output:-
Enter a number 11
prime
n=int(input('enter a number'))
a=2
if n>1:
while a<n:
if n%a==0:
print('not prime')
break
a+=1
else:
print('prime')
else:
print('not prime')
Output:-
Enter a number 5
prime
l=[11,22,33,44,55]
l[1],l[-1]=l[-1],l[1]
print(l)
Output:-
l=[11,22,33,44,55]
l[0],l[-1]=l[-1],l[0]
print(l)
Output:-
l=[11,22,33,44,55]
l[3],l[-5]=l[-5],l[3]
print(l)
Output:-
n=int(input('enter a number'))
fact=1
for i in range(1,n+1):
fact*=i
print(fact)
Output:-
Enter a number 5
120
n=int(input('enter a number'))
fact=1
a=1
while a<n+1:
fact*=a
a+=1
print(fact)
Output:-
Enter a number 5
120
def fibo(n,a,b):
if n==1:
print(a)
elif n==2:
print(a,b)
else:
print(a,b)
for i in range(2,n):
c=a+b
print(c)
a,b=b,c
fibo(10,2,3)
Output:-
23
5
8
13
21
34
55
89
144
n=int(input('enter a number'))
a=int(input('enter a number'))
b=int(input('enter a number'))
for i in range(2,n):
if n==1:
print(a)
elif n==2:
print(a,b)
else:
print(a,b)
c=a+b
print(c)
a,b=b,c
Output:-
enter a number10
enter a number2
enter a number3
23
5
35
8
58
13
8 13
21
13 21
34
21 34
55
34 55
89
55 89
144
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
num=5
for i in range(1,num+1):
x=fact(i)
print(x)
Output:-
1
2
6
24
120
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
num=5
x=fact(num)
print(x)
Output:-
120
def fibo(n):
if n==1 or n==2:
return n-1
else:
return fibo(n-1)+fibo(n-2)
num=10
for i in range(1,num+1):
x=fibo(i)
print(x)
Output:-
0
1
1
2
3
5
8
13
21
34
def fibo(n):
if n==1 or n==2:
return n-1
else:
return fibo(n-1)+fibo(n-2)
num=10
x=fibo(num)
print(x)
Output:-
34
Output:-
Another method
s='we are the programmers'
l=s.split( )
rev=l[::-1]
print(' '.join(rev))
Output:-
Output:-
n=int(input('enter a number'))
rev=0
num=n
while n>0:
rem=n%10
rev=rev*10+rem
n=n//10
if num==rev:
print('palindrome number')
else:
print('not palindrome number')
Output:-
Enter a number 11
palindrome
Another method
n=int(input('enter a number'))
l=str(n)
if l==l[::-1]:
print('palindrome number')
else:
print('not palindrome number')
Output:-
Enter a number 11
palindrome
s=input('enter a string')
rev=s[::-1]
if rev==s:
print('palindrome')
else:
print('not palindrome')
Output:-
enter a string malayalam
palindrome
l=[11,22,66,99,88,77]
max=l[0]
for i in l:
if i>max:
max=i
print(max)
Output:-
99
Another method
l=[11,88,55,98,78,54]
l.sort()
print(l[-1])
Output:-
98
l=[10,55,18,9,2,5,12]
n=len(l)
for i in range(0,n-1):
for j in range(0,n-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l[-1])
Output:-
55
l=[11,88,99,55,98,78,54]
l.sort()
print(l[-2])
Output:-
98
Another method
l=[11,88,99,55,98,78,54]
n=len(l)
for i in range(0,n-1):
for j in range(0,n-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l[-2])
Output:-
98
l=[11,88,99,55,98,78,54]
l.sort()
print(l[0])
Output:-
11
Another method
l=[11,22,66,99,88,77]
min=l[0]
for i in l:
if i<min:
min=i
print(min)
Output:-
11
l=[10,55,18,9,2,5,12]
n=len(l)
for i in range(0,n-1):
for j in range(0,n-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l)
Output:-
18. Write a program to swap first and last element in given list?
l=[11,22,66,99,88,77]
l[0],l[-1]=l[-1],l[0]
print(l)
Output:-
s='aaabbcccdaabbb'
new=''
c=1
for i in range(1,len(s)):
if s[i]==s[i-1]:
c+=1
else:
new=new+str(c)+s[i-1]
c=1
if i==len(s)-1:
new=new+str(c)+s[i-1]
print(new)
Output:-
3a2b3c1d2a3b
20. Input:- ['kumar', 'somu', 'sonu', 'ram', 'raju'], Output:- {'k': 1, 's': 2,
'r': 2}
Output:-
l=[10,55,18,9,2,5,12]
n=len(l)
for i in range(0,n-1):
for j in range(0,n-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l)
Output:-
Theory:
Or
Variable:-
Identifiers:-
● .py It is a file with .py extension, which stores the actual code
● .pyc It is a file with .pyc, which stores the compiled code or bytecode
5. What is the difference between list and tuple ?
3. List is a variable length data types whereas tuple is a fixed length data
type
4. We can initialize a single value in list (,) is not mandatory whereas we can
initialize a single in tuple (,) is mandatory.
5. For empty list we use square brackets where empty tuple we use tuple ()
(tuple function)
List Tuple
List is declared in a pair of square Tuple is declared in a pair of
brackets([ ]) parentheses or round brackets( ( ) ).
2. List is an ordered collection data type. So, we can perform indexing and
slicing whereas set is an unordered collection data type. So, we cannot
perform indexing and slicing
3. List allows duplicate values where as set is does not allows duplicate
values
4. For empty list we use square brackets whereas empty set we use set
function
List can store duplicate values Set cannot store duplicate values.
t=(10,20,30,40,50)
print(t)
l=list(t)
print(l)
l.append(80)
print(l)
print(tuple(l))
Output:-
We cannot add multiple values in tuple. By using type casting we can add
elements in tuple.
Example:-
t= (10,55,66,88,99)
t= list((10,55,6,88,99))
Example :-
l=[11,55,66,77]
l[0],l[-1]=l[-1],l[0]
Output:-
Pop Remove
Pop method is used for removing the Remove method is used for removing
element based on index position the element based on value
Pop method will return the value Remove method will not return the
value
Pop method will take one argument Remove method will take one
mandatory argument
Syntax for pop:- Syntax for remove:-
pop(index position) remove(value)
if condition:-
Ex:-
a=int(input('enter a value'))
if a>10:
print(a)
print('come out of the block')
Output:-
enter a value15
15
Here the a value is greater than 10.so,that it will enter into the if block and it
will execute the output
a=int(input('enter a value'))
if a>10:
print(a)
Output:-
enter a value5
Here the value (a) is not greater than 10. So,that it will not enter into the if block
If-else block:-
If block is not get executed then else block will get executed
Ex:-
a=int(input('enter a value'))
if a%2==0:
print('even number')
else:
print('odd number')
Output:-
enter a value6
even number
a=int(input('enter a value'))
if a%2==0:
print('even number')
else:
print('odd number')
Output:-
enter a value5
odd number
Here the given condition is not satisfied. So,that else block is executed
if-elif block:-
Ex:-
a=int(input('enter a value'))
b=int(input('enter b value'))
c=int(input('enter c block'))
if a>b:
print('a is maximum')
elif b>c:
print('b is maximum')
else:
print('c is maximum')
Output:-
enter a value10
enter b value15
enter c block25
c is maximum
Here the a,b values are not maximum.So, that else is executed
a=int(input('enter a value'))
b=int(input('enter b value'))
c=int(input('enter c block'))
if a>b:
print('a is maximum')
elif b>c:
print('b is maximum')
else:
print('c is maximum')
Output:-
enter a value10
enter b value36
enter c block25
b is maximum
a=int(input('enter a value'))
b=int(input('enter b value'))
c=int(input('enter c block'))
if a>b:
print('a is maximum')
elif b>c:
print('b is maximum')
else:
print('c is maximum')
Output:-
enter a value95
enter b value25
enter c block45
a is maximum
Note: If we write any condition, that should end with a block(:) after give one
tab indentation and then write the statements or code of a program.
19. Explain about break, continue and pass?
Break
It is used for terminating the loop
Ex:
for i in range(1,10):
print(i)
if i==5:
break
Output
12345
Continue
Continue is used for skipping the current iteration
Ex:
For i in range(1,11)
print(i)
if i==3:
continue
Output
1 2 4 5 6 7 8 9 10
Pass
Ex :-
For i in range(1,10)
pass
If a>10:
Pass
Pass
20. What is difference between for loop and while loop?
● Until and unless we call the function, the statements will not get executed
● We can call the function anyplace and anywhere check before it has been
declared or not
Types of functions
1. Built-in function
2. User defined function
Built-in function
These are already developed by the python developers
EX:-
Print, type, id, Len, input.
User defined function
User defined functions are defined by using def keyword
Advantages:-
1. Reusability
2. Debugging
Ex:-
def sum ( ): # functionname ex:- sum
print(‘hello’)
print(‘hai’)
sum( )
Output:-
hello
hai
Ex:-
def details(name,age):
print(f'Name = {name}')
print(f'Age ={age}')
details('hari',25)
Output:-
Name = hari
Age =25
22. What is difference between positional and keyword arguments?
Ex:- Ex:-
def add(a,b): def add(a,b):
print(a) print(b)
print(b) print(a)
add(10,50) add(10,50)
Output:- Output:-
10 50
50 10
Ex:- Ex:-
No.of arguments in function call less than or equal to the no.of arguments in
function declaration
Ex:-
def hai(a=44,b=88):
print(a)
print(b)
hai(10)
Output :-
10
88
Ex:-
def details(name='raju',age=20,mobile=9674124596):
print(f'Name = {name}')
print(f'Age ={age}')
print(f'Mobile ={mobile}')
details('anand',30,9673154863)
Output:-
Name = anand
Age =30
Mobile =9673154863
24. What is return keyword in python?
● Return is used for returning some values from function space to main
space
● In Return we any data type
● Return is the final block of statement
● After return we cannot write single line of statement
● In return if we give multiple elements the output format is in tuple
● We can call the return function in three ways
1. Assign the value to a variable.
2. Call the function inside a print function
3. We can call using conditional statements
Ex:-
def add(a,b):
print(a)
print(b)
return 50
a=add(10,20)
print(a)
Output:-
10
20
50
def add(a,b):
print(a)
print(b)
return 50
print(add(10,20))
Output:-
10
20
50
def add(a,b):
print(a)
print(b)
return 50,40,80
print(add(10,20))
Output:-
10
20
(50, 40, 80)
hello(10,50,66,88) hello(a=10,b='hello',c='hai')
Output:- Output:-
(10, 50, 66, 88) {'a': 10, 'b': 'hello', 'c': 'hai'}
26. What is recursion and advantages of using recursion?
Ex:-
def sum(n):
if n==0:
return 0
return n+sum(n-1)
print(sum(5))
Output:- 15
def fact(n):
if n==0:
return 0
return n*fact(n-1)
print(fact(5))
Output:-
120
27. What is lambda function?
(Optional)
Output:- print(list(filter(lambda
[22, 44, 176, 198, 154] n:n%2==0,[11,22,88,99,77])))
Output:-
[22, 88]
Global variables
global var1,var2,......varN
Ex:-
a,b=10,50
def hello( ):
global a,b
print(a,b)
a-=8
b+=5
print(a,b)
hello()
Output:-
10 50
2 55
Local variables
Ex:-
def hai( ):
a=10
print(a)
hai()
Output:-
10
Non-Local variables
non-local var1,var2,......varN
Ex:-
def add( ):
a,b=40,50
print(a,b)
def sum():
nonlocal a,b
print(a,b)
a+=100
b-=20
print(a,b)
sum()
print(a,b)
add()
Output:-
40 50
40 50
140 30
140 30
32.What is object?
Here the states and methods are considered as keys and their respective values
are considered as values
Class classname( ):
( ) are optional
Ex:-
class A:
x=10
y=20
def hai(self):
Code
Ex:-
class A:
x=10
y=20
def hai(self):
pass
obj=A( )
print(obj.x)
print(obj.y)
Output:-
10
20
● The members that are declared public can be accessible any part of the
program
2. Protected
3. Private
● The members that are declared private can be accessible within a class
● To indicate variable protected we use double underscore(__) before the
variable
Ex:-
class C:
def __init__(self):
self.x=10
self._y=20
self.__z=30
def get(self):
return self.__z
def set(self,m):
self.__z=m
obj=C()
print(obj.x)
print(obj._y)
print(obj.get())
obj.set(50)
print(obj.get())
Output:-
10
20
30
50
Note:-
Protected can be accessed from within the class,outside the class or within the
package.
39. Explain the difference between class method and static method?
It is used for binding the states and behaviours of an object under one roof
Types of inheritance
2.multiple inheritance
3.hierarical inheritance
4.multilevel inheritance
5.hybrid inheritance
It is process of inheriting the properties from single parent class to single child
class
Ex:
class A:
def __init__(self):
print('init of a')
class B(A):
pass
ob=B()
Output
init of a
2. Multiple inheritance
Ex:
class A:
def __init__(self):
print('init of a')
class B():
def __init__(self):
print('init of b')
class C(A,B):
pass
ob=C()
Output:
init of a
3. Hierarical inheritance
It is the process of inheriting the properties from single parent class to multiple
child class.
Ex:
class A:
def __init__(self):
print('init of a')
class B(A):
def __init__(self):
print('init of b')
class C(A):
pass
ob=C()
Output
init of a
4. Multilevel inheritance
It is a process of inheriting the properties from one child class to another child
class.
Ex:
class A:
def __init__(self):
print('init of a')
class B(A):
def __init__(self):
print('init of b')
class C(B):
pass
ob=C()
Output
init of b
5. Hybrid inheritance
def __init__(self):
print('init of a')
class B(A):
def __init__(self):
print('init of b')
class C(A):
def __init__(self):
print('init of c')
class D(B,C):
def __init__(self):
print('init of d')
obj=D()
Output:-
init of d
Syntax
44. How to implement parent class method along with child class method in
case of multilevel inheritance?
class A:
def __init__(self):
print('init of a')
class B(A):
def __init__(self):
print('init of b')
class C(B):
pass
ob=C()
Output
init of b
Method overriding
When we use method overriding the code redundancy is more using chaining
process along with method overriding we can achieve that.
Ex:-
Class BankV1( ) :
bank_name=’sbi’
bank_ifsc=1234
def __init__(self,n,a,bal):
self.name=n
self.age=a
self.balance=bal
Class Bank_v2( ) :
def __init__(self,n,a,bal,ac,mo):
self.name=n
self.age=a
self.balance=bal
self.account=ac
self.mobile=mo
Method overloading
Method overloading is not possible because it will take the latest defined
method
Ex:-
Class Bank( ) :
def __init__(self,n,a,bal):
self.name=n
self.age=a
self.balance=bal
def __init__(self,n,a,bal,ac,mo):
self.name=n
self.age=a
self.balance=bal
self.account=ac
self.mobile=mo