Vk's Notebook python

Welcome To my Blog !

Hello Venkatesh!

#python program to reverse a number in list with user input.


x =[]

n = int(input("Enter the number:"))

for i in range(0,n):

    a = int(input())

    x.append(a)


# reversing the number in list

x.reverse()

print(x)


Generators:

we use generators alo iterator to check one by one values.

iterator is a more general concept: any object whose class has a __next__ method (next in Python 2) and an __iter__ method that does return self.

Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions (yield statements, in Python 2.5 and earlier), and is an object that meets the previous paragraph's definition of an iterator.

Method Overloading:

It means two or more function having same name but different number of pararmeters.

not likeliy supported in python!


Method overiding:

It means two method having same name & parameter but each method are in different class.

Constructors are used to intialize the object , they dont return unlike normal methods,constructor is called automatically once obj created.

Abstract class contains atleast one Abstract method where can can't create object.

self refers to the current object that is passed to a method present inside a class.

cls refers to the corresponding class .

Exception handling in python!

a = 5

b = 0

try:

    print(a/b)   #try block hold critical statement

except Exception as e :    

    print("Divide by zero error")   #exception block is executed when any error occurs otherwise not.

finally:

    print("Finally block will print whether exception occurs or not")


Three types of errors:

1. compile time error

2. runtime error

3. logical error

python is a  compiled and interpreted language. compiler scans the whole code while interpreter scans line by line.

# PYTHON PROGRAM FOR LINEAR SEARCH


pos = -1 # position of element to be searched

n = int(input("Enter the size for list"))

listed = []
for i in range(0, n):
b = int(input())
listed.append(b)

# print(listed)

x = int(input("Enter the element to be searched"))


def search(listed, x): #function to return the position and element
j = 0
while j < n:
if listed[j] == x:
globals()['pos'] = j
return True
j += 1

return False


if search(listed, x):
print("element found", pos+1)

else:
print("not found")


Binary search:

# PYTHON PROGRAM FOR Binary SEARCH
# List must be sorted in binary search
# s is the number to be searched
# search function performs the binary search
# l represents lower bound & u represents upper bound
# l =0 and u has last value
# mid value is calculated using integer division by averaging l & u
# if mid value is found it is returned
# if not then midvalue is checked against lower and upper bound
# if midvalue is less than s : l = s
# if midvalue is greater than s : u = s


pos = -1

def search(list,n):

l = 0
u = len(list) - 1

while l < u:
mid = (l+u) // 2

if list[mid] == s:
globals()['pos'] = mid
return True
else:
if list[mid] < s:
l = s
else:
u = s

list = [1,2,3,4,5,6,7,8,9,11,22,44,66,77,89]

s = 8

if search(list,s):
print("Element found",pos+1)
else:
print("Element not found")





BUBBLE SORT:    


# program for bubble sort
# Bubble sort algorithm works by comparing the neighbour element and swapping takes place.
# we use two for loop 1. outer loop for number of iteration 2. for swapping element
# we use temp value to swap the values





def sort(nums):
for i in range(len(nums)-1, 0, -1): # outer loop
for j in range(i): # inner loop
if nums[j] > nums[j + 1]:
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp



nums = [4,5,3,7,8,2]
sort(nums)

print(nums)



                                                                           

Comments