Python Practicle
Python Practicle
Python Practicle
IT Workshop(Python)
if 98 < 100:
print("hlo")
Output:
Code: Corrected
if 98 < 100:
print("hlo")
Output:
1.INTEGER
Whole number from -infinity to +infinity are integer numbers. For
example: 45, -90, 89, 1171 are integer numbers.
Code:
a = 100
print(type(a))
Output:
2.FLOAT
In python programming, float data type is used to represent floating
point numbers. Example of floating-point numbers are: -17.23,
78.99, 99.0 etc.
Code:
a = 17.34
print(type(a))
Output:
3.BOOLEAN
In python programming, Boolean data types is used to represent
logical
True and False
Code:
a = True
print(type(a))
Output:
4.STRING
In python programming, string data types is used to represent
collection of characters. Characters can be any alphabets, digits and
special characters. Example of strings are 'welcome to python',
'hello 123', '@#$$$' etc.
Code:
a = "hello guys"
print(type(a))
Output;
Output:
Code:
num = int (input ("Enter the number: "))
num_sqrt = num ** 0.5
print ('The square root of %0.3f is %0.3f'% (num, num_sqrt))
Output:
Code:
mylist = [1,3,5,6,6,3,72,34]
print(list(mylist))
print ("length of list= “, len(mylist))
count = mylist. Count (6)
print ("Count of 6 in list”, count)
index= mylist. index (72)
print ("index number of 72 in list”, index)
mylist. append (54)
print ("after append= “, mylist)
mylist. insert (3, 2)
print ("after insert= “, mylist)
num = [2,65,7,4,8]
mylist. extend(num)
print ("after extend= “, mylist)
mylist. remove(34)
print ("after removing 34 = “, mylist)
poped_item = mylist.pop (3)
print ("poped item =", poped_item)
print ("list after pop= “, mylist)
mylist. reverse ()
print ("reversed list= “, mylist)
mylist. sort ()
print ("Sorted list= “, mylist)
copy_list= mylist. copy ()
print ("copied list =", copy_list)
copy_list. clear ()
print ("list after clear () = “, copy_list)
Output:
8.Demonstrate the different ways of creating tuple
objects with suitable example
A tuple in Python is similar to a list. The difference between the two is that
we cannot change the elements of a tuple once it is assigned whereas we can
change the elements of a list.
Code:
# Empty tuple
my_tuple = ()
print(my_tuple)
# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple)
# Tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple)
# Nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
Output:
9. Demonstrate the following function/methods which
operates on Tuple in python with suitable example
Code:
mytup = (1,3,5,6,6,3,72,34)
print ("after mutup () = “, mytup)
print ("length of tuple= “, len(mytup))
count = mytup. count (6)
print ("the count of 6 in tuple= “, count)
print ("the index of 72 in tuple = “, mytup. index (72))
print ("Sorted tuple = “, sorted(mytup))
print ("maximum value in tupple= “, max(mytup))
print ("minimum value in tupple= “, min(mytup))
print ("SUM of items in tuple= “, sum(mytup))
Output:
10. Demonstrate the different ways of creating
Dictionary objects with suitable example
Code:
numbers = {1: "One", 2: "Two", 3: "Three"}
things ={1:"car",2:"bike",3:"helicopter"}
num = dict(numbers)
print ("After dict () =", num)
print ("length of dictionary= “, len(numbers))
print ("After str ()", str (numbers))
vehicle = things. copy ()
print ("After copy () = ", vehicle)
print ("get () =%s" % numbers. Get (2))
print ("items () =%s " % numbers. items ())
poped_item= things.pop (3)
print ("poped item -”, poped_item)
print ("dictionary after pop = “, things)
item = things. popitem ()
print ("popitem () = “, item)
print ("keys () = %s "% numbers. keys ())
print ("values () = %s"% numbers. values ())
numbers1 ={4:"four",5:"five"}
numbers. update (numbers1)
print ("After update = “, numbers)
numbers. clear ()
print ("After clear () = “, numbers)
Output:
12. Demonstrate the different ways of creating Sets
objects with suitable example
Code:
student_id = {112, 114, 116, 118, 115}
print ("length of set = “, len(student_id))
student_id.add (119)
print ("After add () = “, student_id)
stu = {112,113,114,120}
print ("After union () = “, student_id. Union(stu))
student = {"a,","r","b"}
student_id. Update(student)
print ("After update () = “, student_id)
student_id. remove("r")
print ("After removing r = “, student_id)
clas= student_id.copy()
print ("After copy () = “, clas)
Output:
14.Demonstrate the following kinds of parameters
used while writing functions in python
Parameters:
A parameter is the variable listed inside the parentheses in the function
definition. An argument is the value that is sent to the function when it is
called
Types of parameters:
1.Positional parameters
2.Default parameters
3.Keyword parameters
4.Variable length parameters
Code:
1.Positional parameters:
def add (a, b):
c=a+b
print ("the sum of given no. is = “, c)
add (4,5)
Output:
2.Default parameters:
# c is default parameter
def add (a, b, c= 6):
D= a+b+c
Print ("the sum of given no. is = ", D)
Add (4,5)
Output
3.Keyword parameters:
def add (a, b):
D= a+b
Print ("the sum of given no. is = ", D)
Add (a=6, b=7)
Output:
4.Variable length parameter:
def add(*num):
z=num [0] +num [1] +num [2]
print ("first number = ", num [0])
print ("second number = ", num [1])
print ("third number = ", num [2])
print ("the sum of given no. is = ", z)
add (2, 56, 8)
Output:
15.Write a python program to find the factorial of a
given number
Code:
def factorial(n):
return 1 if (n==1 or n==0) else n * factorial (n - 1);
num = int (input ("Enter the number who's factorial you want: "))
print ("Factorial of”, num,"is",
factorial(num))
Output:
16.write a python program to find the Fibonacci
numbers
Code:
def Fibonacci(n):
if n < 0:
print ("Incorrect input")
elif n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
num=int (input ("Enter the number for fibonacci number= "))
print ("the Fibonacci number is = “, Fibonacci(num))
Output:
17.Write a program to find the HCF of three numbers
Code:
from math import gcd
def solve(nums):
if len(nums) == 1:
return nums [0]
div = gcd (nums [0], nums [1])
if len(nums) == 2:
return div
for i in range (1, len(nums) - 1):
div = gcd (div, nums [i + 1])
if div == 1:
return div
return div
nums = [15, 81, 78]
print ("The HCF of given number is = ", solve(nums))
Output:
18. Write a program to find the LCM of three numbers
Code:
import math
def LCMofArray(a):
lcm = a [0]
for i in range (1, len(a)):
lcm = lcm*a[i]//math.gcd (lcm, a[i])
return lcm
num = [1,2,3]
print ("LCM of arr1 elements:", LCMofArray(num))
Output:
19.Write a python program to find the sum of digits of
integer number
Code:
num = input ("Enter Number: ")
sum = 0
for i in num:
sum = sum + int(i)
print ("the sum of digits of number is = ", sum)
Output:
20. Write a python program to find the sum of digits
of integer number using recursion
Code:
def sum_of_digit (n):
if n == 0:
return 0
return (n % 10 + sum_of_digit (int (n / 10)))
num = int (input ("Enter numbers = "))
result = sum_of_digit(num)
print ("Sum of digits in ", num,"is", result)
Output:
21.Write a python program to find HCF of numbers
using recursion
Code:
def gcd (num1, num2):
if num2 == 0:
return num1;
return gcd (num2, num1 % num2)
num1 = int (input ("Enter first number: "))
num2 = int (input ("Enter second number: "))
print ("hcf/gcd of", num1,"and", num2,"=", gcd (num1, num2))
Output:
22.Write a python program to find the factorial of
number using recursion
Code:
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int (input ("Enter the number = "))
if num < 0:
print ("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print ("The factorial of 0 is 1")
else:
print ("The factorial of", num, "is", recur_factorial(num))
Output:
23.Write a program to implement single level
inheritance in python
Code:
class Parent:
def func1(self):
print ("This function is in parent class.")
class Child (Parent):
def func2(self):
print ("This function is in child class.")
object = Child ()
object. func1 ()
object. func2 ()
Output:
24.Write a program to implement Multilevel
inheritance in python
Code:
class Grandfather:
def __init__ (self, grandfathername):
self. grandfathername = grandfathername
class Father (Grandfather):
def __init__ (self, fathername, grandfathername):
self. fathername = fathername
Grandfather. __init__ (self, grandfathername)
class Son (Father):
def __init__ (self, sonname, fathername, grandfathername):
self. sonname = sonname
Father. __init__ (self, fathername, grandfathername)
def print_name(self):
print ('Grandfather name:', self. grandfathername)
print ("Father name:", self. fathername)
print ("Son name:", self. sonname)
s1 = Son ('Prince', 'Rampal', 'Lal mani')
print (s1. grandfathername) s1.print_name ()
Output:
25.Write a program to implement Hierarchy
inheritance in python
Code:
class Parent:
def func1(self):
print ("This function is in parent class.")
class Child1(Parent):
def func2(self):
print ("This function is in child 1.")
class Child2(Parent):
def func3 (self):
print ("This function is in child 2.")
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
Output:
26. Write a program to implement Multiple
inheritance in python
Code:
class Mother:
mothername = ""
def mother(self):
print (self. Mothername)
class Father:
fathername = ""
def father(self):
print (self. fathername)
class Son (Mother, Father):
def parents(self):
print ("Father:", self. fathername)
print ("Mother:", self. mothername)
s1 = Son ()
s1. fathername = "khali chand"
s1. mothername = "kampadi devi"
s1. parents ()
Output:
27.Write a program to implement Hybrid inheritance
in python
Code:
class School:
def func1(self):
print ("This function is in school.")
class Student1(School):
def func2(self):
print ("This function is in student 1. ")
class Student2(School):
def func3(self):
print ("This function is in student 2.")
class Student3(Student1, School):
def func4(self):
print ("This function is in student 3.")
object = Student3()
obj = Student2()
object. func1() object. func2() obj. func3() object. func4()
Output:
28. Write a program to find the sum of all elements of
an array.
Code:
def _sum(arr):
sum = 0
for i in arr:
sum = sum + i
return(sum)
arr = []
arr = [12, 3, 4, 15]
n = len(arr)
ans = _sum(arr)
print ('Sum of the array is ', ans)
Output:
29. Write a program to find the union and intersection
of two arrays in python.
Code:
firstArr=list (map (int, input ('Enter elements of first list:'). split ()))
secondArr=list (map (int, input ('Enter elements of second list:'). split ()))
x=list(set(firstArr)|set(secondArr))
y=list(set(firstArr)&set(secondArr))
Code:
m = [[4,1,2], [7,5,3], [9,6,9]]
for i in m:
print(i)
Output:
31. Write a program to create matrix using NumPy
Code:
import numpy as np
Output:
32. Write a program to find the maximum, minimum,
and sum of the elements in an array
Code:
import numpy as np
a = np. array ([10,20,30,40,50])
b = np.max(a)
c = np.min(a)
d = np.sum(a)
print ("the maximum value in array = “, b)
print ("the minimum value in array = “, c)
print ("the sum of elements of array = “, d)
Output: