0% found this document useful (0 votes)
8 views10 pages

Python Programs for Basic Operations

The document contains ten Python programs that perform various tasks such as displaying a welcome message, comparing numbers, checking for prime numbers, generating Fibonacci series, calculating GCD and LCM, counting characters in a string, checking for palindromes, and searching for elements in a list. Each program includes user input and outputs results based on the logic implemented. The programs demonstrate fundamental programming concepts and operations in Python.

Uploaded by

Ansh Singh
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)
8 views10 pages

Python Programs for Basic Operations

The document contains ten Python programs that perform various tasks such as displaying a welcome message, comparing numbers, checking for prime numbers, generating Fibonacci series, calculating GCD and LCM, counting characters in a string, checking for palindromes, and searching for elements in a list. Each program includes user input and outputs results based on the logic implemented. The programs demonstrate fundamental programming concepts and operations in Python.

Uploaded by

Ansh Singh
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

Program1-input a welcome message and display it

message=input("Enter welcome message : ")

print("Hello, ",message)
program2- input two numbers and display the larger/smaller number

num1 = int(input(“Enter first number”))

num2 = int(input(“Enter second number”))

if num1>num2:

print(num1 , "is greater")

else:

print(num1, "is smaller")

if num2>num1:

print(num2 , "is greater")

else:

print(num2 , "is smaller")


program3- input 3 numbers and display larger/smaller number

n1 = int(input("Enter first number"))

n2 = int(input("Enter second number"))

n3 = int(input("Enter third number"))

# Find largest

if (n1 > n2) and (n1 > n3):

large = n1

elif (n2 > n1) and (n2 > n3):

large = n2

else:

large = n3

# Find smallest

if (n1 < n2) and (n1 < n3):

small = n1

elif (n2 < n1) and (n2 < n3):

small = n2

else:

small = n3

print("The large number is", large, "\n")

print("The small number is", small, "\n")


program4- input a number and check if the number is a prime or composite number

num = int(input("Enter any number : "))

if num > 1:

for i in range(2, num):

if (num % i) == 0:

print(num, "is NOT a PRIME number, it is a COMPOSITE number")

break

else:

print(num, "is a PRIME number")

elif num == 0 or 1:

print(num, "is a neither Prime NOR Composite number")


program5-display the terms of Fibonacci series

n = 10

num1 = 0

num2 = 1

next_number = num2

count = 1

while count <= n:

print(next_number, end=" ")

count += 1

num1, num2 = num2, next_number

next_number = num1 + num2

print()
program6-compute the greatest common divisor and least common multiple of two

integers

def gcd(a, b):

# Find minimum of a and b

result = min(a, b)

while result:

if a % result == 0 and b % result == 0:

break

result -= 1

# Return the gcd of a and b

return result

# Driver Code

if __name__ == '__main__':

a = 98

b = 56

print(f"GCD of {a} and {b} is {gcd(a, b)}")


Program7-count and display the numbrs of vowels,consonents,upper case,lower

Case in a string

str = input("Type the string: ")

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 [Link]():

uppercase_count += 1

elif [Link]():

lowercase_count += 1

print("Number of UPPER Case in ",str,"' is :",uppercase_count)

print("Number of lower case in ",str,"' is :",lowercase_count)


program8-input a string and determine whether it is a palindrome or not

def isPalindrome(s):

left = 0

right = len(s)-1

while left < right:

if not s[left].isalnum():

left += 1

elif not s[right].isalnum():

right -= 1

elif s[left].lower() != s[right].lower():

return False

else:

left += 1

right -= 1

return True

if __name__ == "__main__":

str = "ABCDCBA"

ans = isPalindrome(str)

if ans == True:

print("Palindrome")

else:

print("Not Palindrome")
program9-find the largest/smallest number in a list

list1 = [7, 10, 12, 3, 100, 95]

print("Smallest element is:", min(list1))

print("Largest element is:", max(list1))


program10-input a list of elements, search for a given element in the list

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

length=len(lst)

element=int(input("Enter element to be searched for:"))

for i in range(0,length):

if element==lst[i]:

print(element,"found at index",i)

break

else:

print(element,"not found in given list")

You might also like