Python Lab Manual (I BCA) 2023-2024 Final (1) - 8-23
Python Lab Manual (I BCA) 2023-2024 Final (1) - 8-23
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
# 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)
OUTPUT:
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
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
OUTPUT:
a=b: False
a+=b: 38
a-=b: 26
a*=b: 192
a%=b: 2
a**=b: 1073741824
a//=b: 5
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
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
OUTPUT:
Hello Python
Hello Python
OUTPUT:
H
E
L
L
O
IndexError: string index out of range
EX.NO: 10 PROGRAM USING MODULES
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
OUTPUT:
[1, 2, 'Python', 'Program', 15.9]
['Amy', 'Ryan', 'Henry', 'Emma']
< class ' list ' >
< class ' list ' >
OUTPUT:
(A) TUPLES
OUTPUT:
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
OUTPUT:
<class 'dict'>
printing Employee data ....
{'Name': 'Johnny', 'Age': 32, 'salary': 26000, 'Company': TCS}
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!
OUTPUT:
Hello
#Read lines
f=open(“demofile.txt”,”r”)
print(f.readline())
OUTPUT:
Hello! Welcome to demofile.txt
OUTPUT:
Hello! Welcome to demofile.txt