Skip to content

Latest commit

 

History

History
2344 lines (1120 loc) · 16.2 KB

File metadata and controls

2344 lines (1120 loc) · 16.2 KB

BASICS OF PYTHON

print('hello word')
hello word

Veriable

student = 'sam'
student
'sam'
student = 'matt'
student
'matt'

Data type

a1 = 10
a1
10
type(a1)
int
a1 = 3.14
a1
3.14
type(a1)
float
a1 = True
a1
True
type(a1)
bool
a1 = 'hello word'
a1
'hello word'
type(a1)
str
a1 = 3+4j
a1
(3+4j)
type(a1)
complex

OPERATORS

Arithemetric operators

# +,-,*,/
a=20
b=30
a,b
(20, 30)
a+b
50
a-b
-10
b-a
10
a*b
600
a/b
0.6666666666666666

#Relational operators

# <,>,==,!=
a=50
b=70
a<b
True
a>b
False
a==b
False
a!=b
True

Logical operators

# &,|
a=True
b=False
a & b
False
a & a
True
b & a
False
b & b
False
a | b
True
a | a
True
b | a
True
b | b
False

python tokens

#keywods

#True, class, if,yield, while, false, finally, is, return, try, none, continue, for, lambda, try, def, from, nonlocal, while, and, del, global, not, with, as, eelif, if, or

identifiers

Student = 'sartha'
student ='dinesh'
Student
'sartha'
student
'dinesh'

#literals - Do not change

a ='hello'
a
'hello'
#strings
str1 ="this is my first string"
str1
'this is my first string'
str2 ="this is my second string"
str2
'this is my second string'
str3 = '''
this
is 
my 
third
string
'''
str3
'\nthis\nis \nmy \nthird\nstring\n\n'
my_string ="My name OM"
my_string
'My name OM'
my_string[0]
'M'
my_string[-2]
'O'
my_string[4:10]
'ame OM'
len(my_string)
10

#string function

my_string.lower()
'my name om'
my_string.upper()
'MY NAME OM'
my_string.replace('M','P')
'Py name OP'
my_string.count('name')
1
ste_new ='sparta sparta sparta 2 2 2 22 2 2 2 2 2 22'
ste_new
'sparta sparta sparta 2 2 2 22 2 2 2 2 2 22'
ste_new.count('2')
12
s1 ='this iss om'
s1
'this iss om'
s1.find('om')
9
fruit ='I like banna,apple,mango'
fruit
'I like banna,apple,mango'
fruit.split(',')
['I like banna', 'apple', 'mango']

Data-structure in python

# 1Tuple 2.list 3.Dictionary 4.Set

Tuple in python

tup1 =(1,True,'OM',2.34,2+4j)
tup1
(1, True, 'OM', 2.34, (2+4j))
tup1[0]
1
tup1[2]
'OM'
tup1[-1]
(2+4j)
tup1[0:4]
(1, True, 'OM', 2.34)
type(tup1)
tuple
tup1[2]='hello'
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_5212/764570895.py in <module>
----> 1 tup1[2]='hello'


TypeError: 'tuple' object does not support item assignment
len(tup1)
5
tup2=(1,False,2.3,'hello',2+8j)
tup2
(1, False, 2.3, 'hello', (2+8j))
tup1
(1, True, 'OM', 2.34, (2+4j))
tup1+tup2
(1, True, 'OM', 2.34, (2+4j), 1, False, 2.3, 'hello', (2+8j))
tup2+tup1
(1, False, 2.3, 'hello', (2+8j), 1, True, 'OM', 2.34, (2+4j))
tup1,tup2
((1, True, 'OM', 2.34, (2+4j)), (1, False, 2.3, 'hello', (2+8j)))
tup3=(1,2,3)
tup3*8
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
tup1*3+tup2*1
(1,
 True,
 'OM',
 2.34,
 (2+4j),
 1,
 True,
 'OM',
 2.34,
 (2+4j),
 1,
 True,
 'OM',
 2.34,
 (2+4j),
 1,
 False,
 2.3,
 'hello',
 (2+8j))
min(tup3)
1
max(tup3)
3

list in python

l1=[1,'a',False]
l1
[1, 'a', False]
type(l1)
list
a1=[1]
type(a1)
list
l1[0]
1
l1[-1]
False
l1[0:3]
[1, 'a', False]
l1
[1, 21, False]
l2 = [1,'a',2,3,'OM']

l2

