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

python all

The document contains a collection of Python code snippets demonstrating various programming concepts, including conversions between units, calculations for geometric shapes, number classification, list and tuple operations, set manipulations, dictionary handling, and exception management. Each section provides a specific functionality, such as calculating surface area, checking for palindromes, generating random numbers, and implementing classes with inheritance. The snippets serve as practical examples for learning Python programming.

Uploaded by

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

python all

The document contains a collection of Python code snippets demonstrating various programming concepts, including conversions between units, calculations for geometric shapes, number classification, list and tuple operations, set manipulations, dictionary handling, and exception management. Each section provides a specific functionality, such as calculating surface area, checking for palindromes, generating random numbers, and implementing classes with inheritance. The snippets serve as practical examples for learning Python programming.

Uploaded by

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

PYTHON INTERNAL

1.convert bits to MB,GB and TB

Bits = int(input(“enter bits : ”))

Byte= Bits/8

Kb= Byte/1024

Mb= Kb/1024

Gb= Mb/1024

Tb= Gb/1024

print(Bits, “bits is ”,Mb, “MB”)

print(Bits, “bits is ”,Gb, “GB”)

print(Bits, “bits is ”,Tb, “TB”)

2.Surface area

print("Enter the height=")

len1=int(input())

print("Enter the radius=")

red1=int(input())

area=2*3.14*red1*len1+2*3.14*red1*red1

volume=3.14*red1*red1*len1

print("SurfaceArea of Cylinder",area)

print("Volume of Cylinder",volume)

3.Positive And Negative

print("Enter the Number=")

num=int(input())

if num==0 :

print("Number is zero")

elif num>0 :

print("Number is positive")
else:

print("Number is negative")

4.grade

print("Enter the 5 subject marks :")

m1=int(input())

m2=int(input())

m3=int(input())

m4=int(input())

m5=int(input())

total=m1+m2+m3+m4+m5

per=(total/100)*100

if per>=75 and per<=100 :

print("A grade")

elif per>=60 and per<=75 :

print("B grade")

elif per>=40 and per<=60 :

print("C grade")

else:

print("Fail")

5.fibo

print("Enter the number=")

num=int(input())

a=0

b=1

print(a)

print(b)

i=3

while i<=num:

c=a+b
print(c)

a,b=b,c

i=i+1

6.palindrom

print("Enter the number=")

num=int(input())

rev=0

tem=num

while num>0:

rem=num%10

rev=(rev*10)+rem

num=num//10

if rev==tem:

print("It is palindrome")

else:

print("It is not palindrome")

7.largest from list

list1 = [10, 20, 38, 2, 4]

list1.sort()

print("Sorted array")

print(list1)

print("Largest number")

print(list1[-1])

8. even from a list

list1 = [10, 20, 38, 2, 4,77,89]

for i in list1:

if i%2==0 :
print(i)

9.maximum from tuple

tuple1=(10,2,3,4,5,67)

print("MAX==",max(tuple1))

print("MIN==",min(tuple1))

10. repeated from list

tuple1=(10,20,39,20,39,67,10)

#print(tuple1.count(10))

i=0

a=tuple1.__len__()

while i<a :

j=i+1

while j<a :

if tuple1[i]==tuple1[j]:

print(tuple1[i])

j=j+1

i=i+1

11. intersection

set1={10,20,30,40,}

set2={50,60,40}

set3=set1.union(set2)

set4=set1.intersection(set2)

set5=set1.difference(set2)

set6=set1.symmetric_difference(set2)

print(set3)

print(set4)

print(set5)
print(set6)

set1.clear()

print(set1)

12. add and remove

set1={ 10,20,30,40}

set1.add(50)

print(set1)

13 .max and min from set

set1={10,20,30,40,}

print(“MAX==”,max(set1))

print(“MIN==”,min(set1))

14.dictionary sort

my_dict = { 1:30, 4:60, 2:40, 3:30 }

