KEMBAR78
Project (15 Pages) | PDF | Letter Case | Arithmetic
0% found this document useful (0 votes)
20 views15 pages

Project (15 Pages)

The document contains Python code snippets that demonstrate various programming concepts like counting characters in a string, reversing a string, searching an element in a list, sorting a list, finding maximum salary from employee records, reading and writing to files, creating a 2D list, and using stack operations. Each code snippet is preceded by a comment explaining the concept demonstrated in that section.

Uploaded by

Ritu Agarwal
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)
20 views15 pages

Project (15 Pages)

The document contains Python code snippets that demonstrate various programming concepts like counting characters in a string, reversing a string, searching an element in a list, sorting a list, finding maximum salary from employee records, reading and writing to files, creating a 2D list, and using stack operations. Each code snippet is preceded by a comment explaining the concept demonstrated in that section.

Uploaded by

Ritu Agarwal
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/ 15

#Counting the number of:

#uppercase Letters, Lowercase letters,


#Alphabets and Digits.

line=input("Enter a line:")

lcase=ucase=0

digit=alpha=0

for a in line:

if a.islower():

lcase+=1

elif a.isupper():

ucase+=1

elif a.isdigit():

digit+=1

if a.isalpha():

alpha+=1

print("Number of uppercase letters:",ucase)

print("Number of lowercase letters:",lcase)

print("Number of alphabets:",alpha)

print("Number of digits:",digit)
#Reversing a string.

s=input("Enter the string:")


r=""
for i in range(len(s)-1,-1,-1):
r+=s[i]
print("String entered:",s)
print("It's reverse:",r)
#Element searching in a List.

lst=eval(input("Enter the List:"))


length=len(lst)
elem=int(input("Enter the element to be seached: "))
for i in range(length):
if elem==lst[i]:
print(elem,"found at index:",i)
break
else:
print("Element not found in list")
#Sorting a list in increasing order.

lst=eval(input("Enter the list: "))


print("List created:",lst)
for i in range(len(lst)):
for j in range(0,len(lst)-i-1):
if lst[j]>lst[j+1]:
lst[j],lst[j+1]=lst[j+1],lst[j]
print("List(after sorting):",lst)
#Entering records of employees.
#Also finding the employee with highest salary.

d={}
n=int(input("How many employees?"))
for i in range(n):
nm=input("Enter the name:")
sal=int(input("Enter the salary:"))
d[nm]=sal
print("dictionary created:",d)
g=0
nm=""
for i in d:
if d[i]>g:
g=d[i]
nm=i
print("Employee with highest salary:",nm)
print("his salary:",g)
#Entering records of students.
#Also printing names of students with marks
above 75.

n=int(input("how many students"))


stu={}
for i in range(1,n+1):
print("enter details of student",(i))
name=input("name")
marks=int(input("marks:"))
roll=int(input("roll no."))
d={"roll":roll,"marks":marks,name:"name"}
key="stu"+str(i)
stu[key]=d
print(stu)
for i in range(1,n+1):
key="stu"+str(i)
if stu[key]["marks"]>=75:
print(stu[key])
#Performing all arithmetic operations.
#Using funtion.

def cal(x,y):
return x+y,x-y,x*y,x/y,x%y
num1=int(input("Enter number 1:"))
num2=int(input("Enter number 2:"))
add,sub,mult,div,mod=cal(num1,num2)
print("Sum of given numbers:",add)
print("Subtraction of given number:",sub)
print("Product of given numbers:",mult)
print("Division of given numbers:",div)
print("Modulo of given numbers:",mod)
#Checking number entered is palindrome
#or not using function

def reverse(n):
rev=0
t=n
while (n!=0):
R=n%10
rev=rev*10+R
n=n//10
return rev
n=int(input("Enter The Number = "))
a=reverse(n)
print(a)
if (a==n):
print("It is a Palindrome Number")
else:
print("It is not a Palindrome Number")
#Checking a number is prime or not.
#Using function.

def prime(x):
count=0
for a in range(2,(x//2)):
if(x%a==0):
count+=1
x=int(input("Enter the Number: "))
c=prime(x)
print(c)
if (c==0):
print("It is a Prime Number(*=*).")
else:
print("It is not a Prime Number!!!")
#Reading and Writing in a text file.

def writefile():
n=int(input("Enter the no. of students:"))
file=open("T1.txt","w")
for i in range(n):
nm=input("Enter the name:")
file.write(nm)
file.write("\n")
file.close()
def readfile():
file=open("T1.txt","r")
s=" "
while s:
s=file.readline()
print(s,end="")
file.close()
writefile()
readfile()
#Searching number number of vowels.
#File already exist and its name is "T1.txt".

def vowels():
file=open("T1.txt","r")
s=" "
c=0
while s:
s=file.read(1)
if s in ["a","e","i","o","u"]:
c+=1
print("Number of vowels:",c)
vowels()
#Writing in a binary file.

import pickle
stu={}
stufile=open("stu.dat","wb")
ans="y"
while ans=="y":
r=int(input("Enter the rollno.:"))
nm=input("Enter the name:")
mrks=int(input("Enter the marks"))
stu["Rollno"]=r
stu["Name"]=nm
stu["Marks"]=mrks
pickle.dump(stu,stufile)
ans=input("Want to enter more records?(y/n)...")
stufile.close()
#Writing in a csv file.

import csv
fh=open("student.csv","w")
swriter=csv.writer(fh)
swriter.writerow(["Rollno.","Name","Marks"])
for i in range(3):
print("student record",(i+1))
r=int(input("Enter Rollno."))
nm=input("Enter the name:")
mrks=int(input("Enter the marks:"))
rec=[r,nm,mrks]
swriter.writerow(rec)
#Creating a 2d list and traversing it.

lst=[]
r=int(input("How many rows:"))
c=int(input("How many columns:"))
for i in range(r):
row=[]
for j in range(c):
e=int(input("Enter the element:"))
row.append(e)
lst.append(row)
print("list created:",lst)
for i in range(r):
for j in range(c):
print(a[i][j],end=" ")
print()
#Usage of push and pop in stack.

def push(stk,item):
a.append(item)
def pop(stk):
if stk==[]:
print("Underflow!!!")
else:
item=stk.pop()
stk=[]
n=int(input("How mant elements?"))
for i in range(n):
e=int(input("Enter the element:"))
stk.append(e)
print("Stack created:",stk)
item=int(input("Enter the element to be insert:"))
push(stk,item)
pop(stk)

You might also like