[go: up one dir, main page]

0% found this document useful (0 votes)
28 views13 pages

PDF

The document contains 24 code snippets demonstrating various string operations and functions in Python, including: 1. Comparing and finding the greatest of two or three numbers. 2. Performing arithmetic operations on input numbers. 3. Creating and modifying lists, tuples, dictionaries, sets and frozensets. 4. Checking if a number is a palindrome, prime, or calculating its factorial. 5. Printing multiplication tables and calculating area and circumference of a circle. 6. Accessing string elements using indexes, slicing, loops, and various operators like repetition, comparison, etc. 7. Counting characters and words in a string.
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)
28 views13 pages

PDF

The document contains 24 code snippets demonstrating various string operations and functions in Python, including: 1. Comparing and finding the greatest of two or three numbers. 2. Performing arithmetic operations on input numbers. 3. Creating and modifying lists, tuples, dictionaries, sets and frozensets. 4. Checking if a number is a palindrome, prime, or calculating its factorial. 5. Printing multiplication tables and calculating area and circumference of a circle. 6. Accessing string elements using indexes, slicing, loops, and various operators like repetition, comparison, etc. 7. Counting characters and words in a string.
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/ 13

#1.

find the greater between two number


n1 = int(input("enter first number"))
n2 = int(input("enter second number"))
if n1 > n2:
print("First number is greater")
else:
print("second number is greater")

enter first number 5


enter second number 6

second number is greater

#2. find the greatest between three number


n1 = int(input("enter first number"))
n2 = int(input("enter second number"))
n3 = int(input("enter three number"))
if n1 > n2:
if n1 > n3:
print("First number is greater:-",n1)
else:
print("third number is greater:-",n3)
elif n2 > n3:
print("second number is greatest:-",n2)
else:
print("third number is greatest:-",n3)

enter first number 56


enter second number 65
enter three number 78

third number is greatest:- 78

#3 arithemetic operation
n1 = int(input("enter first number"))
n2 = int(input("enter second number"))
a = n1+n2
b = n1-n2
c = n1*n2
d = n1/n2
e = n1%n2
print(a,b,c,d,e)

enter first number 56


enter second number 67

123 -11 3752 0.835820895522388 56

#4. tree pattern


