100% found this document useful (2 votes)
209 views20 pages

Phython 1

The document contains 20 Python programs with various functions like checking if a number is positive, negative or zero; finding the factorial, grade, or leap year of a number; determining the largest of three numbers; calculating the LCM of two numbers; counting digits; printing tables; generating the Fibonacci series; checking if a number is prime; displaying bar and pie charts; performing linear and binary searches; adding numbers in a list recursively; writing to a text file; creating, inserting, updating, and selecting data from a MySQL database table.

Uploaded by

Divyansh Rana
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
100% found this document useful (2 votes)
209 views20 pages

Phython 1

The document contains 20 Python programs with various functions like checking if a number is positive, negative or zero; finding the factorial, grade, or leap year of a number; determining the largest of three numbers; calculating the LCM of two numbers; counting digits; printing tables; generating the Fibonacci series; checking if a number is prime; displaying bar and pie charts; performing linear and binary searches; adding numbers in a list recursively; writing to a text file; creating, inserting, updating, and selecting data from a MySQL database table.

Uploaded by

Divyansh Rana
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/ 20

PROGRAM NO.

PYTHON
PROGRAMS:
#Made by Piyush Shukla
# check if the number is positive or negative or zero using
if elif .
INPUT
num = float(input(“enter a number”))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
OUTPUT
PROGRAM NO.2
#Made by Piyush Shukla
#to find the factorial of a number
INPUT
n=int(input("Enter number:"))
fact=1
while(n>0):
fact=fact*n
n=n-1
print("Factorial of the number is: ")
print(fact)

OUTPUT
PROGRAM NO.3
#Made by Piyush Shukla
#to convert a numerical grade to a letter grade

INPUT
score=int(input('enter score'))
if score >= 90:
letter = 'A'
else: # grade must be B, C, D or F
if score >= 80:
letter = 'B'
else: # grade must be C, D or F
if score >= 70:
letter = 'C'
else: # grade must D or F
if score >= 60:
letter = 'D'
else:
letter = 'F'
print('grade is',letter)

OUTPUT
PROGRAM NO.4
#Made by Piyush Shukla
#to check if the input year is a leap year or not
INPUT
year = int(input("Enter a year: "))

if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
OUTPUT
PROGRAM NO.5
#Made by Piyush shukla
#find the largest number among the three input numbers
using logical operator
INPUT
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is",largest)
OUTPUT
PROGRAM NO.6
#Made by Piyush Shukla
#to find the LCM of two numbers
INPUT
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
if(a>b):
min1=a
else:
min1=b
while(1):
if(min1%a==0 and min1%b==0):
print("LCM is:",min1)
break
min1=min1+1
OUTPUT
PROGRAM NO.7
#Made by Piyush Shukla
#to count the number of digits in a number
INPUT
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the
number are:",count)
OUTPUT
PROGRAM NO.8
#Made by Piyush Shukla
#to print the table of a given number
INPUT
n=int(input("Enter the number to print
the tables for:"))
for i in range(1,11):
print(n,"x",i,"=",n*i)
OUTPUT
PROGRAM NO.9
#Made by Piyush Shukla
#Fibonacci Series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
INPUT
Number = int(input("Please Enter the Range Number: "))
# Initializing First and Second Values of a Series
First_Value = 0
Second_Value = 1
# Find & Displaying Fibonacci series
for Num in range(0, Number):
if(Num <= 1):
Next = Num
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next, end=',')
print('....')
OUTPUT
PROGRAM NO.10

#Made by Piyush Shukla


#check prime no
INPUT
number = int(input("Enter any number: "))
# prime number is always greater than 1
if number > 1:
for i in range(2, number):
if (number % i) == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")
# if the entered number is less than or equal to 1
# then it is not prime number
else:
print(number, "is not a prime number")

OUTPUT
PROGRAM NO.11
#Made by Piyush Shukla
#Python program to display bar chart with following
data prog_lanugages = ('Python', 'C++', 'Java', 'Perl',
'C', 'Lisp') performance = [10,7,6,4,2,1]
INPUT
import matplotlib.pyplot as plt
import numpy as np
prog_lanugages = ('Python', 'C++', 'Java', 'Perl', 'C', 'Lisp')
y_pos = np.arange(len(prog_lanugages))
performance = [10,7,6,4,2,1]
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, prog_lanugages)
plt.ylabel('Usage')
plt.title('Programming language usage') plt.show()

