0% found this document useful (0 votes)
46 views15 pages

PYTHON

The document contains Python code for 32 programs. The programs cover a range of concepts including: palindrome checking, loops, lists, functions, classes, inheritance, file handling, and random number generation. Many programs take user input, perform operations on lists or files, and output results. Overall the programs demonstrate a variety of Python coding techniques and concepts.

Uploaded by

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

PYTHON

The document contains Python code for 32 programs. The programs cover a range of concepts including: palindrome checking, loops, lists, functions, classes, inheritance, file handling, and random number generation. Many programs take user input, perform operations on lists or files, and output results. Overall the programs demonstrate a variety of Python coding techniques and concepts.

Uploaded by

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

PYTHON CODE

------------------------------------------------------------------------

# PALINDROME

n=int(input("Enter the number"))


t=n
rev=0
while t>0:
s=t%10
rev=(rev*10)+s
t//=10
if rev==n:
print("Given number is palindrome")
else:
print("Given number is not palindrome")

# PROGRAM 1

a=int(input("Enter the number"))


pre_num=0
for i in range(0,a):
add=pre_num+i
print("current number:",i,"previous number:",pre_num,"sum of the current
value:",add)
pre_num=i

------------------------------------------------------------------------

# PROGRAM 2

num=int(input("Enter the number check that prime or not"))


if num==1:
print("1 is not prime number")
elif num==2:
print("2 is prime number")
elif num>1:
for i in range(2,num):
if(num%i==0):
print("The number is not prime")
break
else:
print("it is a prime number")

else:
print("it is not valid input")

------------------------------------------------------------------------

# PROGRAM 3
a=int(input("Enter the prime number range"))
for i in range(2,a):
for j in range(2,a):
if i%j==0:
break
if i==j:
print(i,end=",")

------------------------------------------------------------------------

# PROGRAM 4

a="computer"
for i in range(0,len(a),i+2):
print(a[i])

------------------------------------------------------------------------

# PROGRAM 5

a=[1,2,3,4,5,6]
sum=0
for i in a:
sum+=i
print(sum)

------------------------------------------------------------------------

# PROGRAM 6

a=[10,2]
sum=1
for i in a:
sum*=i
print(sum)

------------------------------------------------------------------------

# PROGRAM 7

lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=int(input("Enter the array elements"))
lst.append(ele)
print(lst)
print("product values of the list elements ")
sum=1
for i in lst:
sum*=i
print(sum)
print ("the sum of the list elements")
sum=0
for i in lst:
sum+=i
print(sum)

------------------------------------------------------------------------

# PROGRAM 8

lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=int(input("Enter the array elements"))
lst.append(ele)
print(lst)
lst.sort()
print("The largest number is:",lst[-1])

------------------------------------------------------------------------

# PROGRAM 9

lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=int(input("Enter the array elements"))
lst.append(ele)
print(lst)
m=lst[0]
for i in lst:
if i>m:
m=i
print(m)

------------------------------------------------------------------------

# PROGRAM 10

def MAX(list):
m=list[0]
for i in list:
if i>m:
m=i
return m
lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=int(input("Enter the array elements"))
lst.append(ele)
print(lst)
d=MAX(lst)
print(d)

------------------------------------------------------------------------

# PROGRAM 11

lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=input("Enter the array elements")
lst.append(ele)
print(lst)
c=0
for i in lst:
if (i[0]==i[-1]):
c=c+1
print(c)

------------------------------------------------------------------------

# PROGRAM 12

def MIN(list):
m=list[0]
for i in list:
if i<m:
m=i
return m
lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=int(input("Enter the array elements"))
lst.append(ele)
print(lst)
d=MIN(lst)
print(d)

------------------------------------------------------------------------

# PROGRAM 13

a=int(input("Enter the year"))


if ((a%400 == 0) and ( a%100 == 0)):
print("The year is leap year")
elif((a%4 == 0) and ( a%100 != 0)):
print("This year is leap year")
else:
print("This not leap year")

------------------------------------------------------------------------
# PROGRAM 14

n=int(input("Enter the factorial number"))


fact=1
for i in range(1,n+1):
fact=fact*i
print(fact)

------------------------------------------------------------------------

# PROGRAM 15

def secondlargestelement(list):
list.sort()
return list[-2]
lst=[]
a=int(input("Enter the range of list elements"))
for i in range (0,a):
ele=int(input("Enter the array elements"))
lst.append(ele)
print(lst)
E=[]
O=[]
for i in range(0,a):
if i%2==0:
e=lst[i]
E.append(e)
else:
o=lst[i]
O.append(o)

print(E)
print(O)
Evenl=secondlargestelement(E)
Oddl=secondlargestelement(O)
print(Evenl)
print(Oddl)
Sum=Evenl+Oddl
print("Sum",Sum)

------------------------------------------------------------------------

# PROGRAM 16

file=open('Mypythonfile.txt','x')

------------------------------------------------------------------------

# PROGRAM 17
fp=open("Mypythonfile.txt","w")
fp.write("I am writing sentence 4")
fp.close()

------------------------------------------------------------------------

# PROGRM 18

file=open('Mypythonfile.txt','r')

------------------------------------------------------------------------

# PROGRAM 19

fp=open('helloworld.txt','x')

------------------------------------------------------------------------

# PROGRM 20

fp=open('helloworld.txt','w')
fp.write("Hello World")

------------------------------------------------------------------------

# PROGRAM 21

numberofwords=0
with open(r'helloworld.txt','r') as fp:
data=fp.read()
line=data.split()
numberofwords +=len(line)
print(numberofwords)

------------------------------------------------------------------------

# PROGRAM 22

character=0
numberofwords=0
with open(r'helloworld.txt','r') as fp:
data=fp.read()
line=data.split()
numberofwords +=len(line)

character += len(numberofwords)
print(numberofwords)
print(character)

------------------------------------------------------------------------

# PROGRAM 23

import random
lst=[]
n=int(input("Enter the range of list :"))
for i in range(0,n):
ele=int(input("Enter the list element :"))
lst.append(ele)
print(lst)
d=random.choice(lst)
print(d)

------------------------------------------------------------------------

# PROGRAM 24

import random
set=('Elon',123,'Musk',4.5,567)
print(random.choice(set))

------------------------------------------------------------------------

# PROGRAM 25

import random
dict = {
"Apple": ["Green", "Healthy", "Sweet"],
"Banana": ["Yellow", "Squishy", "Bland"],
"Steak": ["Red", "Protein", "Savory"]
}

random_value=random.choice(list(dict.values()))
print ("Random_value : ",random_value)
random_key=random.choice(list(dict.keys()))
print ("Random_key : ",random_key)
random_item=random.choice(list(dict.items()))
print ("Random_item : ",random_item)

------------------------------------------------------------------------

# PROGRAM 26

import os,random
print("\nSelect a random file from a directory.:")
print(random.choice(os.listdir("/")))
------------------------------------------------------------------------

# PROGRAM 27

res=""
inp=input("Enter the string")
for i in range(0,len(inp),2):
for j in range(int(inp[i+1])):
res=res+inp[i]
print(res)

------------------------------------------------------------------------

# PROGRAM 28

class Dog:
def __init__(self,name,age):
self.name=name
self.age=age
def bark(self):
print(f"{self.name} is barking!")
def Age(self):
print(f"{self.age} years old!")
def Detail(self):
print(f"Dog name is :{self.name}")
print(f"Dog age is :{self.age}")
my_dog=Dog("Buddy",3)
my_dog.bark()
my_dog.Age()
dog=Dog("Rose",5)
dog.Detail()

------------------------------------------------------------------------

# PROGRAM 29

class Vehicle:
def __init__(self,speed,Mileage,name):
self.speed=speed
self.Mileage=Mileage
self.name=name
model=Vehicle(220,50,"ferrari")
print(f"The Vehicle name is :{model.name},and its top speed is: {model.speed} ,and
its Milage:{model.Mileage}")

------------------------------------------------------------------------

# PROGRAM 30

class areaofcircle:
def __init__(self,pi,r):
self.pi=pi
self.r=r
self.t=pi*r*r
def area(self):
print(f"Area of the circle :{self.t}")
circle_area=areaofcircle(3.14,10)
circle_area.area()
class circum:
def __init__(self,pi,r):
self.pi=pi
self.r=r
self.t=pi*r*2
def cir(self):
print(f"#circum of the circle :{self.t}")
circle_circum=circum(3.14,4)
circle_circum.cir()

------------------------------------------------------------------------

# PROGRAM 31

#inheritance
class Scl:
def __init__(self,name,regno):
self.name=name
self.regno=regno
class Student(Scl):
def Details(self,atten,bldg):
self.atten=atten
self.bldg=bldg
print(f"the student name is {self.name}and student bloodgroup is
{self.bldg}")
my_detail=Student("vijay",610722)

my_detail.Details(60,"o+")

------------------------------------------------------------------------

# PROGRAM 32

#inheritance using super


class Scl:
def __init__(self,name,age):
self.name=name
self.age=age
def print(self):
#print(f"the student name is {self.name}and student bloodgroup is
{self.year}")
print(f"the student name is {self.name}and student bloodgroup is
{self.age}")
class Student(Scl):
def __init__(self,name,age,currentyear):
super().__init__(name,age)
self.year=currentyear

def Display(self):
print(f"the student name is {self.name}and student bloodgroup is
{self.year}")

my_detail=Student("vijay",61,2019)
my_detail.print()
my_detail.Display()

------------------------------------------------------------------------

# PROGRAM 33

class Vehicle:
def __init__(self, seating_capacity):
self.seating_capacity = seating_capacity

def fare(self):
return self.seating_capacity * 100

class Bus(Vehicle):
def __init__(self, seating_capacity):
super().__init__(seating_capacity)

def fare(self):
original_fare = super().fare()
return original_fare + (0.1 * original_fare)

my_bus = Bus(50)

print(f"The total fare for the bus is {my_bus.fare()} units.")

------------------------------------------------------------------------

# PROGRAM 34

class Employee:
def __init__(self,emp_id,emp_name,emp_department):
self.emp_id=emp_id
self.emp_name=emp_name
self.emp_department=emp_department
def calculate_emp_salary(self):
def emp_assign_department(self):
def print_employee_Detail(self):

------------------------------------------------------------------------
INSTALL AND IMPORT NUMPY

# PROGRAM 35

import numpy as np

# In[1]:
a=np.array(23)
print(a)

# In[2]:

print(a.ndim)

# In[3]:

get_ipython().system('pip install numpy')

# In[10]:

print(a.shape)

# In[4]:

print(type(a))

# In[12]:

print(a.size)

# In[5]:

b=np.array([[1,2],[3,4]])
print(b)

# In[6]:

print(type(b))

# In[7]:

print(b.shape)
print(b.ndim)

# In[8]:

print(b.size)
# In[9]:

#3_D array
c=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(c)
#shape
print(c.shape)
#size
print(c.size)
#type
print(type(c))
#dimensional
print(c.ndim)

# In[10]:

#Accessing one dimensional array


d=np.array([1,2,3,4,5,6,7,8,9,10])
print(d[:])#start and stop
print(d[0:3])#start:Stop
print(d[0:8:2])# start:stop:step
print(d[::-1])#reverse array

# In[11]:

#Accessing Two dimensional array


E=np.array([[1,2,3,4],[5,6,7,8]])
print(E)#print array all elements
print(E[0:1]) #print 1 row only
print(E[0:2]) #print 2 rows
print(E[0,0]) #print the specific element
print(e)

# In[13]:

#Accessing the three dimensional array


F=np.array([[1,2,3,4],[5,6,7,8],[7,8,9,2]])
print(F)
print("__________")
print(F[0:1])
print("__________")
print(F[1:2])
print("__________")
print(F[2:3])
print("__________")
print(F[0,3])
print("__________")
print(F[1,2])
# In[14]:

#list numeric elements convert to array


lst=[12.23,13.32,100,36.32]
print("Original list",lst)
a=np.array(lst)
print("Modify to array elements",a)

# In[15]:

#program 3*3 values range from 2 to 10


g=np.array([2,3,4,5,6,7,8,9,10])
print(g)
t=g.reshape(3,3)
print(t)

# In[16]:

#python program to create null vector and update the values


q=np.zeros(10)
print(q)
q[5]=11
print(q)

# In[17]:

list=[1,2,3,4,5,6,7,8]
tuple=(8,7,6,5,4,3,2,1)
a=np.array(list)
b=np.array(tuple)
print(a)
print(b)

# In[18]:

list=[1,2,3,4,5,6,7,8]
tuple=([8,7,6],[5,4,3])
a=np.array(list)
b=np.array(tuple)
print(a)
print(b)

# In[19]:

a=np.array((10,20,30))
b=np.array((40,50,60))
c=np.column_stack((a,b))
print(c)

-------------------------------------------------------------------------
MYSQL CONNECTOR

# PROGRAM 36

get_ipython().system('pip install Mysql-connector-python')

# In[1]:

import mysql.connector
mydb=mysql.connector.connect(
host= " localhost",
user="root",
password="Gceb@6107",
database="gceb")
print(mydb)

# In[2]:

mycursor=mydb.cursor()
mycursor.execute("create database gceb")

# In[26]:

mycursor.execute("show database")
for x in range(mycursor):
print(x)

# In[3]:

mycursor.execute("use gceb")

# In[4]:

mycursor.execute("create table students(std_id numeric primary key not


null,std_fname varchar(45),std_lname varchar(45),std_age numeric,std_mark
numeric)")

# In[5]:

mycursor=mydb.cursor(buffered="true")
col="insert into students (std_id,std_fname,std_lname)values (%s,%s,%s)"
val=[(102,"ckon","dkon"),(103,"ekon","fkon"),(104,"gkon","hkon")]
mycursor.executemany(col,val)
mydb.commit()
print(mycursor.rowcount,"record inserted")

# In[6]:

mycursor.execute("select *from students")


myresult=mycursor.fetchall()
for x in myresult:
print(x)

# In[7]:

mycursor=mydb.cursor(buffered="true")
mycursor.execute("select *from students")
myresult=mycursor.fetchone()
for x in myresult:
print(x)

# In[8]:

mycursor.execute("select *from students")


myresult=mycursor.fetchmany()
for x in myresult:
print(x)

# In[9]:

mycursor.execute("select *from students")


myresult=mycursor.fetchone()
for x in myresult:
print(x)

You might also like