l2
[1, 'a', 2, 3, 'OM']
l2[0]=100
l2
[100, 'a', 2, 3, 'OM']
l2.pop()
'OM'
l2
[100, 'a', 2, 3]
l2.append('this is  strata')
l2
[100, 'a', 2, 3, 'this is  strata']
l2.reverse()
l2
['this is  strata', 3, 2, 'a', 100]
l2.insert(2,2+4j)
l2
['this is  strata', 3, (2+4j), 2, 'a', 100]
l3=['mango','banna','apple','grapes']
l3
['mango', 'banna', 'apple', 'grapes']
l3.sort()
l3
['apple', 'banna', 'grapes', 'mango']
l3*3+l2*5
['apple',
 'banna',
 'grapes',
 'mango',
 'apple',
 'banna',
 'grapes',
 'mango',
 'apple',
 'banna',
 'grapes',
 'mango',
 'this is  strata',
 3,
 (2+4j),
 2,
 'a',
 100,
 'this is  strata',
 3,
 (2+4j),
 2,
 'a',
 100,
 'this is  strata',
 3,
 (2+4j),
 2,
 'a',
 100,
 'this is  strata',
 3,
 (2+4j),
 2,
 'a',
 100,
 'this is  strata',
 3,
 (2+4j),
 2,
 'a',
 100]
l1+l2+l3
['this is  strata',
 3,
 (2+4j),
 2,
 'a',
 100,
 'apple',
 'banna',
 'grapes',
 'mango']
l1,l2,l3
([],
 ['this is  strata', 3, (2+4j), 2, 'a', 100],
 ['apple', 'banna', 'grapes', 'mango'])

Dictionary

d1={'applee':50,'mango':100,'banna':40}
d1
{'applee': 50, 'mango': 100, 'banna': 40}
type(d1)
dict
d1.keys()
dict_keys(['applee', 'mango', 'banna'])
d1.values()
dict_values([50, 100, 40])
d1['grapes']=50
d1
{'applee': 50, 'mango': 100, 'banna': 40, 'grapes': 50}
d1['applee']=200
d1
{'applee': 200, 'mango': 100, 'banna': 40, 'grapes': 50, 'apple': 200}
d2={'guava':80,'orange':70}
d2
{'guava': 80, 'orange': 70}
d1.update(d2)
d1
{'applee': 200,
 'mango': 100,
 'banna': 40,
 'grapes': 50,
 'apple': 200,
 'guava': 80,
 'orange': 70}
d2.update(d1)
d2
{'guava': 80,
 'orange': 70,
 'applee': 200,
 'mango': 100,
 'banna': 40,
 'grapes': 50,
 'apple': 200}
d2.pop('banna')
40
d2
{'guava': 80,
 'orange': 70,
 'applee': 200,
 'mango': 100,
 'grapes': 50,
 'apple': 200}

If statement

a=10
b=20
if b>a:
    print('b is greateer then a')
b is greateer then a
if a>b:
    print('b is greater theb a')
if a>b:
    print('a is greater then b')
else:
    print('b is greater then a')
b is greater then a
a=10
b=20
c=30
if (a>c) & (a>b):
    print('a is the greatest')
elif (b>a) &(b>c):
    print('b is greatest')
else:
    print('c is greatest')
    
c is greatest
#if with tuple
tup1=('a','b','c')
if 'a' in tup1:
    print('value a is present in tup1')
value a is present in tup1
if 'd' in tup1:
    print('value d is present in tup1')
#if with list
l1=[1,2,3]
l1
[1, 2, 3]
if l1[2]==3:
    l1[2]=4
   
l1
[1, 2, 4]
#if with dictionaary
d1={'k1':20,'k2':40,'k3':50}
d1
{'k1': 20, 'k2': 40, 'k3': 50}
if d1['k3']==50:
    d1['k3']=100
d1
{'k1': 20, 'k2': 40, 'k3': 100}

looping statements

While condition

i=1
while i<=10:
        print(i)
        i=i+1
    
1
2
3
4
5
6
7
8
9
10
i=100
while i<=120:
    print(i)
    i=i+3
100
103
106
109
112
115
118
i=1
n=2
while i<=10:
    print(n ," * ",i," = ", n*i)
    i=i+1
2  *  1  =  2
2  *  2  =  4
2  *  3  =  6
2  *  4  =  8
2  *  5  =  10
2  *  6  =  12
2  *  7  =  14
2  *  8  =  16
2  *  9  =  18
2  *  10  =  20
#while with list
l1=[1,2,3,4]
i=0
while i<len(l1):
    l1[i]=l1[i]+100
    i=i+1
l1
[101, 102, 103, 104]

For loop

l1 = ['mango','graps','oranga','banana']
for i in l1:
    print(i)
mango
graps
oranga
banana
#multiple for loop
l1=['book','laptop','chair','table']
l2=['black','orange','white']
for i in l1:
    for j in l2:
        print(i,j)
