Python 2024
Python 2024
➢ print("Hello")
Output :
2
a=5
b=7
c=a
a=b
b=c
print("value of a=",a)
print("value of b=",b)
Output :
3
print("Fibonacci sequence:")
while count < nterms: #0<5,1<5,2<5,3<5,4<5,5<5 so false
print(n1) #answer = 0,1,1,2,3
nth = n1 + n2 #0+1=1,1+1=2,1+2=3,2+3=5,3+5=8
# update values
n1 = n2 #n1=1,1,2,3,5
n2 = nth #n2=1,2,3,5,8
count = count + 1 #count=0+1=1,1+1=2,2+1=3,3+1=4,4+1=5
Output :
4
Output :
5
Output :
7
Output :
8
PI = 3.14
r = float(input("enter radius : "))
area = PI*r*r
print("area of circle = ", area)
Output :
9
Output :
10
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
#print(rev)
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Output :
11
# List of strings
str = ["Test", "My", "Word", "Tag", "Has"]
str.sort()
print("\nascending=",str)
str.sort(reverse=True)
print("descending=",str)
Output :
12
Output :
14
result = a(x, y)
#print("Sum is:", result)
print('The sum of {0} and {1} is : {2}'.format(x, y, result))
Output :
15
def car_details(self):
print("Car brand is ", self.brand_name)
print("Car model is ", self.model)
print("Car manufacture year is ", self.manu_year)
def get_Car_brand(self):
print("Car brand is ", self.brand_name)
def get_Car_model(self):
print("Car model is ", self.model)
[15b]
from p15a import Car
car_det = Car("Hyundai", ' Creta SX(O) 1.5', 2020)
print(car_det.brand_name)
print(car_det.car_details())
print(car_det.get_Car_brand())
print(car_det.get_Car_model())
Output :
16
Output :
17
#append item
odd.append(10)
print(odd) #Output : [1, 3, 5, 7, 10]
#extend item
odd.extend([9, 11, 13])
print(odd) #Output : [1, 3, 5, 7, 10, 9, 11, 13]
#adding of 2 lists
print(odd + [9, 7, 5]) #Output : [1, 3, 7, 10, 9, 11, 13, 9, 7, 5]
Output :
18
Output :
19
print(num[2][2][0]) # Prints 5
Output :
20
Output :
21
21. Write a Python program to read a file bca.txt and print the
contents of file along with number of vowels present in it.
file1 = open("bca21.txt", "r")
str1 = file1.read()
vowel_count = 0
for i in str1:
if (i == 'A' or i == 'a' or i == 'E' or i == 'e' or i == 'I'or i
== 'i' or i == 'O' or i == 'o'or i == 'U' or i == 'u'):
vowel_count =vowel_count + 1
print("\n" + str1 + "\n")
print('The Number of Vowels in text file :', vowel_count)
file1.close()
Output :
22
except:
print("Error: number2 cannot be 0.")
finally:
print("This is finally block.")
Output :
23
Create Database
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password=""
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE crudoperation")
Create Table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="crudoperation"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE students (std_ID INT, FirstName
VARCHAR(255), LastName VARCHAR(255),Contact VARCHAR(255))")
24
Insert Data
import mysql.connector
mycursor.execute(sql_insert, values_insert)
mydb.commit()
Output :
26
Update Data
import mysql.connector
if new_first_name:
sql_update += " FirstName = %s,"
update_values.append(new_first_name)
if new_last_name:
sql_update += " LastName = %s,"
update_values.append(new_last_name)
if new_age:
sql_update += " Age = %s,"
update_values.append(new_age)
if mycursor.rowcount > 0:
print(f"{mycursor.rowcount} record(s) updated successfully.")
mycursor.execute ("SELECT * FROM students")
result_select = mycursor.fetchall ()
print ("\nUpdated List of Students:")
for row in result_select :
print (row)
else:
print(f"No records updated. Student ID {student_id_to_update}
not found.")
Output :
28
Delete Data
import mysql.connector
mycursor.execute(sql_delete, values_delete)
mydb.commit()
if mycursor.rowcount > 0:
print(f"{mycursor.rowcount} record(s) deleted successfully.")
# Display the updated data in the table
mycursor.execute("SELECT * FROM students")
result_select_after_delete = mycursor.fetchall()
print("\nUpdated List of Students:")
for row in result_select_after_delete:
print(row)
else:
print(f"No records deleted. Student ID {student_id_to_delete}
not found.")
Output :
30
Search Data
import mysql.connector
# Display the data for a particular row where std_ID matches the
specified value
mycursor.execute("SELECT * FROM students WHERE std_ID = %s",
(std_id_to_search,))
result_select = mycursor.fetchall()
Output :