Professional Documents
Culture Documents
Advantage:
1.Easy to Learn
2.Less code
3-4 lines in java
3.Reduce time
4.Interpreter language(line by line)
5.Huge Library
pygame
SQlite
DJango
Numpy
scipy
6.Open source
7.Python is platform Independent
Learn:
1.Bank Application
2.Billing software
3.Ticket Reservation
4.Hotel MS
..............
software:
Download:
https://www.python.org/downloads/windows/
python IDLE:
Java:
class Anu{
public static void main(String args[]){
Sysytem.put.println("Welcom");
}
}
Python:
print("Welcome")
2.Script mode:
-GO to the file menu
-click new file>script mode will open
-type program>go to file > save as
-.py is the extension to save the file
Python Variables:
variable is used as identifier Location
a=10
variable name is a
value = 10
b=10
aneri=5
syntax:
a=20
2.FLoat:float
it will be having all the decimal values
1.2
2.3
4.5
9.0001
syntax:
a=3.111104
3.String:str
collection of character
" "
or
' '
syntax:
a="welcome you all"
or
a="Python course"
len(a)=5
a="JiTHU"
lower()
a=20
b="My age is:{}"
print(b.format(a))
a=5
b=4
c="I want{} cup of icecream of {} items"
print(c.format(a,b))
a="hello"
capitalize()
a="hello"
print(a.capitalize())
4.Boolean:
true
or
false
keywords in python:
predefined reserved words in python
we can't able to use keywords for defining var,class,function....
if
if-else
False
True
and
or
not
def
class
excpet
elif
None
raise
except
from
import...................................
Python Operators:
it is going to do some operation
1.Aritchmetic oper:
all the mathematical related operations
add +
sub -
mul *
div /
floor div //
modulus -remainder of division %
Exponential-power **
a=4
b=3
print("Exp value is:",a**b)
Equal ==
Not Equal !=
Greater >
Lesser <
Greatr or equal >=
lesser or equal <=
3.Logical oper:Logically and give output in boolean format
t-t=t
t-f=f
f-t=f
f-f=f
a=True
b=True
print(a and b)
a=3
print(a>5 and a<1)
Logical OR:or
when any one is true and one is false----true
t-t=t
t-f=t
f-t=t
f-f=f
a=False
b=True
print(a or b)
Logical NOT:
reverse or oppe
a=True
print(not(a))---false
4.Bitwise oper:
bitwise AND:&
1-1=1
1-0=0
0-1=0
0-0=0
a=10
b=24
a=10
b=24
print(a & b)
c=30
d=24
print(c & d)
bitwise OR: |
1-1=1
1-0=1
0-1=1
0-0=0
a=12
b=5
bitwise NOT: ~
aneri=5
print(~aneri)
-6
a=-9
print(~a)
5.Assignment oper:
naj=10
anu=10
>>> print(anu)
10
>>> anu +=10
>>> print(anu)
20
>>> anu -=10
>>> print(anu)
10
>>> anu *=3
>>> print(anu)
30
>>> anu /=5
>>> print(anu)
6.0
>>>
6.Membership oper:
in
not in
aneri=["apple","orange","grapes","banana","cherry"]
print("banana" in aneri)
output-Bool
school=["anu","aneri","najila","jithu","arun"]
print("aneri" in school)
7.Identity oper:
used to check the given data is similar or not
is
is not
a=["apple","Banana"]
b=["Grapes"]
a=["apple","orange"]
a=["apple"]
print(a is not a)
a=["apple","orange"]
a=["orange"]
print(a is a)
a=10
b=20
if a<b:
print("Greater")
username="anupriyan"
password="anu12345"
username="anupriyan"
password="anu12345"
username=input("Enter a Username:")
password=input("Enter a password:")
username=input("Enter a Username:")
password=input("Enter a password:")
else:
print("Invalid credintial")
----nested if:
within a if statement onemore if statement
a=20
if a>10:
print("a is greater")
if a >25:
print("15 is greater")
else:
print("number is lesser")
a=150
if a<100:
print("lesser")
if a==75:
print("equal")
else:
print("greater")
2.Looping Statement:Repetation
---for loop:
it is an iterator used to print a data in sequqnce order
a=["apple","orange","grapes","cherry"]
for fruits in a:
print(fruits)
for a in range(1,10):
print(a)
---while loop:
a=1
while a<10:
print(a)
a+=2
--break in for:
a=[1,2,3,4,5,6]
a=4
break
for i in range(20):
if i==12:
break
print(i)
--continue:
for a in "Aneri patel":
if a=="p":
continue
print(a)
for a in range(10):
if a==5:
continue
print(a)
a=1
while a<20:
print(a)
if a==5:
break
a+=1
a=1
while a<20:
a+=1
if a==5:
continue
print(a)
--pass:
for a in [1,2,3,4,5]:
if a==3:
pass
print("this is pass block")
print(a)
function in python:
-perform some task
--break the modules into groups
--code reuse
syntax:
def show(arg):
.......................
function call:
def anu():
print("Hello Everyone")
anu()
function arg:
def anu(name):
print("my name is:"+name)
anu("shaji")
anu("aneri")
def add(a,b):
c=a+b
print("added value is:",c)
add(4,5)
def sub(a,b,c):
d=a-b-c
print("Sub value is:",d)
sub(9,3,2)
def anu():
a=5
print("inside value is:",a)
a=10
anu()
print("outside valus is :",a)
Types of arguments:
1.Keyword argument:
syntax:
def demo(arg):
.....................print............
demo(values)
2.Default argument:
def anu(name, msg="Welcome you"):
print("Hello", name, msg)
anu("")
3.Arbitary arguments:
def demo(*fruits):
for a in fruits:
print("i like:", a)
demo("apple","orange","grapes","banana")
factorial:
def demo(a):
if a==1:
return 1
else:
return(a *demo(a-1))
num=int(input("Enter a number:"))
print("the factorial is:",demo(num))
Fibonnocie series:
Lambda Function:
it will have many arguments but only one expression
syntax:
lambda arg:expression
anu=lambda a:a+5
print(anu(5))
filter:
demo=[1,6,8,5,11,12,4,3,16,18,21,22]
ans=list(filter(lambda a:(a%2!=0),demo))
print(ans)
String literal:
anna---anna
bb--bb
malayalam--
demo=["aneri","anna","bb","malayalam","jithu","najila"]
ans=list(filter(lambda a:(a=="".join(reversed(a))),demo))
print(ans)
anagrams---common chaar
aneri
einra
map in lambda:
a=[2,4,6]
b=[3,5,7]
ans=map(lambda x,y:x+y,a,b)
print(list(ans))
Data structure:
data-collection of information
structure- correct view
syntax:
a=[ 1,2,3,4]
properties:
1.mutable behaviour:update, add, delete.......
len
index
a=[1,2,3,4,5,6,7,8,9,10]
a.clear()
print(a)
a=["Jithu","aneri","najila"]
a[2]="anu"
print(a)
syntax:
a=( 1,2,3,4,5)
a=("anu")
len
count
index
slice
3.Set
store the data in unorder way
syntax:
a={ 1,2,3,4,5,6,7}
set operations:
1.set union:
a={1,2,3,4,5}
b={4,5,6,7}
a.union(b)
2.set intersection:
a={1,2,3,4,5,6,7,8,9}
b={5,6,7,8,9,10,11,12}
print(a.intersection(b))
3.set difference:
a={1,2,3,4,5,6,7,8,9}
b={5,6,7,8,9,10,11,12}
print(a.difference(b))
4.symmetric difference:
a={1,2,3,4,5,6,7,8,9}
b={5,6,7,8,9,10,11,12}
print(a.symmetric_difference(b))
a={1,2,3,4,5,6,7,8,9}
print(len(a))
a={1,2,3,4,5,6,7,8,9}
a.add(10)
print(a)
a={1,2,3,4,5,6,7,8,9}
a.update([10,11,13,12])
print(a)
a={1,2,3,4,5,6,7,8,9}
a.remove(5)
print(a)
a={1,2,3,4,5,6,7,8,9}
a.pop()
print(a)
a={1,2,3,4,5,6,7,8,9}
a.discard(9)
print(a)
4.Dictionary:
unorder collection of data
syntax:
a={1:"anu", 2:"najila", 3:"aneri"}
a={1:"anu", 2:"aneri"}
a.pop(1)
print(a)
a={1:"anu", 2:"aneri"}
a.clear()
print(a)
add,pop
modules:
file .py ....modules
import aneri
aneri.function()
import modulename
print
Packages:
collection of module is called package
1.Basic example
eg:Games
File system:
we are going to do the file Handling
open
read - r
a=open("D:/open.txt","r")
print(a.readline())
print(a.readline())
print(a.readline())
append: a
a=open("D:/open.txt","a")
a.write(" I hope you are fine and guod. welcome you all")
a.close()
Errros:
Syntax error
attribute error
value
type error
index error
indent error
name error
zero div error
raise runtime error
assertion error.........
try-
program if we write inside the try block
-check whether the prg a any error not
except-handle a error
except nameerror---
except Typeerror---
Example 1:
try:
a=int(input("Enter a 1st number:"))
b=int(input("Enter a 2nd number:"))
c=a/b
print(c)
except ZeroDivisionError:
print("can't div by zero")
Example 2:
try:
a=int(input("Enter a 1st number:"))
b=int(input("Enter a 2nd number:"))
c=a/b
print(c)
except ZeroDivisionError:
print("can't div by zero")
except ValueError:
print("enter number alone")
finally:
print("bye......")
Example 3:
try:
f=open("D:/oooopen.txt","r")
print(f.read())
except IOError:
print("file not found")
else:
print("file is opened successfullyy")
f.close()
Example 4:
try:
a=int(input("Enter a age:"))
if(a<18):
raise ValueError
else:
print("you have rights to vote")
except ValueError:
print("not eligible to vote")
Example 5:
try:
a=int(input("Enter a age:"))
if(a<=0):
raise ValueError
else:
print("correct ")
except ValueError:
print("enter +ve number")
Oops in python:
object oriented programming language
Adv:
1. reuse
2.Easy to create any app
3.secure
-class
-object
-inheritance
-polymorphism
-Abstraction
-Encapsulation
1.Class:
it is like a blueprint, class as attributes and behaivours
Human----class
attr-name.,age,gender,height,weight,addres,........
behiv-run,sing,dance,walk,eat......................
syntax:
class Aneri
......................
....................
2.Object:
-object is an instance of class
-obj as entity that have state and behaviour
syntax:
obj=classname()
Ex 1:
class Human:
def eat(self):
print("Eating")
anu=Human()
anu.eat()
Ex 2:
class Emp:
def __init__(self,name,address):
self.name=name;
self.address=address;
def show(self):
print(self.name,self.address)
najila=Emp("Aneri","India")
najila.show()
3.Inheritance:
class A:
........eat........
class B(A):
......run........
obj=B()
obj..
run
eat
class Aneri:
def eat(self):
print("eating..")
class Jithu(Aneri):
def run(self):
print("running")
obj=Jithu()
obj.eat()
obj.run()
class A
....eat.....
class B
.......run.....
class C(A,B)
.........sleep......
obj=C()
class Aneri:
def eat(self):
print("eating..")
class Jithu:
def run(self):
print("running")
class Najila(Aneri,Jithu):
def sleep(self):
print("sleeping")
obj=Najila()
obj.eat()
obj.run()
obj.sleep()
3.Multilevel Inheritance:
inherit using level by level
class A:
...eat.....----------------eat
class B(A):
....sleep....-----------eat,sleep
class C(B):
........run....--------------eat,sleep,run
class D(C):
.........jump......------eat,sleep,run,jump
obj=D()
class Aneri:
def eat(self):
print("eating..")
class Jithu(Aneri):
def run(self):
print("running")
class Najila(Jithu):
def sleep(self):
print("sleeping")
class Anu(Najila):
def lazy(self):
print("Lazy")
obj=Anu()
obj.eat()
obj.run()
obj.sleep()
obj.lazy()
4.Hybrid inheritance:
similar to multiple inheritance
polymorphism:
poly-many
perform many task
def add(a,b)
def add(a,b,c)
add(3,4,3)
class Najila:
def eat(self):
print("najila eating")
class Jithu:
def eat(self):
print("jithu eating")
obj=Najila()
obj.eat()
operator overloading:
same operator perform many task
print(4 * "aneri")
function overriding:
class Aneri():
def eat(self):
print("Aneri eating")
class Jithu(Aneri):
def eat(self):
Aneri.eat(self)
print("jithu eating")
obj=Jithu()
obj.eat()
Abstraction:
import abstract class
class Aneri(Najila):
def eat(self):
print("aneri eating")
class Jithu(Najila):
def eat(self):
print("jithu eating")
obj=Aneri()
obj.eat()
obj=Jithu()
obj.eat()
Encapsulation:
prevent from direct modification
class Computer:
def __init__(self):
self.__maxprice=900
def sell(self):
print(format(self.__maxprice))
def setMaxPrice(self,price):
self.__maxprice=price
a=Computer()
a.sell()
#change price
a.__maxprice=2000
a.sell()
a.setMaxPrice(2000)
a.sell()
constructor:
class Human:
def __init__(self,name,address,professional):
self.name=name
self.address=address
self.professional=professional
def result(self):
print(self.name,self.address,self.professional)
obj=Human("Aneri","India","Developer")
obj.result()
tkinter - module
widget:
button
heading label
entry
radio
check
title of our tkinter
add DB
message box
listbox
spinbox
........
anu.mainloop()
3.Check Button:
def python():
messagebox.showinfo("Python","yes you are attending python")
def java():
messagebox.showinfo("Java","No you are not into java")
anu.mainloop()
def python():
messagebox.showinfo("Python","yes you are attending python")
def java():
messagebox.showinfo("Java","No you are not into java")
anu.title("Online exam")
anu.mainloop()
Label
ENtry
firstname
lastname
email
gender
mobile
password
button
anu=Tk()
def task():
print("FirstName:", e1.get())
print("LastName:",e2.get())
print("Gender:", var.get())
print("Email:", e3.get())
print("Mobile no:", e4.get())
print("Password:",e5.get())
e1=Entry(anu)
e1.place(x=250, y=150)
e2=Entry(anu)
e2.place(x=250, y=180)
d=Label(anu, text="Gender", font=("bold",10))
d.place(x=160, y=210)
var=IntVar()
Radiobutton(anu, text="male",variable=var, value=0).place(x=250, y=210)
Radiobutton(anu, text="Female",variable=var,value=1).place(x=300, y=210)
e3=Entry(anu)
e3.place(x=250, y=240)
e4=Entry(anu)
e4.place(x=250, y=270)
e5=Entry(anu,show='*')
e5.place(x=250, y=300)
listbox in tkinter:
anu=Tk()
b=Listbox(anu)
b.insert(1,"Jithu")
b.insert(2,"Najila")
b.insert(3,"Aneri")
b.insert(4,"ANupriyan")
b.insert(5,"shaji")
btn=Button(anu,text="Delete",command=lambda b=b:b.delete(ANCHOR))
a.pack()
b.pack()
btn.pack()
anu.geometry("300x300")
anu.mainloop()
spinbox:
from tkinter import *
anu=Tk()
a=Spinbox(anu, from_=0, to=100)
a.pack()
anu.geometry("300x300")
anu.mainloop()
combobox:
anu.geometry("600x600")
anu.mainloop()
Frame:
from tkinter import *
anu=Tk()
f1=LabelFrame(anu, text="Aneri patel")
f1.pack(fill="both", expand="yes")
f2=LabelFrame(anu,text="Jithu shaji")
f2.pack(fill="both",expand="yes")
messagebox:
showinfo:
from tkinter import *
from tkinter import messagebox
anu=Tk()
messagebox.showinfo("Information","Software installed successfully")
anu.mainloop()
show warning:
from tkinter import *
from tkinter import messagebox
anu=Tk()
messagebox.showwarning("Warning","Disk full, delete some data")
anu.mainloop()
show error:
from tkinter import *
from tkinter import messagebox
anu=Tk()
messagebox.showerror("Error","Virus in the file")
anu.mainloop()
ask question:
from tkinter import *
from tkinter import messagebox
anu=Tk()
messagebox.askquestion("Confirm","Are you sure?")
anu.mainloop()
methods:
start() - start a new thread
run() - call start methos to run the app
isalive() - to check whether the thread is still running or not
setname() - set a name of thread
getname() - get a thread name
join() - terminate a thread
import threading
import time
exitFlag = 0
1.lock.acq()
2.block()
3.release.acq()
Networking in python:
network: connection
DataBase Connectivity:
School:
----student,staff,lib
Insert query -
select - fetch
update-
delete-
sql
mysql
mongo db - nosql
oracle
sqlite3
mysql/sql
software:
python IDLE---editor--1
mysql--DB----2
Jithu:Linux
https://dev.mysql.com/downloads/mysql/
Najila:windows 32
https://dev.mysql.com/downloads/windows/installer/8.0.html
Afterdownloading:
Steps:
1.open the mysql folder
2.open the bin folder > copy bin path
3.open cmd>run as admin
4.cd C:\Users\Livewire\Desktop\mysql-5.7.24-winx64\mysql-5.7.24-winx64\bin
5.>>mysqld --console --initialize
Localhost:127.0.0.1
user:root
password: :OrWw_Ex:4ki
6. start DB server
>mysqld --console
8.initialize
>>mysqld -initialize
>>mysql -u root -p
C:\Users\Livewire\Desktop\mysql-5.7.24-winx64\mysql-5.7.24-winx64\bin>mysql -u root
-p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
11. cltr+z
come out and connect once again
13.create DB name
>create database college;
15.
open pymysql >>>pymysql folder will be there>>>>copy the folder
>>C:\Users\Livewire\AppData\Local\Programs\Python\Python37\Lib
paste it here
import pymysql
db = pymysql.connect("localhost","root","najila","college")
cursor = db.cursor()
cursor.execute(sql)
print("Student Table Created")
db.close()
17. Insert value into table:
import pymysql
db = pymysql.connect("localhost","root","najila","college" )
cursor = db.cursor()
print("value is Inserted")
db.close()
eng5.ts@livewireindia.com
+91 9444807306(Whats)