Python Class Examples
Python Class Examples
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")
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()
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>