OUTPUT
PROGRAM NO.12
#Made by Piyush Shukla
#Python program to display pie chart with following
data runs = [77,22,42,103] players = ['Rohit
sharma','MS dhoni','Jasmit bumrah','Virat kohli']
INPUT
import matplotlib.pyplot as plt
runs = [77,22,42,103]
players = ['Rohit sharma','MS dhoni','Jasmit bumrah','Virat kohli']
cols = ['c','m','r','b']
plt.pie(runs, labels=players, colors=cols, startangle=90, shadow= True,
explode=(0,0.1,0,0), autopct='%1.1f%%')
plt.title('Runs Scored Chart')
plt.show()

OUTPUT
PROGRAM NO.13
#Made by Piyush Shukla
#LINEAR SEARCH
INPUT
def search(arr, n, x):

for i in range (0, n):


if (arr[i] == x):
return i
return -1

# Driver Code
arr = []
j=int(input('how many number you want to add in list:'))
for i in range(0 , j):
k=int(input('enter your number:'))
arr.append(k)
print('your list is',arr)
x = int(input('enter number you want to search:'))
n = len(arr)
result = search(arr, n, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)
OUTPUT
PROGRAM NO.14
#Made by Piyush Shukla
#BINARY SEARCH

INPUT
def Binary_search(arr,start_index,last_index,element):
while (start_index<= last_index):
mid =int((start_index+last_index)/2)
if (element>arr[mid]):
start_index = mid+1
elif (element<arr[mid]):
last_index = mid-1
elif (element == arr[mid]):
return mid
return -1
arr = []
j=int(input('how many number you want to add in list:'))
for i in range(0 , j):
k=int(input('enter your number:'))
arr.append(k)
element = int(input('enter number you want to search:'))
start_index = 0
last_index = len(arr)-1
found = Binary_search(arr,start_index,last_index,element)
if (found == -1):
print ("element not present in array")
else:
print ("element is present at index " + str(found))
OUTPUT
PROGRAM NO.15
#Made by Piyush Shukla
#Recursive function to add all numbers in a list.
INPUT
def sum(list):
if len(list) == 1:
return list[0]
else:
return list[0] + sum(list[1:])
arr = []
j=int(input('how many number you want to add in list:'))
for i in range(0 , j):
k=int(input('enter your number:'))
arr.append(k)
print('your list is',arr)
print('addition of element in list is:',sum(arr))
OUTPUT
PROGRAM NO.16
#Made by Piyush Shukla
#Program in python to write a text file
INPUT
f=open("project.txt","w")
s=f.write('''This is a python project
made by PIYUSH SHUKLA
of class XII-A for the session 2019-20''')
print('written successfully')
f.close()
OUTPUT
On Python IDLE

On Notepad
PROGRAM NO.17
#Made by Piyush Shukla
#To create a table using mysql.connector and describe it.
INPUT
import mysql.connector as sql
mycon= sql.connect(host = 'localhost' , user ='root',
password='', database= 'project')
cursor= mycon.cursor()
cursor.execute('drop table if exists student’)
cursor.execute('''create table student
(STU_ID int,
NAME VARCHAR(30),
GENDER CHAR(1),
CLASS VARCHAR(3))''')
print('table created succesfully')
mycon.close()
OUTPUT
On Python IDLE

On Mysql
PROGRAM NO.18
#Made by Piyush Shukla
#To insert data into table ‘student’.
INPUT
import mysql.connector as sql
mycon= sql.connect(host = 'localhost' , user ='root',
password='', database= 'project')
cursor= mycon.cursor()
st='''insert into student (STU_ID,NAME, GENDER, CLASS)
VALUES({},'{}','{}','{}')'''.format(101,'ANURAAG','M','XIA')
cursor.execute(st)
mycon.commit()
print('data inserted succesfully')
mycon.close()
OUTPUT
On Python IDLE

On Mysql
PROGRAM NO.19
#Made by Piyush Shukla
#To UPDATE data in table ‘student’.
INPUT
import mysql.connector as sql
mycon= sql.connect(host = 'localhost' , user ='root',
password='', database= 'project')
cursor= mycon.cursor()
st='''UPDATE student SET STU_ID={}
where STU_ID={}'''.format(102,101)
cursor.execute(st)
mycon.commit()
print('data UPDATED succesfully')
mycon.close()
OUTPUT
On Python IDLE

On Mysql
PROGRAM NO.20
#Made by Piyush Shukla
#To display first three rows fetched from ‘student’ table.
INPUT
import mysql.connector as sql
mycon= sql.connect(host = 'localhost' , user ='root',
password='', database= 'project')
cursor= mycon.cursor()
cursor.execute('select * from student')
data = cursor.fetchmany(3)
count= cursor.rowcount
for row in data:
print(row)
mycon.close()
OUTPUT

You might also like