0% found this document useful (0 votes)
75 views6 pages

Nandini 2

The document contains Python code that performs various operations like: 1) Comparing two numbers and printing the larger number 2) Finding the largest of three numbers 3) Printing patterns using loops 4) Calculating sum of a series 5) Checking if a number is perfect, Armstrong or palindrome 6) Generating Fibonacci series 7) Checking if a number is prime 8) Finding GCD and LCM of two numbers 9) Performing string operations like counting vowels, consonants etc. 10) Sorting elements in a list.

Uploaded by

Shaurya Yadav
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)
75 views6 pages

Nandini 2

The document contains Python code that performs various operations like: 1) Comparing two numbers and printing the larger number 2) Finding the largest of three numbers 3) Printing patterns using loops 4) Calculating sum of a series 5) Checking if a number is perfect, Armstrong or palindrome 6) Generating Fibonacci series 7) Checking if a number is prime 8) Finding GCD and LCM of two numbers 9) Performing string operations like counting vowels, consonants etc. 10) Sorting elements in a list.

Uploaded by

Shaurya Yadav
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/ 6

print ("welcome user") #welcome message

n1= int(input("enter the first number")) #displaying the larger number from two
numbers
n2= int(input("enter the second number"))
if n1>n2:
print (n1,"is greater")
else:
print (n2,"is greater")

n3= int(input("enter the first number")) #displaying the larger number from three
numbers
n4= int(input("enter the second number"))
n5= int(input("enter the third number"))
if n3>n4>n5:
print (n3," is largest number")
if n4>n3>n5:
print (n4, "is the largest number")
else: print (n5,"is the largest number")

#pattern1
n=1
for a in range(7):
print(n)
n = n*10+1

#pattern2
for i in range (7):
for j in range(7, i, -1):
print(j, end=" ")
else:
print()
#pattern3

n=5
k=round(n/2)*2
for i in range (0, n, 2):
for j in range(0, k+1):
print(end=" ")
for j in range(0, i+1):
print("*", end="")
k =k-2
print()
k=1
for i in range(n-1, 0,-2):
for j in range(0, k+2):
print(end=" ")
for j in range(0,i-1):
print("*", end="")
k=k+2
print()

x=int(input("enter value of x: ")) #sum of the series


n=int(input("enter the power (n): "))
s = x
sign = +1
for a in range(2, n+1):
f = 1
for i in range(1, a+1) :
f *=i
term = ((x**a)*sign)/f
s += term
sign *= -1
print("sum of first", n, "terms", s)

n= int(input("enter the number")) #checking if a number is perfect number or


not
sum = 0
for i in range (1, n):
if n%i==0:
sum=sum+i

if sum==n:
print(n, "is perfect number")
else :
print(n,"is not a perfect number")

n= int(input("enter any number")) #checking if a number is an armstrong number


or not
temp = n
total = 0
while temp > 0 :
digit = temp %10
total = total + (digit**3)
temp = temp//10
if n == total:
print( n, "is an armstrong number")
else :
print( n, "is not an armstrong number")

n = int(input("enter any number")) #checking if a number is a palindrome number


or not
temp = n
rev = 0
while n > 0:
d = n % 10
rev = rev *10 + d
n = n//10
if temp == rev :
print( temp, "is a palindrome number")
else :
print (temp, "is not a palindrome number")

t = int(input("how many ternms?(enter 2+ value):")) #fibonacci series


first = 0
second = 1

print("Fibonacci series is:")


print(first, "," , second, end=", ")

for i in range(2, t):


next = first+second
print(next, end=", ")
first = second
second = next

n= int(input("enter any number")) #checking if a number if


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

n1 = int(input("enter the first number")) # common divisor and least


common multiple of two integers
n2 = int(input("enter the second number"))
x = n1
y = n2
while (n2!=0):
t = n2
n2 = n1 % n2
n1 = t
gcd = n1
print("GCD of {0} and {1} = {2}".format(x,y,gcd))
lcm = (x*y)/gcd
print ("LCM of {0} and {1} = {2}".format(x,y,lcm))
str = input("Type the string: ") #string manipulation
vowel_count=0
consonants_count=0
vowel = set("aeiouAEIOU")
for alphabet in str:
if alphabet in vowel:
vowel_count=vowel_count +1
elif alphabet == chr(32):
consonants_count=consonants_count
else:
consonants_count=consonants_count+1

print("Number of Vowels in ",str," is :",vowel_count)


print("Number of Consonants in ",str," is :",consonants_count)
# Upper and lower case count
uppercase_count=0
lowercase_count=0
for elem in str:
if elem.isupper():
uppercase_count += 1
elif elem.islower():
lowercase_count += 1
print("Number of UPPER Case in ",str,"' is :",uppercase_count)
print("Number of lower case in ",str,"' is :",lowercase_count)

str_1 = input ("Enter the string to check if it is a palindrome: ") #palindrome

str_1 = str_1.casefold ()

rev_str = reversed (str_1)

if (list, str_1) == list (rev_str):

print ("The string is a palindrome.")

else:

print ("The string is not a palindrome.")

def check_palin (str):

for i in range (0, int (len (str)/2)):

if str [i] != str [len (str) -i-1]:


return False

return True

str_1 = input ("Enter a string.")

ans = check_palin (str_1)

if (ans):

print ("Yes, it is a palindrome.")

else:

print ("No, it is not a palindrome.")

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
#list/tuple
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)

NumList.sort()

print("The Smallest Element in this List is : ", NumList[0])


print("The Largest Element in this List is : ", NumList[Number - 1])

mylist = [] #swapping elements


print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
# printing original list
print("The original list : " + str(mylist))
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(mylist)):
if i % 2:
even_i.append(mylist[i])
else :
odd_i.append(mylist[i])
result = odd_i + even_i
# print result
print("Separated odd and even index list: " + str(result))

mylist = [] #searching element in list or tuple


print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)

#largest and smallest number in a list


length = len(list1)
list1.sort()
print("Largest element is:", list1[length-1])
print("Smallest element is:", list1[0])
print("Second Largest element is:", list1[length-2])
print("Second Smallest element is:", list1[1])

no_of_std = int(input("Enter number of students: ")) #dictonary with names of


students who scored 75 above marks
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks
is/are",(result[student][0]))

You might also like