Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PYTHON Record - New

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

ARITHMEIC OPERAION

AIM
To perform a python program for the arithmetic operations using tow integers.

CODING
a=int(input(‘enter the number 1:’))
b=int(input(‘enter the number 2:’))
add=a+b
sub=a-b
mul=a*b
div=a%b
modules=a/b
quiotent=a//b
power=a**b
print(‘ARITHMETIC OPERATION’)
print(‘Addition of ‘a ‘and’b’is:’,add)
print(‘subtraction of ‘a ‘and’b’is:’,sub)
print(‘Multiplication of ‘a ‘and’b’is:’,mul)
print(‘Division of ‘a ‘and’b’is:’,div)
print(‘Modules of ‘a ‘and’b’is:’,modules)
print(‘quiotent of ‘a ‘and’b’is:’,quiotent)
print(‘Power of ‘a ‘and’b’is:’,power)

OUTPUT
enter the number 1: 5
enter the number 2: 2
ARITHMETIC OPERATION
Addition of 5 and 2 is: 7
Subtraction of 5 and 2 is: 3
Multiplication of 5 and 2 is: 10
Division of 5 and 2 is: 2
Modules of 5 and 2 is: 1
Quotient of 5 and 2 is: 2
Power of 5 and 2 is: 25
FINDING POSITIVE OR NEGATIVE NUMBER

AIM
To perform a python program for finding the number positive or negative.

CODING

Num=int(input(“Enter anumber:”))
ifNum>0:
print(“The Given Number is Positive”)
elifNum==0:
print(“Zero”)
else:
print(“The Given Number is Negative”)

OUTPUT 1:
Enter the Number: 1
The Given Number is Positive

OUTPUT 2:
Enter the Number: -9
The Given Number is Negative
BRANCHING STATEMENT IN PYTHON

AIM
To perform a python program for branching statement.

CODING
a) If else :

n=int(input("Enter a number:"))
if n%2==0:
print(n,"is even")
else:
print(n,"is odd")

OUTPUT

Enter a number:6
6 is even
Enter a number:3
3 is odd

b) If elif:

Num=int(input("Enter anumber:"))
if Num>0:
print("The Given Number is Positive")
elif Num==0:
print("Zero")
else:
print("The Given Number is Negative")
OUTPUT

Enter anumber:5
The Given Number is Positive

Enter anumber:-3
The Given Number is Negative
LOOPING STATEMENT IN PYTHON

AIM
To perform a python program for looping statement.

CODING

a) For loop :
example 1:
colors=["red","green","yellow","black"]
for x in colors:
print(x)

OUTPUT

example 1:
red
green
yellow
black

example 2:
print ("Example 2 ")
s= "blue"
for x in s:
print(x)
OUTPUT
Example 2
b
l
u
e

example 3:
print ("Example 3 ")
i=1
for i in range (5):
print (i)

OUTPUT
Example 3
0
1
2
3
4

example 4:
print ("Example 4 ")
for i in range (2):
i=int(input("enter the number 1:"))
print(i)
OUTPUT
Example 4
enter the number 1:8
8
enter the number 1:15
15
TO FIND THE SUM OF ARRAY

AIM
To perform a python program for finding a sum of array.

CODING

arr=[ ]
arr=[10,3,2,15]
ans=sum(arr)
print(“Sum of the Array is:”,ans)

OUTPUT

Sum of the Array is: 30


SLICING STATEMENT IN PYHTON

AIM
To perform a python program for slicing statements.

CODING

a) String:
my_str = "Javatpoint"
slice1 = slice(0,10,3)
slice2 = slice(-1,0,-3)
result1 = my_str[slice1]
result2 = my_str[slice2]
print("Result 1:",result1)
print("result 2:",result2)

OUTPUT
Result 1: Jaot
Result 2: toa

b) Tuple:

tup = (45,68,955,1214,41,558,636,66)
slice1= slice(0,10,3)
slice2= slice(-1,0,-3)
result1 = tup[slice1]
result2 = tup[slice2]
print("Result 1:", result1)
print("Result 2:", result2)
OUTPUT

Result 1: (45, 1214, 636)


Result 2: (66, 41, 68)
ADDITION OF TWO MATRIXES

AIM
To perform a python program for Addition of two matrixes.

CODING

X=[[12,7,3]],
[4,5,6],
[7,8,9]]
Y=[[5,8,1]],
[6,7,3],
[4,5,9]]
result=[[0,0,0]],
[0,0,0],
[0,0,0]]
print(“The Addition of two matrix is:”)
#iterate through rows
fori in range(len(X)):
#iterate through columns
for j in range(len(X[0])):
result[i][j]=X[i][j]+y[i][j]
for r in result:
print(r)

OUTPUT

The Addition of two matrix is:


[17,15,4]
[10,12,9]
[11,13,18]
ILLUSTRATION OF LIST OPERATIONS

AIM
To perform a python program for list operations.

CODING

l1=[1,2,"python",3.14]
l2=[4.5,8,10,"hello","world"]
print('l1 =',l1)
print('l2 =',l2)
#concatenation
print('\n concatenation')
l3=l1+l2
print(l3)

#length
print('\n Length')
print(len(l2))

#indexing
print('\n indexing')
print((l3)[3])

#slicing
print('\n slicing')
print((l3)[3:-1])

#repeating
print('\n repeating')
print(2*l2)
OUTPUT

l1 = [1, 2, 'python', 3.14]


l2 = [4.5, 8, 10, 'hello', 'world']

concatenation
[1, 2, 'python', 3.14, 4.5, 8, 10, 'hello', 'world']

Length
5

indexing
3.14

slicing
[3.14, 4.5, 8, 10, 'hello']

repeating
[4.5, 8, 10, 'hello', 'world', 4.5, 8, 10, 'hello', 'world']
ILLUSTRATION OF TUPLES OPERATIONS

AIM
To perform a python program for tuples operations.

CODING

t1=(1,2,3,"python")
t2=(4,5,"hello","world")
print('t1 =',t1)
print('t2 =',t2)

#concatenation
print('\n concatenation')
t3=t1+t2
print(t3)

#length
print('\n Length')
print(len(t3))

#indexing
print('\n indexing')
print((t3)[3])

#slicing
print('\n slicing')
print((t3)[4:6])

#repeating
print('\n repeating')
print(2*t2)
OUTPUT

t1 = (1, 2, 3, 'python')
t2 = (4, 5, 'hello', 'world')

concatenation
(1, 2, 3, 'python', 4, 5, 'hello', 'world')
Length
8
indexing
python
slicing
(4, 5)
repeating
(4, 5, 'hello', 'world', 4, 5, 'hello', 'world')
ILLUSTRATION OF STRING OPERATIONS

AIM
To perform a python program for string operations.

CODING

st1="HELLO "
st2="HELLO WORLD"
print(st1)
print(st1[0])
print(st1[1])
print(st1[2])
print(st1[3])
print(st1[4])
#using for
print (" \n indexing using for")
i=1
for i in range (5):
print(st1[i])
#length
print('\n Length')
print(len(st2))
#concatenation
print('\n concatenation')
st3=st1+st2
print(st3)

#slicing
print('\n slicing')
print((st3)[5:])

#repeating
print('\n repeating')
print(2*st1)
OUTPUT

HELLO
H
E
L
L
O

indexing using for


H
E
L
L
O

Length
11

concatenation
HELLO HELLO WORLD

slicing
HELLO WORLD

repeating
HELLO HELLO
DATE AND TIME

AIM
To perform a python program for date and time.

CODING
a)
from datetime import datetime
now = datetime.now()
print("now=",now)
dt_string = now.strftime("%d/%m/%y %h:%m:%s")
print("date and time = ",dt_string)

OUTPUT

now= 2023-11-26 11:56:53.841068

b)
form datetimeimport date
today = date.today()
d1 = today.strftime("%d/%m/%y")
print("d1 = ",d1)
d2 = today.strftime("%m,%d,%y")
print("d2 = ",d2)
d3 = today.strftime("%m/%d/%y")
print("d3 = ",d3)
d4 = today.strftime("%m-%d-%y")
print("d4 = ",d4)
OUTPUT

