Sie sind auf Seite 1von 27

CONTENTS

1.Introduction
2.Operators.
3.Selection Statements.
4.Data Collection and Variable.
5.Slicing .
6.Data Item Reference.
7.Break and Continue.
8.Functions.
9.Lambda Functions.
10.Class and Object.
11. Inheritance.
Introduction:
Python is a general purpose interactive interpreted object oriented and
high level programming language.

It was introduced in 1991 by Guido van Rassum.

Python places a strong emphasis on code realibility and simplicity so


that the programmers can develop applications rapidly.

Python supports cross platform development.

Python is multi-paradigm programming language, which allows user


allow to code in several different styles.

Python is widely used for scripting in game menu applications


effectively.
Advantages:

Program written in python require less numbers


of lines of code to perform the same task
compared to other languages like C. So it reduces
programming errors and required time also.

Python has simplest syntax, availability of


libraries and built in module.

Python can be used for large variety of tasks like


desktop applications, database applications,
network programming and even mobile
development also.
Operators:
• Arithmetic Operator
Python accepts all the basic operators like ‘+’ , ’-’ , ’*’ ,
’/’ , ’||’(floor division), ‘%’ , ‘**’(exponent).
e.g.
a=10
b=20
print(a+b)
output: 30

• Conditional Operator
<,>,<=,>=,==.
e.g.
a=10
b=20
a>b
output: false
• Membership Operator
is not
e.g.
a=“hi”
b=“HI”
c=a is not b
print(c)
output: true

•Logical operator:
and ,or,not
e.g:
5<6 and 4<7
Output:true
Selection Statement
• Conditional Statement
If, elif , else
Syntax:
if (condition) :
#statement
elif (condition):
#statement
else:
#statement
e.g.
x=1
if(x==1):
print(“one”)
elif(x==2):
print(“two”)
elif(x==3):
print(“three”)
else:
print (“number is not found”)
output: one
While Loop

Syntax:
initialization

while(condition)
#Statement

#increment/decrement
e.g:
i=1
while(i<=5):
print(i)
i=i+1

Output:
1
2
3
4
5
For loop:

Syntax:
for i in variable name:
#Statement
OR
for i in range(start,end,step):
#Statement

e.g: a=[1,2,3,4,5]
for i in a:
print(i)
Output:
1
2
3
4
5
e.g:
a=[1,2,3,4,5,6,7,8,9,10] Output:1 3 5 7 9
for i in range(1,10,2):
print(i,end=“ “)
Getting input from user:
Syntax:
variable name=type(input(“Statement”))

e.g:

a=int(input(“Enter the 1st number”))


b=int(input(“Enter the 2nd number”))

c=a+b
print(c)
•Data collection variable:
List: It is mutable
e.g.
a=[1,2,3,”ASSAM”]

Tuple: It is immutable
e.g:
a=(1,2,3,4,5)

Set: It is immutable
e.g.
a={1,2,3,4,5}

Dictionary:It is mutable
e.g.
syntax:
variable name={key:value}

a={1:”Rupa”,2:”Kripa”,3:”Mrityunjoy”}
Slicing:
Syntax:
List(start:end:step )
Data item reference:
Tuple:
Count() & index()
e.g:
s=(“compiler”,2,2,3)
print(s.count(2))

Output:2

e.g:
s=(1,2,3,4,5,100)
Print(s.index(100))

Output:5
List operation:
Insert(), pop(), append(), reverse(), sort(), count(), del(), remove(),

e.g: input()
b=[1,2,3,4,5,8,10]
b.insert(0,100)
print(b)
Output:
pop():
[100,2,3,4,5,8,10]
b.pop()
Output:
[100,2,3,4,5,8]

Append()
b.append(1000)
Output:
[100,2,3,4,5,8,1000]

reverse()
b.reverse()
Output:
[10000,8,5,3,2,100]
Dictionary:

Keys(), values(), items()


e.g:
a={1:"rupa",2:"Kripa"}
b=a.keys()
c=a.values()
d=a.items()
print(b)
print(c)
print(d)

Output:
dict_keys([1, 2])
dict_values([“rupa”,”kripa”])
dict_items([(1:”rupa”),(2:”kripa”)])
len():
syntax:len(dict)

cmp() :
syntax:cmp(dict1, dict2)
String methods:
Upper(), lower(), split(), replace(), count(),
swapcase(),isupper(), islower(), isalpha(),

e.g:
a=“HeLlo”
Print(a.lower())
Print(a.upper())
Print(a.swapcase())

Output:
hello
HELLO
hElLO
e.g:

c=“ASSAM INDIA”
print(c.split(‘ ‘))
Output: [‘ASSAM’, ’INDIA’]
 Break and Continue
e.g.
i=0
i=0
while(i<10):
while(i<10):
print(i)
i=i+1
if(i==5):
if(i==5):
break
continue
i=i+1
print(i)
output: 0,1,2,3,4,5
output: 1,2,3,4,6,7,8,9,10
 Functions
 no argument
Syntax:
def function_name():
#statement
e.g:
def abc():
print(“hi”)
abc()

output: hi

 Parameterize
e.g:
def abc(a):
return(a*3)
print(abc(4))

output: 12
 Lambda Function
 Used when there is no function
definition.

 Lambda(keyword)
e.g:
a=lambda i,j,k : i+j+k
print(a(2,3,4))
output: 9
 Class and Object:
syntax:

class class_name:
#definition

e.g:

class ABC:
def __init__(self):
print("Hi")
ob=ABC()

Output: Hi
•Inheritance:
Inheritance is the capability of one class to drive or inherit
the properties from some another class. The benefits of
inheritance are-
1.It represents the real world relationship well.
2.It provide the reusability of a code. We don’t have to write
same code again and again. It allows us to add more
features to a class without modifying it.
3.It is transitive in nature, which means that if class B is
inherits from another class A, then the all subclasses of B
would automatically inherit from class A.
Syntax:
class BaseClass:
body of base class;
class DerivedClass(BaseClass):
body of derived class;
Types of inheritance:
1.Single Inheritance:
Syntax:
class derivedclass(base class):
e.g:

class Animal:
def speak(self):
print("Animal Speaking")

class Dog(Animal):
def bark(self):
print("dog barking")

d = Dog()
d.bark()
d.speak()

Output:
dog barking
Animal speaking
2. Multilevel Inhertitance:
Syntax:
class class1:
#body
class class2(class1):
#body
class class3(class2):
#body
e.g:
class Animal:
def speak(self):
print("Animal Speaking")

class Dog(Animal):
def bark(self)
print("dog barking")

class DogChild(Dog): Output: dog barking


def eat(self): Animal Speaking
print("Eating bread...") Eating bread

d = DogChild()
d.bark()
d.speak()
d.eat()
3.Multiple Inheritance:
Syntax:
class base1:
#body
Class base2:
#body
Class derived(base1,base2):
#body
e.g:
class Calculation1:
def Summation(self,a,b):
return a+b;

class Calculation2:
def Multiplication(self,a,b):
return a*b;

class Derived(Calculation1,Calculation2): Output: 30


def Divide(self,a,b): 200
return a/b; 0.5

d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
THANK YOU

Das könnte Ihnen auch gefallen