book black
book orange
book white
laptop black
laptop orange
laptop white
chair black
chair orange
chair white
table black
table orange
table white

Functions

def hello():
    print('hello word')
hello()
hello word
def add_10(x):
    return x+10
add_10(16)
26
add_10(10)
20
def even_odd(x):
    if x%2==0:
        print(x, 'is even')
    else:
        print(x,'is oddd')
even_odd(10)
10 is even
even_odd(5)
5 is oddd
g=lambda x: x*x*x
g(7)
343
g(10)
1000
#lambda with filter
l1 = [1,23,45,67,98,56]

final_list=list(filter(lambda x:(x%2!=0), l1))
final_list
[1, 23, 45, 67]
#lambda with map
l1 =[1,2,3,4,5,6,7,8]
list_final=list(map(lambda x:x*2, l1))
list_final
[2, 4, 6, 8, 10, 12, 14, 16]
from funktools import reduce
l1=[1,2,3,4,5,6,7,8]
sum=reduce(lambda x,y: x+y,l1)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_3556/3401324226.py in <module>
----> 1 sum=reduce(lambda x,y: x+y,l1)


NameError: name 'reduce' is not defined
sum
<function sum(iterable, /, start=0)>

Python object Oriented Programming

Classes

#creating the first class 
class Phone:
    def make_call(self):
        print('making call')
    def play_game(self):
        print('playing game')
p1 = Phone()
p1.make_call()
making call
p1.play_game()
playing game
#adding parameterss to the class 
class Phone:
    def set_color(self,color):
        self.color=color
    def set_cost(self,cost):
        self.cost=cost
    def show_color(self):
        return self.color
    def show_cost(self):
        return self.cost
    def make_call(self):
        print('making call')
    def play_game(self):
        print('playing game')
p2 = Phone()
p2.set_color('black')
p2.set_cost(10000)
p2.show_color()
'black'
p2.show_cost()
10000
p2.make_call()
making call
p2.play_game()
playing game

creating class with constuctor

class Employee:
    def __init__(self,name,age,salary,gender):
        self.name = name
        self.age = age
        self.salary = salary
        self.gender = gender
        
    def show_employee_details(self):
        print('name of employee is',self.name)
        print('age of employee is',self.age)
        print('salary of employee is',self.salary)
        print('gender of employee is',self.gender)
e1 = Employee('Om',18,50000,'male')
e1.show_employee_details()
name of employee is Om
age of employee is 18
salary of employee is 50000
gender of employee is male

inheritance in python

class vehicle:
    def __init__(self,mileage,cost):
        self.mileage = mileage
        self.cost = cost
        
    def show_vehicle_details(self):
        print('mileage of vihicle is',self.mileage)
        print('cost of vehicle is',self.cost)
c1 = vehicle(50,5000000)
c1.show_vehicle_details()
mileage of vihicle is 50
cost of vehicle is 5000000
class car(vehicle):
    def show_car_details(seif):
        print('I am a car')
c1 = car(200,1200000)
c1.show_vehicle_details()
mileage of vihicle is 200
cost of vehicle is 1200000
c1.show_car_details()
I am a car

over_riding the init method

class car(vehicle):
    def __init__(self,mileage,cost,tyers,hp):
        self.mileage = mileage
        self.cost = cost
        self.tyers = tyers
        self.hp = hp
        
    def show_car_details(self):
        print('tyers of vihicle is',self.tyers)
        print('hp of vehicle is',self.hp)
        print('i am a car')
c1 = car(300,2000000,4,400)
c1.show_car_details()
tyers of vihicle is 4
hp of vehicle is 400
i am a car

multiple inheritance

class perent1:
    def assign_string_one(self,str1):
        self.ster1 = str1
    def assign_string_one(self):
        return self.str1
class perent2:
    def assign_string_two(self,str2):
        self.str2 = str2
    def assign_string_two(self):
        return self.str2
class child(perent1,perent2):
    def assign_string_three(self,str3):
        self.str3 = str3
    def assign_string_three(self):
        self.str3
my_child = child()

#some work is due

multi-level inheritanc

class parent:
    def get_name(self,name):
        self.name = name
    def show_name(self):
        return self.name
class child(parent):
    def get_age(self,age):
        self.age = age
    def show_age(self):
        return self.age
class grandchild(child):
    def get_gender(self,gender):
        self.gender = gender
    def get_gender(self):
        return self.gender
    
gc = grandchild()
gc.get_name('om')
gc.get_age(18)
gc.show_name()
'om'
gc.show_age()
18

Thanks for Reading

Best Regards

Om Prakash