J.C.
Bose University of Science and Technology, YMCA, FARIDABAD, HARYANA
IT Workshop(Python)
Submitted by: Gautam Buddh
Roll no: 21001016024
Branch: Computer Engineering with data science.
[Link] a program to purposefully raise indentation
Error and correct it
Code: Indentation Error:
if 98 < 100:
print("hlo")
Output:
Code: Corrected
if 98 < 100:
print("hlo")
Output:
[Link] about the fundamental data types in
python programming
Python programming language has four primitive or fundamental
data types, namely, integers, floats, booleans and strings.
[Link]
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:
[Link]
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:
[Link]
In python programming, Boolean data types is used to represent
logical
True and False
Code:
a = True
print(type(a))
Output:
[Link]
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;
[Link] a program to check weather the given number
is even or not, Using for loop
Code:
num = int (input ("Enter a number: "))
if (num % 2) == 0:
print ("{0} is Even”. format (num))
else:
print ("{0} is Odd”. format(num))
Output:
[Link] a program with nested for loop to find the
prime number
Code:
i = 2, j = 2
while (i < 100):
while (j <= (i/j)):
if not(i%j): break j = j + 1
if (j > i/j): print (i, " is prime")
i=i+1
print ("Good bye!")
Output:
[Link] a program to find the square root of number
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:
[Link] the different ways of creating list
objects with suitable example programs
A list is a collection of similar or different types of data.
Code:
# Empty list
list = []
# List of int number
mylist = [1,3,5,6]
# List of names
mylist1 = ["Rahul","Mohan"]
# List of mix data
mylist2 = ["hlo", 23,"bye",89]
print ("list= “, list)
print ("mylist= “, mylist)
print ("mylist1= “, mylist1)
print ("mylist2= “, mylist2)
Output:
[Link] the following function/methods which
operates on lists in python with suitable example
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 = [Link] (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:
[Link] 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
Python dictionary is an ordered collection (starting from Python 3.7) of items.
It stores elements in key/value pairs. Here, keys are unique identifiers that
are associated with each value.
Code:
# Dictionary
numbers = {1: "One", 2: "Two", 3: "Three"}
print(numbers)
Output:
11. Demonstrate the following function/methods
which operates on Dictionary in python 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= [Link] (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
A set is a collection of unique data. That is, elements of a set
cannot be duplicate
Code:
# set of integer type
student_id = {112, 114, 116, 118, 115}
print ('Student ID:', student_id)
# Set of string type
vowel_letters = {'a', 'e', 'i', 'o', 'u'}
print ('Vowel Letters:', vowel_letters)
# Set of mixed data types
mixed_set = {'Hello', 101, -2, 'Bye'}
print ('Set of mixed data types:', mixed_set)
Output:
[Link] the following function/methods
which operates on Sets in python 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:
[Link] 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:
[Link] parameters
[Link] parameters
[Link] parameters
[Link] length parameters
Code:
[Link] parameters:
def add (a, b):
c=a+b
print ("the sum of given no. is = “, c)
add (4,5)
Output:
[Link] 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
[Link] parameters:
def add (a, b):
D= a+b
Print ("the sum of given no. is = ", D)
Add (a=6, b=7)
Output:
[Link] 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:
[Link] 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:
[Link] 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:
[Link] 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]//[Link] (lcm, a[i])
return lcm
num = [1,2,3]
print ("LCM of arr1 elements:", LCMofArray(num))
Output:
[Link] 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:
[Link] 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:
[Link] 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:
[Link] 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:
[Link] 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:
[Link] 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:
[Link] 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))
print ('Union of the arrays:’, x)
print ('intersection of the arrays:’, y)
Output:
30. Write a program to create matrix in Python.
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
arr = np. array ([[ 1, 2, 3], [ 4, 5, 6]])
print(arr)
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 = [Link](a)
c = [Link](a)
d = [Link](a)
print ("the maximum value in array = “, b)
print ("the minimum value in array = “, c)
print ("the sum of elements of array = “, d)
Output: