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

Python Programs for Number Operations

The document contains Python programs for various operations like finding the maximum and minimum of 3 numbers, checking if a number is Armstrong, even or odd, prime or palindrome, calculating factorial of a number and Fibonacci series. It also contains programs for string operations, list functions like append, insert, extend, sorting and removing elements, drawing patterns like half pyramid, full pyramid and diamond. Finally, it shows a program for linear search of an element in a list.

Uploaded by

shreyaxchauhan
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)
6 views6 pages

Python Programs for Number Operations

The document contains Python programs for various operations like finding the maximum and minimum of 3 numbers, checking if a number is Armstrong, even or odd, prime or palindrome, calculating factorial of a number and Fibonacci series. It also contains programs for string operations, list functions like append, insert, extend, sorting and removing elements, drawing patterns like half pyramid, full pyramid and diamond. Finally, it shows a program for linear search of an element in a list.

Uploaded by

shreyaxchauhan
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

Python Programs

Max of 3 Numbers
num1 = int(input("Enter Number: "))
num2 = int(input("Enter Number: "))
num3 = int(input("Enter Number: "))
if num1 >= num2 and num1 >= num3:
print("Largest Number: ",num1)
elif num2 >= num1 and num2 >= num3:
print("Largest Number: ",num2)
else:
print("Largest Number: ",num3)

Min of 3 Numbers
num1 = int(input("Enter Number: "))
num2 = int(input("Enter Number: "))
num3 = int(input("Enter Number: "))
if num1 <= num2 and num1 <= num3:
print("Smallest Number: ",num1)
elif num2 <= num1 and num2 <= num3:
print("Smallest Number: ",num2)
else:
print("Smallest Number: ",num3)

Check if number is Armstrong


num = int(input("Enter Number: "))
n = num
num_L = len(str(num))
arm =0
while num>0:
rem = num%10
arm = arm + rem ** num_L
num = num//10

if arm == n:
print("It is an Armstrong number")
else:
print("It is not an Armstrong number")
Check if number is Even or Odd
num = int(input("Enter Number: "))
if num%2 == 0:
print("It is an Even Number")
else:
print("It is an Odd number")

Check if number is Palindrome


num = int(input("Enter Number: "))
n = num
rev = 0
while num > 0:
rem = num%10
rev = rev*10 + rem
num = num // 10
if rev == n:
print("It is a Palindrome")
else:
print("It is not a Palindrome")

Check if number is Prime


num = int(input("Enter Number: "))
flag = 0
for i in range(2,num):
rem = num%i
if rem == 0:
flag = 1
break

if flag == 1:
print("It is not a Prime Number")
else:
print("It is a Prime Number")

Factorial of a Number
num = int(input("Enter Number: "))
fact=1
for i in range(1,num+1):
fact=fact*i
print(fact)

Factorial using Library Function


import math

num = int(input("Enter Number: "))


print([Link](num))

Fibonacci Series
num1 = 0
num2 = 1
n = int(input("How many series: "))
for i in range(n):
print(num1)
sum=num1+num2
num1=num2
num2=sum

String Operations
str1 = "exams are almost over"
str2 = "this "
str3 = "is "
str4 = "a "
str5 = "sentence "

#string slicing
str6 = str1[slice(5)]
print("First 5 elements of the string: ",str6)
print("String from 10th element: ",str1[10:])

#string concatination
str7 = str2 + str3 + str4 + str5
print("String after concatinating: ",str7)

#string repetition
str8 = str7*3
print("String being repeated 3 times: ", str8)
List Functions
L1 = [1,2,3,4,5]
L2 = [6,7,8,9]
L3 = ["red","blue","yellow"]

print("Original Lists: ","\n",L1,"\n",L2,"\n",L3)

#add elements to list


[Link]("finish")
print("List after append: ",L1)

[Link](0,L2)
print("List after insert: ",L1)

[Link](L3)
print("List after extend: ",L1)

#print list elements


