Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Python Programs

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Python Programs

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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(math.factorial(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


L1.append("finish")
print("List after append: ",L1)

L1.insert(0,L2)
print("List after insert: ",L1)

L1.extend(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
L2.sort(reverse=True)
print("List 2 in Descending order: ",L2)

L2.sort()
print("List 2 in Ascending order: ",L2)

#remove elements from list


L3.remove("blue")
print("List after removing 'blue': ",L3)

print("Element being popped: ",L3.pop(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")

You might also like