d1 = 26/11/23
d2 = 11,26,23
d3 = 11/26/23
d4 = 11-26-23
STUDENT GRADE CALCULATION

AIM
To perform a python program for Student grade calculaton.

CODING

print(“Student Mark Calculation”)


m1=int(input(“Enter the mark1:”))
m2=int(input(“Enter the mark2:”))
m3=int(input(“Enter the mark3:”))
m4=int(input(“Enter the mark4:”))
m5=int(input(“Enter the mark5:”))
sum=m1+m2+m3=m4+m5
avg=(m1=m2+m3+m4+m5)/5
print(“Total is “,sum)
print(“Average is”,avg)
if(avg>=90):
print(“Grade A”)
elif(avg>=80 and avg<90)
print(“Grade B”)
elif(avg>=70 and avg<80):
print(“Grade C”)
elif(avg>=60 and avg<70):
print(“Grade F”)
OUTPUT

Student Mark Calculation


Enter the mark1: 89
Enter the mark2: 89
Enter the mark3: 78
Enter the mark4: 67
Enter the mark5: 98
Total is 421
Average is 84
Grade B
USAGE OF FUNCTION

AIM
To perform a python program for usage of function.

CODING

print(“USAGE OF FUNCTION”)
def rectangle(w,h):
rect=w*h
print(“\n Area of a rectangle is:”,rect)
def perimeter(w,h):
peri=2*(w+h)
print(“\n Perimeter of a rectangle is:”,peri)
def square(a):
squ=a*a
print(“\n Area of square is:”,squ)
def circle(r):
cir=3.14*r*r
print(“\n Area of circle is:”,cir)
rectangle(6,4)
perimeter(6,4)
square(4)
circle(4)

OUTPUT

USAGE OF FUNCTION
Area of a rectangle is: 24
Perimeter of a rectangle is: 20
Area of square is: 16
Area of circle is: 50.24
RECURSIVE FUNCTION IN PYHTON

AIM
To perform a python program for recursive function.

CODING

example a:
def factorial(x):
if x==1:
return 1

else:
return(x*factorial(x-1))
num=2
print(" the factorial of",num," is ",factorial(num))

OUTPUT

the factorial of 2 is 2

example b:
deftri_recursion(k):
if (k>0):
result=k+tri_recursion(k-1)
print(result)
else:
result=0
return result
print("\n\n RECURSION EXAMPLE RESULT")
tri_recursion(6)
OUTPUT

RECURSION EXAMPLE RESULT


1
3
6
10
15
21

example c:
defrecursive_funtional(n):
if n==1:
return n
else:
return n * recursive_funtional(n-1)
num=6
ifnum<0:
print(" INVALID INPUT")
elifnum ==0:
print("factorial of 0 is 1")
else:
print(" factoriqal of number",num,"=",recursive_funtional(num))

OUTPUT

factoriqal of number 6 = 720


CLASS AND OBJECT IN PYTHON

AIM
To perform a python program for class and object.

CODING

example a:
class bike:
name=" "
gear=0
bike1=bike()
bike1.gear=11
bike1.name="Mountain bike"
print(f"name:{bike1.name},gear:{bike1.gear} \n")

OUTPUT

name:Mountain bike,gear:11

example b:

class bike:
name=" "
gear=0
bike1=bike()
bike1.gear=11
bike1.name="Mountain bike"
print(f"name:{bike1.name},gear:{bike1.gear} \n")
bike2=bike()
bike2.gear=14
bike2.name="r15"
print(f"name:{bike2.name},gear:{bike2.gear} ")
OUTPUT

name:Mountain bike,gear:11
name:r15,gear:14
example c:

class room:
length=0.0
breadth=0.0
defcalculate_area(self):
print("area of length",self.length*self.breadth)
studyroom=room()
studyroom.length=42.5
studyroom.breadth=12.5
studyroom.calculate_area()

