0% found this document useful (0 votes)
19 views

Pylab

This document contains 20 code snippets that demonstrate various Python programming concepts like loops, functions, classes, exceptions handling, file handling etc. Each code snippet is commented and labeled to be independently run and tested. Overall the document serves as an online Python compiler to write, run and debug Python code.

Uploaded by

Vinnu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Pylab

This document contains 20 code snippets that demonstrate various Python programming concepts like loops, functions, classes, exceptions handling, file handling etc. Each code snippet is commented and labeled to be independently run and tested. Overall the document serves as an online Python compiler to write, run and debug Python code.

Uploaded by

Vinnu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

'''

Online Python Compiler.


Code, Compile, Run and Debug python program online.
Write your code in this editor and press "Run" button to execute it.

'''

#print("Hello World")
'''1
a=int(input("Enter a value:"))
b=int(input("Enter b value:"))
c=int(input("Enter c value:"))
t=a+b+c
avg=t/3
print("total",t)
print("avg=",avg)
'''
'''2
for i in range(8,90,3):
print(i,end=",")
'''
'''3
name=input("enter name:")
n=int(input("no of times:"))
for i in range(n):
print(name)
'''
'''4
h=int(input("Enter height:"))
for i in range(h):
print('*'*i)
'''
'''5
w=input("enter word:")
x=0
for i in range(len(w)):
if w[i]=='a' or w[i]=='e' or w[i]=='i' or w[i]=='o' or w[i]=='u' or w[i]=='A' or w[i]=='E' or w[i]=='I' or w[i]=='O' or
w[i]=='U' :
x=1
if (x==1):
print('vowels')
else:
print("no vowels")
'''
'''6
s1=input("enter a string:")
s2=input("enter another string:")
l1=len(s1)
l2=len(s2)
res=""
if(l1==l2):
for i in range(l1):
res+=s1[i]+s2[i]
print("Result=",res)
else:
print("Enter strings of same length")
'''
'''7
def add_commas(num):
result=""
count=0
for c in reversed(num):
print(c+result)
result=c+result
count+=1
if count%3==0 and count!=len(num):
result=','+result
return result
large_int=input("enter large integer:")
f_nm=add_commas(large_int)
print("fnum=",f_nm)
'''
'''8
import random
numbers=[random.randint(1,100)for i in range(20)]
print("list=",numbers)
avg=sum(numbers)/len(numbers)
print("avg=",avg)
large=max(numbers)
small=min(numbers)
print("largest=",large)
print("smallest=",small)
print("sorted list=",sorted(numbers))
print("second largest=",sorted(numbers)[-2])
print("second smallest=",sorted(numbers)[1])
x=0
for i in numbers:
if(i%2==0):
x+=1

x=sum(1 for i in numbers if i%2==0)


print("even no. count=",x)
'''
'''9
def sum_digits(x):
count=0
for i in num:
count+=int(i)
return count
num=(input("Enter an integer:"))
result=sum_digits(num)
print("sum of digits in " ,num ,"is ",result)
'''
'''10
def num_of_factors(y):
x=int(y)
count=0
for i in range(1,x+1):
if x%i==0:
print(i)
count+=1
return count
num=input("enter num:")
result=num_of_factors(num)
print("no of fct=",result)
'''
'''11
def primes(n):
if(n==1 or n==0):
return False
for i in range(2,n):
if(n%i==0):
return False
return True
N=100
for i in range(1,N+1):
if primes(i):
print(i,end=",")
'''
'''12a
li1=[1,11,23,34,35]
li2=[4,10,25,27]
print("list1=",li1)
print("list2=",li2)
length1=len(li1)
length2=len(li2)
res=[]
i,j=0,0
while i<length1 and j<length2:
if li1[i]<li2[j]:
res.append(li1[i])
i+=1
else:
res.append(li2[j])
j+=1
print(res)
print(li1[i:])
print(li2[j:])
res=res+li1[i:]+li2[j:]
print("result:",res)
'''
'''12b
li1=[1,11,23,34,35]
li2=[4,10,25,27]
print("list1=",li1)
print("list2=",li2)
res=sorted(li1+li2)
print('result:',res)
'''
'''13
from itertools import permutations
w=input("Enter a word:")
count=0
for i in range(2,len(w)):
for p in permutations(w,i):
print(''.join(p),end=' ')
count+=1
print(count)
'''
'''14
class product:
def __init__(self,name,items,price):
self.name=name
self.items=items
self.price=price
def get_price(self,n):
if(n<10):
print("regular price is cahrged")
cost=n*self.price
print("actual cost=",cost)
elif n>10 and n<100:
print("10% discount")
cost=n*self.price
disp=(cost*10)/100
final=cost-disp
print("act cost=",cost)
print("discount p=",disp)
print("to be paid=",final)
else:
print("20% discount")
cost=n*self.price
disp=(cost*20)/100
final=cost-disp
print("act cost=",cost)
print("discount p=",disp)
print("to be paid=",final)
def my_purchase(self,n):
pen.get_price(n)
pen=product('pen',200,5)
n=int(input("enter no of pens:"))
pen.get_price(n)
n=int(input("enter no of pens want:"))
pen.my_purchase(n)
'''
'''15
class time:
def __init__(self,sec):
self.sec=sec
def sec_min(self):
s=self.sec
min=s//60
seconds=s%60
print("min:sec=",str(min),":",str(seconds))
def sec_hrs(self):
s=self.sec
hrs=s//3600
mins=(s//60)%60
seconds=s%60
print("hrs:min:sec=",str(hrs),":",str(mins),":",str(seconds))
seconds=int(input("enter seconds:"))
obj=time(seconds)
obj.sec_min()
obj.sec_hrs()
'''
'''16
class power:
def __init__(self,b,exp):
self.b=b
self.exp=exp
def pow_of_num(self):
res=self.b**self.exp
print("result=",res)
obj=power(2,6)
obj.pow_of_num()
'''
'''17
class reverse:
def strrev(self,s):
words=s.split(' ')
reverse_sen=' '.join(reversed(words))
print("Reverse string=",reverse_sen)
obj=reverse()
s=input("Enter string:")
obj.strrev(s)
'''
'''19
try:
a=int(input("enter a value:"))
b=int(input("enter b value:"))
s=a/b
except ZeroDivisionError:
print("can't divide by 0")
else:
print('result:',s)
'''
'''20
try:
a=int(input("enter a value:"))
b=int(input("enter b value:"))
with open("result.txt",'w') as result_file:
s=a/b
result_file.write(f"The result of a/b is{s}")
except ValueError:
print("Enter valid integers")
except ZeroDivisionError:
print("can't divide by 0")
else:
print("The result is",s)
finally:
print('this is finally block')
'''

You might also like