Professional Program Online Tutorials(PPT)
the ways in which Python is used includes:
- Desktop graphical application development, including games;
- Mathematical and scientific analysis of data; and
- Web and internet development.
Hello world using python
output
hai
Adding two integers in python
a=10
b=20
c=a+b
print(c)
output
30
Adding two decimal numbers
a=10.10
b=20.2
c=a+b
print(c)
output
30.299999999999997
String variable print
a="sabari"
print(a)
output
sabari
Runtime input (String)
a=input()
print(a)
output
sabari
sabari
Runtime input(int)
a=int(input())
b=int(input())
c=a+b
print(c)
output
23
32
55
Runtime decimal numbers(float)
a=float(input())
b=float(input())
c=a+b
print(c)
output
20.1
20.2
40.3
Conditional Statements
If-else syntax
If(condition check):
Statement;
else:
statement;
example
a=int(input())
b=int(input())
if(a>b):
print("a is
big")
else:
print("b is
big")
output
23
21
a is big
if-else if
syntax
if(condition check):
statement;
elif(condition check):
statement;
else:
statement;
example
a=int(input())
b=int(input())
c=int(input())
if(a>b and a>c):
print("a is
big")
elif(b>a and b>c):
print("b is
big")
else:
print("c is
big")
output
12
22
33
C is big
Nested if
Syntax
If(condition check):
If(condition
check):
Statement
else:
statement
else:
Statement
Example
a=10
if(a>5):
if(a<20):
print("a
is between 5 to 20")
else:
print("a
is above 20")
else:
print("a is
less then 5")
output
a is between 5 to 20
for loop
syntax
for variable in range(start,end):
statement
ex
for i in range(1,10):
print(i)
output
1
2
3
4
5
6
7
8
9
Example 2
for i in range(10):
print(i)
output
0
1
2
3
4
5
6
7
8
9
Example 3
for i in range(2,20,2):
print(i)
output
2
4
6
8
10
12
14
16
18
While loop
Syntax
Initialize;
While(condition check):
Statement
Inc/dec
Example
i=0
while(i<5):
print("hai")
i=i+1
output
hai
hai
hai
hai
hai
break
i=0
while(i<5):
print("hai")
i=i+1
if(i==3):
break
output
hai
hai
hai
continue
i=0
while(i<5):
i=i+1
if(i==3):
continue
print(i)
output
1
2
4
5
Strings
Example 1
a="sabari"
print(a)
a='sabari'
print(a)
output
sabari
sabari
Example 2(index print)
a="hello"
print(a[0])
output
h
Example 3(index particular)
a="hello"
print(a[1:3])
output
el
Example 4(index upto)
a="hello"
print(a[:3])
output
hel
example 5(index with reverse)
a="hello"
print(a[-1])
output
o
example 5(delete the string)
a="hello"
del a
String methods
1.index
a="hello this is python program"
b=a.index("this")
print(b)
output
6
2.upper
a="hello this is python program"
b=a.upper()
print(b)
output
HELLO THIS IS PYTHON PROGRAM
3.lower
a="HELLO"
b=a.upper()
print(b)
output
hello
4.isdigit()
a="hello"
print(a.isdigit())
output
False
5.isdecimal()
a="5"
print(a.isdecimal())
output
True
6. capitalize()
a="hello this is python"
print(a.capitalize())
output
Hello this is python
7.startswith()
a="hello this is python"
print(a.startswith('h'))
output
True
8.endswith()
a="hello this is python"
print(a.endswith('h'))
output
False
9.replace()
a="hello this is python"
a=a.replace("hello","welcome")
print(a)
output
welcome this is python
Functions example
Syntax
def functionname():#delcare/define
statement
functionname() #calling
example
#define
def sabari():
print("hai")
#calling
sabari()
output
hai
Function with argument
#define
def sabari(fname):
print(fname)
#calling
sabari("sabari")
output
sabari
Lambda expression
Synax
Lambda arguments:Expression
x=lambda a:a+10
print(x(5))
output
15
2 values
x=lambda a,b:a*b
print(x(5,5))
output
25
Arrays
cars=["bmw","ford","volvo"]
print(cars)
output
["bmw","ford","volvo"]
One by one print
cars=["bmw","ford","volvo"]
for i in cars:
print(i)
output
bmw
ford
Volvo
Adding array
cars=["bmw","ford","volvo"]
cars.append("hello")
for i in cars:
print(i)
output
bmw
ford
Volvo
hello
Remove(pop)
cars=["bmw","ford","volvo"]
cars.pop(1)
for i in cars:
print(i)
output
bmw
volvo
remove keyword
cars=["bmw","ford","volvo"]
cars.remove("bmw")
for i in cars:
print(i)
output
ford
Volvo
Python collections
1.List
Ordered and changeable,duplicates
2.Tuple
Order and unchange able,duplicate
3.set
Un order and un index (no duplicates)
4.dictionary
Unordered,changeable and indexed(no duplicates)
1.List
Example
a=["apple","banana","cherry"]
print(a)
a[1]="orange"
print(a)
output
['apple', 'banana', 'cherry']
['apple', 'orange', 'cherry']
Example(list with index)
a=["apple","banana","cherry"]
print(a[1])
output
banana
Example (negative index)
a=["apple","banana","cherry"]
print(a[-1])
output
cherry
append
a=["apple","banana","cherry"]
a.append("orange")
print(a)
ouput
['apple', 'banana', 'cherry', 'orange']
Insert
a=["apple","banana","cherry"]
a.append("orange")
a.insert(1,"wel")
print(a)
ouput
['apple',’wel’, 'banana', 'cherry', 'orange']
Remove
a=["apple","banana","cherry"]
a.remove("apple")
print(a)
output
banana,cherry
pop
a=["apple","banana","cherry"]
a.pop()
print(a)
output
['apple', 'banana']
Tuples
Example
a=(101,'sabari',10)
print(a[0])
output
101
Example 2
a=(101,'sabari',10)
for i in a:
print(i)
output
101
sabari
10
Set
Example
days={"sabari","nathan","mca","sabari"}
print(days)
output
{'mca', 'sabari', 'nathan'}
Add
days={"sabari","nathan","mca","sabari"}
print(days)
days.add("monday")
print(days)
output
{'monday', 'sabari', 'nathan', 'mca'}
Dictionary
example
emp={"Name":"sabari","Age":25}
print(emp)
output
{'Name': 'sabari', 'Age': 25}
Example
emp={"Name":"sabari","Age":25}
print(emp["Name"])
output
sabari
Class and objects
class A:
x=5
print(ob.x)
5
Constructor
class A:
def __init__(self):
print("hai")
ob=A()
output
hai
class and objects with functions
class A:
def get(self,name):
print("hai
",name)
ob=A()
ob.get("sabari")
output
sabari
modules
sd.py
def sabari():
print("hai")
gg.py
import sd
sd.sabari()
output
hai
Super
ReplyDelete