PYTHON PROGRAMMING
1. ASSIGNING VALUE TO VARIABLES
PROGRAM:
c=100
miles=100.0
name="Hari"
print(c)
print(miles)
print(name)
OUTPUT:
100
100.0
Hari
2. PYTHON STRINGS
PROGRAM:
str="hello world!"
print(str)
print(str[0])
print(str[2:5])
print(str[2:])
print(str*2)
print(str+"TEST")
OUTPUT:
hello world!
llo
llo world!
hello world!hello world!
hello world!TEST
3. OPERATORS
PROGRAM
a=21
b=10
c=0
c=a+b
print("add is",c)
c=a-b
print("sub is",c)
c=a*b
print("mul is",c)
c=a/b
print("divi is",c)
c=a%b
print("mod is",c)
c=a**b
print("exp is",c)
a=10
b=5
c=a//b
print("fd is",c)
OUTPUT
add is 31
sub is 11
mul is 210
divi is 2.1
mod is 1
exp is 16679880978201
fd is 2
4. MEMBERSHIP OPERATOR
PROGRAM
a=10
b=20
list=[1,2,3,4,5]
if (a in list):
print("a is available in list")
else:
print("a is not available in list")
if (b not in list):
print("b is available in list")
else:
print("b is available in list")
c=b/a
if (c in list):
print("c is available")
else:
print("c is not available")
OUTPUT:
a is not available in list
b is available in list
c is available
5. AREA AND CIRCUMFERENCE OF THE CIRCLE
PROGRAM
PI=3.14
r=float(input("enter the radius of circle="))
area=PI*r*r
circumference=2*PI*r
print('area is=',area)
print('circumference is=',circumference)
OUTPUT:
enter the radius of circle=6
area is= 113.03999999999999
circumference is= 37.68
6. SIMPLE INTEREST CALCULATION
PROGRAM
p=float(input("enter p="))
n=float(input("enter n="))
r=float(input("enter r="))
sim=p*n*r/100
print('simple interest is=',sim)
OUTPUT:
enter p=9
enter n=8
enter r=7
simple interest is= 5.04
7. SWAPPING TWO NUMBERS
PROGRAM
n=int(input("enter value for x:"))
m=int(input("enter a value for y:"))
temp=n
n=m
m=temp
print("After swapping the value for x is:",n)
print("After swapping the value for y is:",m)
OUTPUT:
enter value for x:7
enter a value for y:8
After swapping the value for x is: 8
After swapping the value for y is: 7
8. IF ELSE
PROGRAM
amount=int(input("enter amount:"))
if amount<1000:
discount=amount*0.05
print("Discount",discount)
else:
discount=amount*0.10
print("DISCOUNT",discount)
print("NETPAY:",amount-discount)
OUTPUT:
enter amount:800
Discount 40.0
enter amount:2000
DISCOUNT 200.0
NETPAY: 1800.0
9. ELIF
PROGRAM
amt=int(input("enter amount:"))
if amt<1000:
d=amt*0.05
print("discount",d)
elif amt<5000:
d=amt*0.10
print("discount",d)
else:
d=amt*0.15
print("discount",d)
print("NETPAY:",amt-d)
OUTPUT:
enter amount:60
discount 3.0
enter amount:4000
discount 400.0
enter amount:10000
discount 1500.0
[Link]
PROGRAM
num=int(input("enter your age"))
if num>=18:
print("your are eligible")
else:
print("your are not eligible")
OUTPUT:
enter your age18
you are eligible
enter your age5
you are not eligible
[Link] OR EVEN
PROGRAM
num=int(input("enter a number:"))
if (num%2==0):
print("number is even")
else:
print("number is odd")
OUTPUT:
enter a number:8
number is even
enter a number:9
number is odd
[Link],UPPERCASE,NUMBER
PROGRAM
ch=input("enter something")
if (ch>='A' and ch<='Z'):
print("you have entered uppercase")
elif (ch>='a' and ch<='z'):
print("you have entered lowercase")
else:
print("you have entered a number")
OUTPUT:
enter something KIT
you have entered uppercase
enter something kit
you have entered lowercase
enter something 8799
you have entered a number
[Link] AMONG GIVEN THREE NUMBERS
PROGRAM
a=int(input("enter a="))
b=int(input("enter b="))
c=int(input("enter c="))
if (a>b and a>c):
print("a is greater than b&c")
elif (b>a and b>c):
print("b is greater than a&c")
else:
print("c is greater than b&a")
OUTPUT:
enter a=8
enter b=6
enter c=3
a is greater than b&c
enter a=5
enter b=6
enter c=3
b is greater than a&c
enter a=3
enter b=2
enter c=7
c is greater than a&b
[Link] OR NEGATIVE
PROGRAM
num=int(input("enter the number"))
if num>0:
print('the given number is positive')
else:
print('the given number is negative')
OUTPUT:
enter the number 8
the given number is positive
enter the number -9
the given number is negative
[Link]
PROGRAM
count=0
while (count<9):
print('count is:',count)
count=count+1
print("HI KIT")
OUTPUT:
count is: 0
HI KIT
count is: 1
HI KIT
count is: 2
HI KIT
count is: 3
HI KIT
count is: 4
HI KIT
count is: 5
HI KIT
count is: 6
HI KIT
count is: 7
HI KIT
count is: 8
HI KIT
[Link] ELSE WITH LOOPS
PROGRAM
c=0
while c<5:
print(c,"is less than 5")
c=c+1
else:
print(c,"is not less than 5")
OUTPUT:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
[Link] LOOP
PROGRAM
for letter in 'python':
print('current letter :',letter)
fruits=['mango','apple','orange']
for fruits in fruits:
print('fruit:',fruits)
OUTPUT:
current letter : p
current letter : y
current letter : t
current letter : h
current letter : o
current letter : n
fruit: mango
fruit: apple
fruit: orange
[Link] IN FOR LOOP
PROGRAM
number=[11,33,55,39,55,75,37,21,23,41,13]
for num in number:
if num%2==0:
print('list have even no')
else:
print('list not contain even no')
OUTPUT:
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
list not contain even no
[Link] FACTORIAL OF A GIVEN NUMBER
PROGRAM
fact=1
num=int(input("enter the no:"))
for i in range(1,num+1):
fact=fact*i
print("the factorial of",num,"is",fact)
OUTPUT
enter the no:5
the factorial of 5 is 1
the factorial of 5 is 2
the factorial of 5 is 6
the factorial of 5 is 24
the factorial of 5 is 120
[Link] THE NUMBER
PROGRAM
num=int(input("enter the number:"))
r=0
while(num>0):
rem=num%10
r=r*10+rem
num=num//10
print("Reverse Number”,r)
OUTPUT
enter the number:723
Reverse Number 327
[Link] SERIES
PROGRAM
n=int(input("enter value:"))
a=0
b=1
i=2
print(a,end="")
print(b,end="")
while(i<n):
sum=a+b
print(sum,end="")
a,b=b,sum
i=i+1
OUTPUT
enter value:5
01123
[Link] 2 INTEGERS USING FUNCTION
PROGRAM
def sum(x,y):
return x+y
a=40
b=20
op=sum(a,b)
print("The Sum is",op)
OUTPUT
The Sum is 60
[Link] USING FUNCTION
PROGRAM
def diff(a,b):
result=a-b
return result
num1=50
num2=40
print("The Sub is", diff(num1,num2))
OUTPUT
The Sub is 10
[Link] STRING USING FUNCTION
PROGRAM
def fun():
for i in range(3):
print("KIT")
fun()
OUTPUT
KIT
KIT
KIT
[Link] ARGUMENTS
PROGRAM
a) def display():
print("Hello")
display("Hi")
OUTPUT
TypeError: display() takes 0 positional arguments but 1 was given
b) def display(str):
print(str)
display()
OUTPUT
TypeError: display() missing 1 required positional argument: 'str'
c) def display(str):
print(str)
str="KIT"
display(str)
OUTPUT
KIT
[Link] ARGUMENTS
PROGRAM
def display(str,a,b):
print("str",str)
print("floatvalue",a)
print("intvalue",b)
display(str="KIT",a=768.77,b=230)
OUTPUT
str KIT
floatvalue 768.77
intvalue 230
During function call we use assignment operator to assign values to function
parameters using other variables (instead of values).
def display(name,age,salary):
print("name",name)
print("age",age)
print("salary",salary)
n="KIT"
a=45
s=25000
display(salary=s,name=n,age=a)
OUTPUT
name KIT
age 45
salary 25000
[Link] ARGUMENTS
PROGRAM
def display(name,course:"BE"):
print("name", name)
print("course",course)
display(course="BCA",name="KIT")
OUTPUT
name KIT
course BCA
[Link]-LENGTH ARGUMENTS
PROGRAM
def func(name,*fav_sub):
print("\n", name,"like to read")
for subject in fav_sub:
print(subject,end=" ")
func("kit","mat","ap")
func("kit")
func("mat")
func("ap")
OUTPUT
kit like to read
mat ap
kit like to read
mat like to read
ap like to read
[Link] VALUES
PROGRAM
def max_min(vals):
x=max(vals)
y=min(vals)
return(x,y)
vals=(99,90,8,95,94,83,84,91,100)
(max_marks,min_marks)=max_min(vals)
print("Highest marks=",max_marks)
print("Lowest marks=",min_marks)
OUTPUT
Highest marks= 100
Lowest marks= 8
[Link] FUNCTION
PROGRAM
def fact(n):
if(n==1 or n==0):
return 1
else:
return n*fact(n-1)
n=int(input("enter value of n:"))
print("factorial of",n,"is",fact(n))
OUTPUT
enter value of n:5
factorial of 5 is 120
[Link] 2 NUMBER USING LAMBDA FUNCTION
PROGRAM
sum=lambda x,y:x+y
print("sum=",sum(5,10))
OUTPUT
Sum= 15
[Link] SMALLEST OF 2 NUMBER USING FUNCTION
PROGRAM
small =lambda a,b:min(a,b)
print("Minimum Numbers")
print(small(1,3))
print(small(3,5))
OUTPUT
Minimum Numbers
[Link] MOD() FUNCTION
PROGRAM
Quo,rem=divmod(100,3)
print("Quotitent=",Quo)
print("remainder=",rem)
OUTPUT
Quotient=33
Remainder=1
[Link]
PROGRAM
L1=['phy','che',1997,2001]
L2=[4,5,6,7,8,9,10]
print("L1[0]:",L1[0])
print("L2[1:6]:",L2[1:6])
OUTPUT
L1[0]: phy
L2[1:6]: [5, 6, 7, 8, 9]
[Link] LIST
PROGRAM
L=['phy','che',1994,307]
L[2]=1999
print("value:",L)
print("new value:",L[2])
OUTPUT
value: ['phy', 'che', 1999, 307]
new value: 1999
[Link] LIST
PROGRAM
L=['phy','che',1994,301]
print(L)
del L[2]
print("new list:",L)
OUTPUT
['phy', 'che', 1994, 301]
new list: ['phy', 'che', 301]
[Link] METHODS
PROGRAM
L1=['phy','che','mat']
print(len(L1))
L2=['python','java','c']
print("max value:",max(L2))
L3=[400,700,500,150,900]
print("min value:",min(L3))
[Link]('compsci')
print("Append",L1)
[Link](2, 'c++')
print("Insert",L2)
L3=['tam','eng','mat']
[Link]()
print("L3",L3)
[Link](1)
print("L3",L3)
L4=['php','sql','ccna']
[Link]('sql')
print("L4",L4)
L5=['html','css','js']
[Link]()
print("L5",L5)
L6=['linux','node','c++']
[Link]()
print("L6",L6)
OUTPUT
max value: python
min value: 150
Append ['phy', 'che', 'mat', 'compsci']
Insert ['python', 'java', 'c++', 'c']
L3 ['tam', 'eng']
L3 ['tam']
L4 ['php', 'ccna']
L5 ['js', 'css', 'html']
L6 ['c++', 'linux', 'node']
[Link] ASSIGNMENT
PROGRAM
(v1,v2,v3)=(1,2,3)
print(v1,v2,v3)
Tup1=(100,200,300)
(v1,v2,v3)=Tup1
print(v1,v2,v3)
(v1,v2,v3)=(2+4,5/3+4,9%6)
print(v1,v2,v3)
OUTPUT
123
100 200 300
6 5.666666666666667 3
[Link] TYPES OF TUPLES
PROGRAM
tup1=()
print(tup1)
tup2=(1,2,3,4,5)
print(tup2)
tup3=('a','b','c','d')
print(tup3)
#tuple with paranthesis
print('a','b',2,4.6)
#tuple without paranthesis
a,b=10,20
print(a,b)
OUTPUT
()
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd')
a b 2 4.6
10 20
[Link] TUPLE
PROGRAM
Tup1=(1,4,6,7,8)
Tup2=(5,9,10,11,13)
Tup3=Tup1+Tup2
print(Tup3)
OUTPUT
1,4,6,7,8,5,9,10,12,13
[Link] IN TERMS OF TUPLE
PROGRAM
Tup=(24,4)
Quo,rem=divmod(*Tup)
print(Quo,rem)
OUTPUT
60
[Link]() FUNTION
PROGRAM
Tup=(1,2,3,4,5)
L1=['a','b','c','d','e',]
print(list((zip(Tup,L1))))
OUTPUT
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
[Link]
PROGRAM
dict={'en':'345','name':'kit','course':'python'}
print(dict)
OUTPUT
{'en': '345', 'name': 'kit', 'course': 'python'}
[Link] ITEM
PROGRAM
dict={'English':'100','Tamil':'100','Hindi':'100'}
dict['Science']=100
print(dict)
OUTPUT
{'English': '100', 'Tamil': '100', 'Hindi': '100', 'Science': 100}
[Link] ITEM
PROGRAM
num={1:"ONE",2:"TWO"}
[Link]()
print("No Items",num)
OUTPUT
No Items {}
[Link] SINGLE KEY IN DICTIONARY
PROGRAM
dict={'1':'ONE','2':'TWO','3':'THREE'}
if '3' in dict:
print(dict['3'])
else:
print(dict['Not Matched'])
OUTPUT
THREE
STRING FUNCTIONS
[Link] FIRST LETTER
PROGRAM
str="python is awesome"
capstr=[Link]()
print('old string:',str)
print('cap string:',capstr)
OUTPUT
old string: python is awesome
cap string: Python is awesome
[Link]
PROGRAM
str="PYTHON IS AWESOME"
print("Lowercase str:",[Link]())
OUTPUT
Lowercase str: python is awesome
[Link] DEFAULT FILLCHAR
PROGRAM
str="python is awesome"
newstr=[Link](50)
print("centered string:",newstr)
OUTPUT
centered string: python is awesome
[Link] * FILL CHAR
PROGRAM
str="Python is awesome"
newstr=[Link](50,'*')
print("centered string:",newstr)
OUTPUT
centered string: ****************Python is awesome*****************
[Link] STRING
PROGRAM
str="Python is awesome,isn't it?"
substr="is"
count=[Link](substr)
print("count is:",count)
OUTPUT
count is: 2
[Link] ENCODE
PROGRAM
str="Python!"
print('string is:',str)
str_utf=[Link]()
print("encoded version:",str_utf)
OUTPUT
string is: Python!
encoded version: b'Python!'
[Link] ENDSWITH
PROGRAM
text="python is easy to learn"
res=[Link]("to learn")
print(res)
OUTPUT
True
[Link] TABS
PROGRAM
str="xyz\t1234\tabc"
res=[Link]()
print(res)
OUTPUT
xyz 1234 abc
[Link] STRING
PROGRAM
quote='let it be,let it be,let it be'
res=[Link]('let it')
print("let it:",res)
res=[Link]('small')
print("small:",res)
if([Link]('be')!=-1):
print("contains be")
else:
print("not contains be")
OUTPUT
let it: 0
small: -1
contains be
[Link] STRING
PROGRAM
Name="M234onica"
print([Link]())
name="M3onica Gell22er"
print([Link]())
OUTPUT
True False
[Link] STRING
PROGRAM
Name="Kalam"
print([Link]())
name="Kalam institute"
print([Link]())
name="M234onica"
print([Link]())
OUTPUT
True
False
False
[Link] STRING
PROGRAM
s="34512"
print([Link]())
s="32ladk3"
print([Link]())
OUTPUT
True
False
[Link] STRING
PROGRAM
str='python'
print([Link]())
str='py thon'
print([Link]())
str='22 python'
print([Link]())
OUTPUT
True
False
False
[Link] LOWERCASE
PROGRAM
s='this is good'
print([Link]())
s='this is not good'
print([Link]())
OUTPUT
True
True
[Link] STRING
PROGRAM
text='Love the neighbour'
print([Link]())
OUTPUT
['Love', 'the', 'neighbour']
[Link] STRING
PROGRAM
str="this should all be lowercase"
print([Link]())
str="THIS SHOULD ALL BE UPPERCASE"
print([Link]())
OUTPUT
THIS SHOULD ALL BE LOWERCASE
this should all be uppercase
[Link]
PROGRAM
cars=["ford","volvo","BMW"]
cars[0]="toyoto"
print(cars)
OUTPUT
['toyoto', 'volvo', 'BMW']
[Link] OF ARRAY
PROGRAM
vegs=["tomato","potato","onion"]
x=len(vegs)
print(x)
OUTPUT
[Link] TO PYTHON
PROGRAM
import json
x='{"name":"john","age":30,"city":"madurai"}'
y=[Link](x)
print(y["age"])
OUTPUT
30
[Link] TO JSON
PROGRAM
import json
x={"name":"john","age":30,"city":"madurai"}
y=[Link](x)
print(y)
OUTPUT
{"name": "john", "age": 30, "city": "madurai"}
[Link] ALL
PROGRAM
import re
txt="The rain is spain"
x=[Link]("ai",txt)
print(x)
OUTPUT
['ai', 'ai']
[Link] ALL
PROGRAM
import re
txt="The rain is spain"
x=[Link]("\s",txt)
print(x)
OUTPUT
['The', 'rain', 'is', 'spain']
[Link] FORMATTING
PROGRAM (a)
price=49
txt="The price is {} dollars"
print([Link](price))
OUTPUT
The price is 49 dollars
PROGRAM (b)
Quantity=3
itemno=567
price=49
Myorder="I want {} pieces of item number {} for {:2f} dollars."
print([Link](Quantity,itemno,price))
OUTPUT
I want 3 pieces of item number 567 for 49.000000 dollars.
[Link] NUMBERS
PROGRAM
age=36
name="KIT"
txt="His name is {1}.{1} is {0} years old."
print([Link](age,name))
OUTPUT
His name is [Link] is 36 years old.
[Link] INDEXES
PROGRAM
Myorder="I have a {carname}, it is a {model}"
print([Link](carname="ford",model="Mustang"))
OUTPUT
I have a ford, it is a Mustang
-------------
TRY THIS:
1) Find COMPOUND INTEREST
Formula:
Amount = principal * (pow((1 + rate / 100), time))
CI = Amount - principal
2) Check ARMSTRONG NUMBER
(eg: 153=1*1*1+5*5*5+3*3*3=153)
3) Profit or Loss
4) Find Equilateral Triangle(Formula: b=1.732/4*a*a)
5) Find Sum of Digits(eg: 12345=1+2+3+4+5=15)
6) Swapping of Two Numbers without using temp variable
7) Fibonacci series using Function
8) Convert Fahrenheit to Celsius(Formula: C=(f-
32)/N,N=1.8)
9) Print 1 to 30 even numbers using loop
10) Simple Interest using Function