[go: up one dir, main page]

0% found this document useful (0 votes)
150 views16 pages

Python Lab Manual (I BCA) 2023-2024 Final (1) - 8-23

Uploaded by

mravigym
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views16 pages

Python Lab Manual (I BCA) 2023-2024 Final (1) - 8-23

Uploaded by

mravigym
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

EX.

NO: 1 PROGRAM USING VARIABLES , CONSTANTS , I/O STATEMENTS

#Creating a Variable and assign value for it


Var = "python"
print(Var)

OUTPUT:
‘python’
# valid variable name
a=1
b=2
c=5
_d = 6
E_ = 7
_f_ = 8

print(a,b,c)
print(_d, E_, _f_)

OUTPUT:

123
456

#Assigning value to the variable

# An integer assignment
age = 45

# A floating point
salary = 1456.8

# A string
name = "John"

print(age)
print(salary)
print(name)

OUTPUT:
45
1456.8
‘John’
# declaring the var
Number = 100

# display
print("Before declare: ", Number)

# re-declare the var


Number = 120.3
print("After re-declare:", Number)

OUTPUT:

Before declare: 100


After re-declare: 120.3

#Assign values to multiple variables


a = b = c = 10

print(a)
print(b)
print(c)

OUTPUT:
10
10
10

#Constants
#create a constant.py
PI=3.14
GRAVITY=9.8
#create main.py
import constant
print(constant.PI)
print(constant.GRAVITY)

OUTPUT:
3.14
9.8

#I/O statement
name=input(“Enter the name:”)
Print(“Hello”,name)

OUTPUT:
Enter the name: GFG
‘Hello GFG’
EX .NO: 02 PROGRAM USING OPERATORS IN PYTHON

a = 32 # Initialize the value of a


b=6 # Initialize the value of b
print('Two numbers are equal or not:',a==b)
print('Two numbers are not equal or not:',a!=b)
print('a is less than or equal to b:',a<=b)
print('a is greater than or equal to b:',a>=b)
print('a is greater b:',a>b)
print('a is less than b:',a<b)

OUTPUT:
Two numbers are equal or not: False
Two numbers are not equal or not: True
a is less than or equal to b: False
a is greater than or equal to b: True
a is greater b: True
a is less than b: False

a = 32 # Initialize the value of a


b=6 # Initialize the value of b
print('a=b:', a==b)
print('a+=b:', a+b)
print('a-=b:', a-b)
print('a*=b:', a*b)
print('a%=b:', a%b)
print('a**=b:', a**b)
print('a//=b:', a//b)

OUTPUT:
a=b: False
a+=b: 38
a-=b: 26
a*=b: 192
a%=b: 2
a**=b: 1073741824
a//=b: 5

a=5 # initialize the value of a


b=6 # initialize the value of b
print('a&b:', a&b)
print('a|b:', a|b)
print('a^b:', a^b)
print('~a:', ~a)
print('a<<b:', a<<b)
print('a>>b:', a>>b)

OUTPUT:
a&b: 4
a|b: 7
a^b: 3
~a: -6
a<>b: 0
a=5 # initialize the value of a
print(Is this statement true?:',a > 3 and a < 5)
print('Any one statement is true?:',a > 3 or a < 5)
print('Each statement is true then return False and vice-versa:',(not(a > 3 and a < 5)))

OUTPUT:
Is this statement true?: False
Any one statement is true?: True
Each statement is true then return False and vice-versa: True

x = ["Rose", "Lotus"]
print(' Is value Present?', "Rose" in x)
print(' Is value not Present?', "Riya" not in x)

OUTPUT:
Is value Present? True
Is value not Present? True
EX.NO:3 PROGRAM USING CONDITIONAL STATEMENTS

num = 5
if num > 0:
print(num, "is a positive number.")
print("This statement is true.")

OUTPUT:
5 is a positive number.
This statement is true.

num = 5
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

OUTPUT :
Positive or Zero

num = 7
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

OUTPUT:
Positive number

num = 8
if num >= 0:
if num == 0:
print("zero")
else:
print("Positive number")
else:
print("Negative number")

OUTPUT:
Positive number
EX.NO: 4 PROGRAM USING LOOPS

#while loop
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")

OUTPUT:
Hello Geek
Hello Geek
Hello Geek

# for loop
# Iterating over range 0 to n-1
n=4
for i in range(0, n):
print(i)

OUTPUT:
0
1
2
3
EX.NO: 5 PROGRAM USING JUMP STATEMENTS

#BREAK STATEMENT
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num == 6:
print("Found 6, breaking loop.")
break
print(num)

OUTPUT:
1
2
3
4
5
Found 6, breaking loop.

#CONTINUE
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)

OUTPUT:
1
3
5
7
9

#PASS STATEMENT
li =['a', 'b', 'c', 'd']
for i in li:
if(i =='a'):
pass
else:
print(i)

OUTPUT:
b
c
d
EX.NO: 6 PROGRAM USING FUNCTIONS

#function definition
def find_square(num):
result = num * num
return result
# function call
square = find_square(3)
print('Square:',square)

OUTPUT:
Square: 9
EX.NO: 7 PROGRAM USING RECURSION

# Recursive function
def recursive_factorial(n):
if n == 1:
return n
else:
return n * recursive_factorial(n-1)
# user input
num = 6
# check if the input is valid or not
if num < 0:
print("Invalid input ! Please enter a positive number.")
elif num == 0:
print("Factorial of number 0 is 1")
else:
print("Factorial of number", num, "=", recursive_factorial(num))

OUTPUT:
Factorial of number 6 = 720
EX.NO: 8 PROGRAM USING ARRAYS

# importing "array" for array creations


import array as arr
# array with int type
a = arr.array('i', [1, 2, 3])
print("Array before insertion : ", end="")
for i in range(0, 3):
print(a[i], end="")
print()
# inserting array using
# insert() function
a.insert(1, 4)
print("Array after insertion : ", end="")
for i in (a):
print(i, end="")
print()
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
print("Array before insertion : ", end="")
for i in range(0, 3):
print(b[i], end="")
print()
# adding an element using append()
b.append(4.4)
print("Array after insertion : ", end="")
for i in (b):
print(i, end="")
print()

OUTPUT:
Array before insertion : 1 2 3
Array after insertion : 1 4 2 3
Array before insertion : 2.5 3.2 3.3
Array after insertion : 2.5 3.2 3.3 4.4
EX.NO: 9 PROGRAM USING STRINGS

#Using single quotes


str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)
#Using triple quotes
str3 = '''''Triple quotes are generally used for
represent the multiline or
docstring'''
print(str3)