print(sorted(my_dict.items())

ascending_dict = dict(sorted(my_dict.items(), key = lambda item:item[1]))

descending_dict = dict(sorted(my_dict.items(), key = lambda item:item[1], reverse=True))

print(ascending_dict)

print(descending_dict)

15.dictionary concatenation

dict1 = {1:10, 2:20}

dict2 = {3:30, 4:40}

dict3 = {}

dict3.update(dict1)

dict3.update(dict2)

print(dict3)
16.srrcheck fun

def strcheck(str1):

lc = 0

uc = 0

for i in str:

if i.islower():

lc += 1

elif i.isupper():

uc += 1

else:

pass

print("No of lower case letter in string", lc)

print("No of Upper case letter in string", uc)

str = input("ENTER THE STRING:")

strcheck(str)

17. prime function

def primeno(no):

count=0

for i in range(1,no+1):

if no % i==0:

count+=1

if count==2:

print(no,"Is prime number")

else:

print(no, "is not prime")

num=int(input("Enter The NO"))

primeno(num)
18. collage user defined

#clg

def college():

name = input("Enter the name of collage:")

print(name)

#demo

import clg

clg.college()

19. calender

import calendar as c

y=int(input("Enter the year:"))

m=int(input("Enter the month:"))

print(c.month(y,m))

20.circumference using built in fuction

import math

r=int(input("Enter the radius="))

print("area=",math.pi*r*r)

print("circumference=",2*math.pi*r)

21.matrix add

import numpy as np

mat1=np.matrix('123;456')

mat2=np.matrix('123;456')

sum1=np.add(mat1,mat2)

print(sum1)

sub=np.subtract(mat1,mat2)

print(sub)

mul=np.multiply(mat1,mat2)

print(mul)
div=np.divide(mat1,mat2)

print(div)

22.class ( area of square)

class Area:

def cal(self ,a=None,b=None):

if(a!=None and b!=None):

print("Area of rectangle=",a*b)

else:

print("Area of Squre=",a * a)

obj=Area()

obj.cal(10,20)

obj.cal(10)

23.degree

from symtable import Class

class Degree:

def getDegree(self):

print("I got degree")

class Undergraduate(Degree):

def getDegree(self):

print("I am Undergraduate")

class post_graduate(Undergraduate):

def getDegree(self):

print("post_graduate")

obj=Degree()

obj.getDegree()

obj1=Undergraduate()

obj1.getDegree()

obj2=post_graduate()
obj2.getDegree()

24 employee

class Employee:

def get(self):

self.name = input("Enter name: ")

self.empid = int(input("Enter Employee ID: "))

self.address = input("Enter Address: ")

def put(self):

print("\nEmployee Details:")

print("NAME:", self.name)

print("EMPLOYEE ID:", self.empid)

print("ADDRESS:", self.address)

# Creating an instance and testing the methods

ob = Employee()

ob.get()

ob.put()

25.multiple inheritance

class A:

def fun1(self):

print(“Function 1 is called…”)

class B:

def fun2(self):
print(“Function 2 is called….”)

class C:

def fun3(self):
print(“Function 3 is called….”)

c1=new C()

c1.fun1()
c1.fun2()

c1.fun3()

26. ZeroDivisorError Exception

try:

a=int(input("Enter number: "))

b=int(input("Enter number: "))

div=a/b

print("Division is",div)

except ZeroDivisionError:

print("Can't divide by 0")

27. six random integer

import numpy as np

# Generate 6 random integers between 10 and 30 (inclusive of 10, exclusive of 30)

random_integers = np.random.randint(10, 31, size=6)

print("Random integers:", random_integers)

28. password check

class wrongData(Exception):

pass

try:

password="Abc@123"

userid=input("Enter userid: ")

pw=input("Enter password: ")

if(password==pw):

print("Login successful...")
else:

raise wrongData

except wrongData:

print("Wrong Password!")

29. random float

import random

# Generate a random float between 5 and 50

random_float = random.uniform(5, 50)

print("Random float between 5 and 50:", random_float)

30. factorial using function

def fact(num):

if num < 0:

print("Factorial is not defined for negative numbers.")

return

factorial1 = 1

for i in range(1, num + 1):

factorial1 *= i

print("Factorial of", num, "=", factorial1)

num1 = int(input("Enter the number: "))

fact(num1)

31. even number

i=1

while i<100 :

if i%2==0 :
print(i)

i=i+1

32. reverse

print("Enter the number=")

num=int(input())

rev=0

while(num>0):

rem=num%10

rev=(rev*10)+rem

num=num//10

print("revers=",rev)

You might also like