size=int(input("enter size:"))
for i in range(size+1):
print(" "*(size-i),end="")
print("*"*((2*i)+1),end="")
print()
for i in range((size+1)//2):
print(" "*(((size*2)//3)),end="")
print("*"*((size*2)//3),end="")
print()

#5 grade
name = input("enter name")
roll_no = int(input("enter roll no"))
mark1 = int(input("enter mark of 1:-"))
mark2 = int(input("enter mark of 2:-"))
mark3 = int(input("enter mark of 3:-"))
mark4 = int(input("enter mark of 4:-"))
mark5 = int(input("enter mark of 5:-"))
sum=mark1+mark2+mark3+mark4+mark5
print("your total marks is :-",sum)
per=(sum*100)/500
print("your percentage is:- ",per)
if per>=90:
print("grade a")
elif per>=80 and per<90:
print("grade b")
elif per>=70 and per<80:
print("grade c")
elif per>=60 and per<70:
print("grade d")
else:
print("grade f \nBetter Luck Next Time")

enter name sunny


enter roll no 4655
enter mark of 1:- 56
enter mark of 2:- 78
enter mark of 3:- 67
enter mark of 4:- 56
enter mark of 5:- 54

your total marks is :- 311


your percentage is:- 62.2
grade d

#6. Display angle of a degree


a = int(input("enter the degree: "))
if a == 90:
print("Right Angle")
elif a==180:
print("straight angle")
elif a>0 and a<90:
print("acute angle")
elif a>180 and a<360:
print("reflex angle")
elif a>180 and a<90:
print("obtuse angle")
else:
print("wrong input")

enter the degree: 67

acute angle

#7.number is palidrome or not


number = int(input("Enter the number:-"))
temp = number
reverse = 0
while(number>0):
dig=number%10
reverse=reverse*10+dig
number=number//10
print("the reverse number is:-",reverse)
if reverse == temp:
print("number is palidrome")
else:
print("number is not palidrome")

Enter the number:- 565

the reverse number is:- 5


the reverse number is:- 56
the reverse number is:- 565
number is palidrome

#8. calculate the factorial of number


fac=int(input("enter the number for factorial:-"))
f=1
for i in range(1,fac):
fac=fac*i
print("the factorial is :-",fac)

enter the number for factorial:- 4

the factorial is :- 24

#9. print the multiplication table


n =int(input("enter the number of table:-"))
for i in range(11):
print(n,"*",i,"=",n*i)
enter the number of table:- 5

5 * 0 = 0
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

#10 Check number is prime or not


n=int(input("Enter the number:-"))
if n>1:
for i in range(2,int(n/2)+1):
if(n%i)==0:
print(n,"is not prime")
break
else:
print("number is prime")
else:
print("not prime")

Enter the number:- 5

number is prime

#11. to show inbuilt function(input,print,uppercase,capitaliza)


name = input("enter your name:-")
rollno = int(input("enter your rollno:-"))
city = input("enter your city:-")
print("your name is:- ",name.upper())
print("your roll number is:- ",rollno)
print("your city is:- ",city.capitalize())

enter your name:- sunny


enter your rollno:- 7426
enter your city:- amritsar

your name is:- SUNNY


your roll number is:- 7426
your city is:- Amritsar

#12. to show user-define function


def circumfrence_circle(r):
cr =2*3.14*r
print("the circumfrence of circle is =",cr)
def area_circle(r):
ar=3.14*r*r
print("Area of Circle is =",ar)
n=int(input("enter radius of circle"))
circumfrence_circle(n)
area_circle(n)

enter radius of circle 4

the circumfrence of circle is = 25.12


Area of Circle is = 50.24

#13. to calculate factorial using recursion


def rec_fac(n):
if n==1:
return n
else:
return n*rec_fac(n-1)
x = int(input("enter the number"))
print("the factorial is :-",rec_fac(x))

enter the number 5

the factorial is :- 120

#14. whether the list is mutable or immutable


thislist=["apple","banana","cherry","orange","kiwi","mango"]
print("Orignal List :-",thislist)
thislist[1:3]=["lichi","watermelon"]
print("modified List :-",thislist)

Orignal List :- ['apple', 'banana', 'cherry', 'orange', 'kiwi',


'mango']
modified List :- ['apple', 'lichi', 'watermelon', 'orange', 'kiwi',
'mango']

#15. whether the tuple is mutable or immutable


mytuple = (1,2,3,4,5)

print(mytuple[1])
print(mytuple[4])

mytuple=(1,2,3,4,2,3)
print(mytuple)

mytuple[1] = 100 #tuple are immutable so it cannot access any


modification
print(mytuple)

2
5
(1, 2, 3, 4, 2, 3)
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[50], line 10
7 mytuple=(1,2,3,4,2,3)
8 print(mytuple)
---> 10 mytuple[1] = 100
11 print(mytuple)

TypeError: 'tuple' object does not support item assignment

#16. to create dictionary and also show whether a dictionary is


mutable or immutable
student = {101:"Sunny",102:"varun",103:"harry"}
print("Original Student Dictionary :-",student)
student[102]="vijay"
print("Modified Student Dictionary :-",student)

Original Student Dictionary :- {101: 'Sunny', 102: 'varun', 103:


'harry'}
Modified Student Dictionary :- {101: 'Sunny', 102: 'vijay', 103:
'harry'}

#17. to create a set and also show whether the set is mutable or
immutable
#Modifying Set
a = {11,13}
print ("original set:-",a)

#adding an item
a.add(12)
print("Adding New items",a)

#adding multiple elements


a.update([22,13,14])
print("Adding multiple value",a)

#adding list and set


a.update([14,15],{11,15,25})
print("Adding list and set together",a)

original set:- {11, 13}


Adding New items {11, 12, 13}
Adding multiple value {11, 12, 13, 14, 22}
Adding list and set together {11, 12, 13, 14, 15, 22, 25}

18. #18 to create a frozen set and also show whether it is mutable or
immutable
fruits=frozenset(["apple","banana","orange"])
print(fruits)
fruits.append("cherry") #frozen set cannot be modified after creation
print(fruits)

frozenset({'banana', 'apple', 'orange'})

----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
Cell In[3], line 4
2 fruits=frozenset(["apple","banana","orange"])
3 print(fruits)
----> 4 fruits.append("cherry") #frozen set cannot be modified after
creation
5 print(fruits)

AttributeError: 'frozenset' object has no attribute 'append'

#19 to create a string and also show whether a string is mutable or


immutable
first_name = input("enter your first name")
print("Original String is:-",first_name)
first_name[3]='t' #string is immutable it cannot modified
print(first_name)

enter your first name sunny

Original String is:- sunny

----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[7], line 4
2 first_name = input("enter your first name")
3 print("Original String is:-",first_name)
----> 4 first_name[3]='t'
5 print(first_name)

TypeError: 'str' object does not support item assignment

#20 to create a program access string using positive and negative


indexing
#demonstrate positive indexing
print("poitive indexing")
mystring = "Hello!! SIR!! HOW MAY I HELP YOU"
print(mystring[5])

#demonstrate negative indexing


print("negative indexing")
mystring = "Hello!! SIR!! HOW MAY I HELP YOU "
print(mystring[-19])

poitive indexing
!
negative indexing
H

#21 to create a program to demonstrate the slicing operation


mystring= "Hello!! SIR!! HOW MAY I HELP YOU "
print("original string :-",mystring)
print(mystring[14:32])

original string :- Hello!! SIR!! HOW MAY I HELP YOU


HOW MAY I HELP YOU

#22 to access element of string using loop


mystring="Welcome to python coding"
#applying for loop on entire string elements
for i in mystring:
print(i,end="")
print("\n")
#appling for loop on specific elements
for i in mystring[0:7]:
print(i,end="")

Welcome to python coding

Welcome

#23 to demonstrate the use of different operator including


repletion,concatenation,comparison and membership operator using
string
s1 = "Hello !! Sir !!"
s2 = "Welcome to python coding"

#applying concatenation on string


print("applying concatenation")
print(s1+s2,"\n")

#applying repletion on string


print("applying repletion")
print(s1*3)
print("\n")

#applying comparison on string


print("applying comparison")
if s1==s2:
print("equal")
else:
print("not equal")
print("\n")

#applying membership on string


print("applying membership")
"Hello !! Sir !!" in s1

"Hello !! Sir !!" not in s2

applying concatenation
Hello !! Sir !!Welcome to python coding

applying repletion
Hello !! Sir !!Hello !! Sir !!Hello !! Sir !!

applying comparison
not equal

applying membership

True

#24 to count the number of character and word in string


s1="Welcome to python coding"
temp=0
for i in range(0,len(s1)):
if(s1[i]!=' '):
temp=temp+1
print("total number of character are:-",temp)

#counting words
str=s1.split()
print("total number of words are",len(str))

total number of character are:- 21


total number of words are 4

#25 to validate password


password=123
while (True):
user_password=int(input("enter the password"))
if (password == user_password):
print("Right password")
break
else:
print("Wrong password")
enter the password 234

Wrong password

enter the password 454

Wrong password

enter the password 645

Wrong password

enter the password 123

Right password

#26 to count vowels constants,white spaces and special characters in


a string
mystring=input("enter a string")
vowels=0
consonant=0
space=0
special=0
for i in range(0,len(mystring)):
ch=mystring[i]
if(ch=='a' or ch=='i' or ch=='o' or ch=='e' or ch=='u'):
vowels=vowels+1
elif(ch==' '):
space=space+1
elif(ch!='a' or ch!='i' or ch!='o' or ch!='e' or ch!='u' or
ch!=' '):
consonant=consonant+1
else:
special=special+1
print("Vowels:-",vowels)
print("Consonant:-",consonant)
print("space:-",space)
print("special character:-",special)

enter a string hello sir my name is sunny

Vowels:- 7
Consonant:- 14
space:- 5
special character:- 0

#27 to demonstrate the use of break continue and pass statements

print("Applying Break")
print("the multiplication are:-")
i =1
while i<=10:
print('6*',(i),'=',6*i)

if i>=5:
break
i = i + 1

print("Applying Continue")
print("The Prime number are:-")
num=0
while num<10:
num+=1

if(num%2)==0:
continue
print(num)

print("applying pass statement")


mylist=['a','b','c','d']
for i in mylist:
if(i=='a'):
pass
else:
print(i)

Applying Break
the multiplication are:-
6* 1 = 6
6* 2 = 12
6* 3 = 18
6* 4 = 24
6* 5 = 30
Applying Continue
The Prime number are:-
1
3
5
7
9
applying pass statement
b
c
d

#28. to demonstrate the use of the range function


x=range(3,20,2) #the number in range print(3,19) and the last (2) used
to increment by 2 instead of 1
for n in x:
print(n)
3
5
7
9
11
13
15
17
19

#29. to demonstrate the use of loop patterns(interactive loop,


sentinal loop, nested loop, file loop)
print("1. Apply Interative loop")
pw="123"
while True:
password=input("enter your password")
if(password==pw):
print("correct password")
break
else:
print("Wrong Password")

print("\n2. Applying Sentinal Loop")


sentinal=-1
n=int(input("enter n"))
while n!=sentinal:
print(n)
break
n=int(input("enter until we get sentinal"))
print("bye")

print("\n3. Applying nested loop")


for i in range(1,6):
for j in range(i):
print("*",end=" ")
print()

print("\n4.Applying file loop")


f = open("myfile.txt","r+")
print(f.read())
f.close()

1. Apply Interative loop

You might also like