OUTPUT:
Hello Python
Hello Python

#Strings Indexing and Splitting


str = "HELLO"
print(str[0])
print(str[1])
print(str[2])
print(str[3])
print(str[4])
# It returns the IndexError because 6th index doesn't exist
print(str[6])

OUTPUT:
H
E
L
L
O
IndexError: string index out of range
EX.NO: 10 PROGRAM USING MODULES

#Save this code in a file named mymodule.py


def greeting(name):
print("Hello, " + name)
#Open new file type the code and then save and run it
import mymodule
mymodule.greeting("Jonathan")

OUTPUT:
Hello, Jonathan
#Save this code in the file mymodule.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}

#Import the module named mymodule, and access the person1 dictionary:
import mymodule
a = mymodule.person1["age"]
print(a)

OUTPUT:
36
EX.NO: 11 PROGRAM USING LISTS

(A) LIST

list1 = [1, 2, "Python", "Program", 15.9]


list2 = ["Amy", "Ryan", "Henry", "Emma"]
print(list1)
print(list2)
print(type(list1))
print(type(list2))

OUTPUT:
[1, 2, 'Python', 'Program', 15.9]
['Amy', 'Ryan', 'Henry', 'Emma']
< class ' list ' >
< class ' list ' >

(B) CONCATENATIONS OF TWO LISTS

list1 = [12, 14, 16, 18, 20]


list2 = [9, 10, 32, 54, 86]
l = list1 + list2
print(l)

OUTPUT:

[12, 14, 16, 18, 20, 9, 10, 32, 54, 86]


EX.NO: 12 PROGRAM USING TUPLES

(A) TUPLES

tuple1 = ("apple", "banana", "cherry")


tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
print(tuple1)
print(tuple2)
print(tuple3)

OUTPUT:

('apple', 'banana', 'cherry')


(1, 5, 7, 9, 3)
(True, False, False)

(B) DECLARATIONS OF TUPLES IN DIFFERENT TYPES

empty tuple = ()
print ("Empty tuple: ", empty tuple)
int_tuple = (4, 6, 8, 10, 12, 14)
print ("Tuple with integers: ", int_tuple)
mixed_tuple = (4, "Python", 9.3)
print ("Tuple with different data types: ", mixed_tuple)
nested_tuple = ("Python", {4: 5, 6: 2, 8:2}, (5, 3, 5, 6))
print ("A nested tuple: ", nested_tuple)

OUTPUT:

Empty tuple: ()
Tuple with integers: (4, 6, 8, 10, 12, 14)
Tuple with different data types: (4, 'Python', 9.3)
A nested tuple: ('Python', {4: 5, 6: 2, 8: 2}, (5, 3, 5, 6))
EX.NO: 13 PROGRAM USING DICTIONARIES

(A) DICTIONARIES

Employee = {"Name": "Johnny", "Age": 32, "salary":26000,"Company":"TCS"}


print (type (Employee))
print ("printing Employee data ..... ")
print (Employee)

OUTPUT:

<class 'dict'>
printing Employee data ....
{'Name': 'Johnny', 'Age': 32, 'salary': 26000, 'Company': TCS}

(B) ACCESSING THE DICTIONARY VALUES


Employee = {"Name": "Dev", "Age": 20, "salary":45000,"Company":"WIPRO"}
print (type (Employee))
print ("printing Employee data ..... ")
print ("Name: %s" %Employee["Name"])
print ("Age: %d" %Employee["Age"])
print ("Salary: %d" %Employee["salary"])
print ("Company: %s" %Employee["Company"])

OUTPUT:

<class 'dict'>
printing Employee data ....
Name: Dev
Age: 20
Salary: 45000
Company: WIPRO
EX.NO: 14 PROGRAM USING FILE HANDLING

#demofile.txt
Hello! Welcome to demofile.txt
This file is for testing purposes
Good Luck!

#Read lines
f=open(“demofile.txt”,”r”)
print(f.read())

OUTPUT:
Hello! Welcome to demofile.txt
This file is for testing purposes
Good Luck!

#Read only the parts of the File


f=open(“demofile.txt”,”r”)
print(f.read(5))

OUTPUT:
Hello

#Read lines
f=open(“demofile.txt”,”r”)
print(f.readline())

OUTPUT:
Hello! Welcome to demofile.txt

#close the file


f=open(“demofile.txt”,”r”)
print(f.readline())
f.close()

OUTPUT:
Hello! Welcome to demofile.txt

You might also like