0% found this document useful (0 votes)
3 views4 pages

Python Class Examples

The document includes various Python code snippets demonstrating the use of mathematical functions, data manipulation with pandas, exception handling, class inheritance, and database operations with MySQL. It also features a simple Flask web application for task management with HTML for rendering tasks. Overall, it showcases fundamental programming concepts and libraries in Python.
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)
3 views4 pages

Python Class Examples

The document includes various Python code snippets demonstrating the use of mathematical functions, data manipulation with pandas, exception handling, class inheritance, and database operations with MySQL. It also features a simple Flask web application for task management with HTML for rendering tasks. Overall, it showcases fundamental programming concepts and libraries in Python.
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/ 4

import math

print(math.pi)
print(math.nan)

print(math.floor(3.54))
print(math.ceil(3.54))
print(math.sqrt(16))

print(math.sin(90))
print(math.cos(90))

print(math.log(10))
print(math.log10(10))

print(math.pow(3,3))
print(math.isinf(45))

import pandas as pd
data={'name':["john", "anjali"], 'marks':[79,80], 'grade':[7, 7] }
df=pd.DataFrame(data, index=['s1','s2'] )
print(df)

data=[89,79,67]
ser=pd.Series([10.9,20.7,30.6,40.5])
print(ser)

print(df.info())

import numpy as np
data=np.array([[10,20],[30, 40]])
np.ndim(data)

file=open('data.txt', 'r')
print(data.read())

n1=int(input("enter no1:"))
n2=int(input("enter no2:"))
try:
res=n1/n2
except:
print("Please donot enter 0 for n2")
else:
print(res)
finally:
#resource close code
print("This finally block")

def check_positive(no):
if no<0:
raise ValueError("The value error.")
else:
return no
try:
print(check_positive(-1))
except TypeError:
print("the exception generate")
except ValueError:
print("the ValueError exception generate")
except:
print("the ValueError exception generate")

try:
n1=int(input("enter no1:"))
n2=int(input("enter no2:"))
res=n1/n2
except (ValueError, ZeroDivisionError) as e :
print("Please donot enter 0 for n2")
else:
print(res)
finally:
#resource close code
print("This finally block")

class vehical:
def __init__(self, nowheel, engine):
self.nowheel=nowheel
self.engine=engine
def vehical_description(self):
print("The vehical :",self.nowheel," Engine:", self.engine)
def start(self):
print("vehical is starting")
def stop(self):
print("vehical is stopped")

class car(vehical):
def __init__(self, color, model, speed):
self.color=color
self.model=model
self.speed=speed
self.__no=34

def get_no(self):
return self.__no
def set_no(self, number):
self.__no=number

def car_description(self):
print("The car color is :",self.color," model:", self.model," and speed:
",self.speed)
def start(self):
print("car is starting")
def stop(self):
print("car is stopped")
def accelarate(slef):
print("car is runnig")

c1=car('Red', 'Sedan', 120)


c1.car_description() #car_description(c1)
c2=car('Blue', 'SUV', 100)
c2.car_description() #car_description(c2)
c2.start()
print(c2.set_no(100))
print(c2.get_no())
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="bismillah",
database="studentdb"
)
mycursor = mydb.cursor()
#mycursor.execute("insert into student values(11,'Amit', 7, 101)")
#mycursor.execute("update student set gade='5' where sid=11")
mycursor.execute("delete from student where sid=11")
mydb.commit()
mycursor.execute("select * from student")
myresult = mycursor.fetchall()

for x in myresult:
print(x)
mydb.close()

from flask import Flask, render_template, request, redirect, url_for

app=Flask(__name__)

tasks=[]
@app.route('/dispTask')
def dispTask():
return render_template('display.html', tasks=tasks)

@app.route('/addTask', methods=['POST'])
def addTask():
task = request.form['task']
tasks.append(task)
return redirect(url_for('dispTask'))

if __name__ == '__main__':
app.run(debug=True)

<!Doctype>
<html>
<head>
<title>First Flask App</title>
</head>
<body>
<h2>Tasks</h2>
<ul>
{% for task in tasks %}
<li>{{ task }} <input type="checkbox" name="completed"></li>
{% endfor %}
</ul>
<form action="/addTask" method="POST">
<input type="text" name="task">
<input type="submit" value="Add Task">
</form>
</body>
</html>

You might also like