print("List 0th Element: ",L1[0])
print("List 6th Element: ",L1[6])

#print 3 - red elements of list


print(L1[3:8])

#print list in reverse


print(L1[::-1])

#sort list
[Link](reverse=True)
print("List 2 in Descending order: ",L2)

[Link]()
print("List 2 in Ascending order: ",L2)

#remove elements from list


[Link]("blue")
print("List after removing 'blue': ",L3)

print("Element being popped: ",[Link](0))


print("List after Popping 0th Element: ",L3)
Draw Pattern
#half Pyramid
n = 5
for i in range(n+1):
for j in range(i):
print("*",end="")
j=j+1
print("\r")
i=i+1

print("\n")

#full pyramid
space = 5
sp_ch = 0
while space > 0:
print(' '*space,'*'+' *'*sp_ch)
space-=1
sp_ch+=1

print("\n")

#Diamond
space = 5
sp_ch = 0

while space > 0:


print(' '*space,'*'+' *'*sp_ch)
space-=1
sp_ch+=1
while sp_ch >= 0:
print(' '*space,'*'+' *'*sp_ch)
space+=1
sp_ch-=1
Linear Search of List
def search(a, n):
for i in range (len(a, n)):
if a[i] == n:
return True
return False

L1 = (1, 2, "loop", 3, 4, "is", 5, 6, "looping", 7, 8)


word = "looping"
if search (L1, word):
print("Found")
else:
print ("Not Found")

Common questions

Powered by AI

The program demonstrates slicing, concatenation, and repetition. Slicing extracts a substring, shown by str6 which is str1's first five elements. Concatenation combines multiple strings into one, producing str7 by joining str2, str3, str4, and str5. Repetition replicates str7 thrice to form str8 .

The diamond pattern is generated using nested loops, where spaces and asterisks print symmetrically. Initially, spaces are set to 5 and decrease while the number of asterisks (sp_ch) increases, forming the upper half. Then, sp_ch is decremented and spaces incremented to complete the lower half, creating a symmetrical diamond shape .

The program illustrates appending, inserting, and extending lists. append adds 'finish' to the end of L1. insert places L2 at the start of L1, nesting it within L1. extend incorporates all elements of L3 into L1. Each operation alters the length and structure of the list, affecting iteration and element access .

The program using a library function leverages Python's 'math' module, specifically math.factorial(num), to directly compute the factorial of a number, making it more efficient. In contrast, the manual method iteratively multiplies numbers from 1 to num, which is more prone to errors and less optimal for larger numbers .

The program initializes a flag to 0, then iterates through numbers from 2 to one less than the given number, checking for divisibility. If the input number is divisible by any of these numbers, the flag is set to 1, indicating non-prime. If the flag remains 0 throughout, the number is prime .

The program compares the three numbers using conditional statements. It first checks if the first number (num1) is greater than or equal to both the second (num2) and third number (num3). If true, it prints num1 as the largest. If not, it checks if num2 is greater than or equal to num1 and num3; if true, it prints num2 as the largest. Otherwise, num3 is the largest and is printed .

The program initializes the first two numbers of the Fibonacci series as num1=0 and num2=1. Using a loop that runs up to n times, it prints num1, then computes the next Fibonacci number as the sum of num1 and num2. It updates num1 and num2 to continue the series .

The program calculates the number of digits in the given number and stores it in num_L. It then initializes arm to 0 and uses a loop to extract each digit of the number. It raises each digit to the power of num_L, adds these to arm, and finally compares arm with the original number (n). If they are equal, the number is an Armstrong number .

The linear search function iterates through the elements of a list. It compares each element to the target value n. If a match is found, it returns True; otherwise, it proceeds until all elements are checked, ultimately returning False. It's used to find if the word "looping" is present in a tuple L1 .

The program reverses the input number by repeatedly extracting the last digit and building a reversed number. It compares this reversed number with the original. If they match, the number is a palindrome .

You might also like