Python File Mca
Python File Mca
PRACTICAL
FILE FOR
PROGRAMMING
CONCEPT USING
PYTHON MCA (606)
Submitted To - Submitted By -
Dr. Shobha Arya Nandita Roy (62755)
INDEX
Introduction to Python :
Python is a high level, interpreted programming language that is
widely used for various purposes such as web development, data
analysis, artificial intelligence, and scientific computing.
History of Python :
Python was created by Guido Van Rossum in the late 1980s ad was
first released in 1991. It is named after the popular British comedy
show Monty Python.
Applications of Python :
Web development
Machine learning and AI
Software development
Network programming
CAS applications
LAB – 1
Aim – Program to demonstrate different
number of data types in python.
Code –
a = "I am a student of MCA"
print("a is a", type(a))
b = 24
print("b is a", type(b))
c = 19.5
print("c is a", type(c))
d = 6j
print("d is a", type(d))
e = ["Guava", "Litchi", "Cherry"]
print("e is a", type(e))
f = ("Car", "Bike", "Bus")
print("f is a", type(f))
g = range(10)
print("g is a", type(g))
h = {"name": "Arnav", "age": 22}
print("h is a", type(h))
i = {"Python", "Java", "SQL"}
print("i is a", type(i))
j = frozenset({"Linux", "Windows", "Mac"})
print("j is a", type(j))
k = True
print("k is a", type(k))
l = b"GBPUAT"
print("l is a", type(l))
m = bytearray(4)
print("m is a", type(m))
n = memoryview(bytes(6))
print("n is a", type(n))
o = None
print("o is a", type(o))
Output –
a is a <class 'str'>
b is a <class 'int'>
c is a <class 'float'>
d is a <class 'complex'>
e is a <class 'list'>
f is a <class 'tuple'>
g is a <class 'range'>
h is a <class 'dict'>
i is a <class 'set'>
j is a <class 'frozenset'>
k is a <class 'bool'>
l is a <class 'bytes'>
m is a <class 'bytearray'>
n is a <class 'memoryview'>
o is a <class 'NoneType'>
Operations on string –
Character at index 0 : P
Character at index 4 : o
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x = x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Code – Output –
x = 10 10
print(x) 13
x+=3 11
print(x) 3.6666666666666665
x-=2 3.6666666666666665
print(x) 49.29629629629629
x/=3 49.29629629629629
print(x) 49.29629629629629
x%=5
print(x)
x**=3
print(x)
x//9
print(x)
x!=3
print(x)
Code – Output –
x = True False
y = False True
print(x and y) False
print(x or y) True
print(not x )
print(not y)
Identity operators – identity operators are used to compare the
objects, not if they are equal, but if they are actually the same object,
with the same memory location.
Operator Description Example
is Returns true if both variables are the x is y
same object
is not Returns true if both variables are not x is not y
the same objcet
Code – Output –
x = [1,2,3] False
y = [1,2,3] True
print(x is y)
print(x is not y)
Code – Output –
x = [5,6,7,8,9]
True
print(5 in x)
False
print(2 in x)
Bitwise operators – bitwise operators are used to compare (binary)
numbers.
Operator Name Description Example
Code – Output –
x=6 4
y=4 6
print(x&y) 2
print(x|y) -7
print(x^y) 12
print(~x) 24
print(x<<1) 3
print(x<<2) 0
print(x>>1)
print(x>>3)
LAB – 4
Aim – Program for List Operation (Creation, Insertion,
Deletion, Append)
Slicing in list – the slice() function returns a slice object. A slice object
is used to specify how to slice a sequence using index.
Code – Output –
list = [10,20,3,50,45,25,15] [10, 20, 3, 50, 45, 25, 15]
list[1:4]
print(list)
Count – the count() method returns the number of times the
specified element appears in the list.
Code – Output –
fruits = ["apple", "banana", "cherry"] 1
x = fruits.count("cherry")
print(x)
Sorting – this method is used to sort the elements of the same data
type in ascending or descending.
Code – Output –
list = [10,20,3,5,4,5,7,9] [3, 4, 5, 5, 7, 9, 10, 20]
list.sort()
print(list)
Code – Output –
list = [10,20,3,5,4,5,7,9]
[20, 10, 9, 7, 5, 5, 4, 3]
list.sort(reverse= True)
print(list)
LAB – 5
Aim – Program for creation of dictionaries in python
and their operations.
Dictionary – dictionary are used to store data values in key value
pairs. A dictionary is a collection which is ordered, changeable and do
not allow duplicates. Dictionaries cannot have two items with the
same key. Dictionaries are written with the curly brackets, and have
keys and values.
Dictionary operations –
Output –
Get – the get() method will return the value of a dictionary entry for
a specified key.
Code – Output –
car = { Mustang
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get("model")
print(x)
Creating an object –
Code –
def full_pyramid(n):
for i in range(1, n + 1):
full_pyramid(5)
Output –
*
***
*****
*******
*********
LAB – 9
Aim – WAP to generate the fibonacci series.
Code –
n = 10
num1 = 0
num2 = 1
next_number = num2
count = 1
while count <= n:
print(next_number, end=" ")
count += 1
num1, num2 = num2, next_number
next_number = num1 + num2
print()
Output –
1 2 3 5 8 13 21 34 55 89
LAB – 10
Aim – WAP to check whether a given number is an
Armstrong number.
Code –
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output –
Code –
num = 29
flag = False
if num == 0 or num == 1:
print(num, "is not a prime number")
elif num > 1:
for i in range(2, num):
if (num % i) == 0:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
Output –
29 is a prime number
LAB – 12
Aim – WAP to calculate factorial of a number using
recursion.
Code –
def factorial(x):
if x == 1 or x == 0:
return 1
else:
return (x * factorial(x-1))
num = 7
result = factorial(num)
print("The factorial of", num, "is", result)
Output –