OUTPUT
area of length 531.25
USAGE OF DICTIONARY
AIM
To perform a python program for usage of dictionary.

CODING

print(“Usage of Dictionary”)
animals={ }
animals={3:”monkey”,1:”tuna”,2:”giraffe”}

#Sort function is used


keys=sorted(animals.key( ))
print(keys)

#len function is used


print(“Length:”,len(animals))
keys=animals.keys( )
values=animals.values( )

print(“Keys:”)
print(keys)
print(len(keys))

print(“values:”)
print(values)
print(len(values))
#use not in.
if “tuna”not is animals:
print(“Has tuna”)
else:
print(“No tuna”)

#use is on nonexistent key.


if “elephant” in animals:
print(“Has elephant”)
else:
print(“No elephant”)

#Update first dictionary with second.


Animals.update(tuna=3)

#Display both dictionaries.


Print(animals)

#To add new item.


Animals[“Tiger”]=5
Print(animals)

OUTPUT

Usage of Dictionary
[1,2,3]
Length:3
Keys:
dict_keys([3,1,2])
3
Values:
dict_values([‘monkey’,’tuna’,’giraffe’])
3
Has tuna
No elephant
{3:’monkey’1:’tuna’2:’giraffe’,’tuna’:3}
{3:’monkey’1:’tuna’2:’giraffe’,’tuna’:3,’Tiger’:5}
READING A TEXT FILE
AIM
To perform a python program for reading a text file.

CODING

#opening the text file


With open (‘sample.txt’,’r’) as file:
#reading each line
for line in file:
#reading each word
for word in line.split( ):
#displaying the words
print(word)
NOTEPADE FILE
OUTPUT
Welcome
to
python
program
WRITING A TEXT FILE

AIM
To perform a python program for Writing a text file.

CODING

while True:
print(“Enter ‘x’ for exit.”)
filename=input(“Enter file name to create and write content:”)
if filename==’x’:
break
else:
c=opean(filename,”w”)
print(“\n The file,”,filename,”created successfully!”)
print(“Enter 3 sentence to write on the file:”)
sent1=input( )
sent2=input( )
sent3=input( )
c.write(sent1)
c.write(“\n”)
c.write(sent2)
c.write(“\n”)
c.write(sent3)
c.close( )
print(“Content successfully placed inside the file.!!\n”)
OUTPUT

Enter’x’for exit.
Enter file name to create and write content:myfile

The file,myfilereated successfully!


Enter the 3 sentences to write on the file:
hi all
how are you
hope you all fine
Content successfully placed inside the file.!!

NOTEPAD WINDOW
WEB SCRAPING IN PYTHON

AIM
To perform a python program for web scraping.

CODING

import requests
from bs4 import BeautifulSoup

# Making a GET request


r = requests.get('https://www.geeksforgeeks.org/python-programming-language/')

# check status code for response received


# success code - 200
print(r)

# Parsing the HTML


soup = BeautifulSoup(r.content, 'html.parser')
print(soup.prettify())

OUTPUT

https://www.geeksforgeeks.org/python-programming-language/
200
DATA VISUALIZATION

AIM
To perform a python program for web scraping.

CODING

example a:
import pandas as pd
import matplotlib.pyplot as plt

# reading the database


data = pd.read_csv("tips.csv")

# Scatter plot with day against tip


plt.scatter(data['day'], data['tip'], c=data['size'],
s=data['total_bill'])

# Adding Title to the Plot


plt.title("Scatter Plot")

# Setting the X and Y labels


plt.xlabel('Day')
plt.ylabel('Tip')

plt.colorbar()

plt.show()
OUTPUT

example b:
import pandas as pd
importmatplotlib.pyplot as plt

# reading the database


data = pd.read_csv("tips.csv")

# Scatter plot with day against tip


plt.plot(data['tip'])
plt.plot(data['size'])

# Adding Title to the Plot


plt.title("Scatter Plot")

# Setting the X and Y labels


plt.xlabel('Day')
plt.ylabel('Tip')
plt.show()

Output:

You might also like