Python Intermediate Programs by Devansh Mishra.

Some normal Python programs are given below and is very useful to Python beginner's. 


1. Calculator Program



operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')

number_1 = int(input('Enter your first number: '))
number_2 = int(input('Enter your second number: '))

print('{} + {} = '.format(number_1, number_2))
print(number_1 + number_2)

print('{} - {} = '.format(number_1, number_2))
print(number_1 - number_2)

print('{} * {} = '.format(number_1, number_2))
print(number_1 * number_2)

print('{} / {} = '.format(number_1, number_2))
print(number_1 / number_2)
     

2. Find vowels in Given String using Python


a_string = input("Enter String ")
lowercase = a_string.lower()

vowel_counts = {}
for vowel in "aeiou":
    count = lowercase.count(vowel)
    vowel_counts[vowel] = count

counts = vowel_counts.values()
total_vowels = sum(counts)
print("total vowels in string" ,total_vowels)
     

3. Find two strings are Anagram using Python


s1=input("Enter first string:")
s2=input("Enter second string:")
if(sorted(s1)==sorted(s2)):
      print("The strings are anagrams.")
else:
      print("The strings aren't anagrams.")
     


4. Remove whitespace from string using Python


def remove(string): 
 return string.replace(" ", "") 
string = input("Enter String with Spaces : ")
print(remove(string)) 
     


5. Snap two strings


str1 = input("Enter First String :")
str2 = input("Enter Second String :")  
   
print("Strings before swapping: " + str1 + " " + str2)
   
str1 = str1 + str2 
 
str2 = str1[0 : (len(str1) - len(str2))]
  
str1 = str1[len(str2):]
   
print("Strings after swapping: " + str1 + " " + str2)
     

6. Binary to Hexadecimal using Python


dec = int(input("Enter a decimal number: "))  
print(hex(dec),"in hexadecimal.")  
     

7. Largest no. From array


arr1 = [5, 1, 70, 75, 56, 78, 40]   
max = arr1[0] 
for i in range(0, len(arr1)):    
   if(arr1[i] > max):    
       max = arr1[i]   
           
print("Largest element : " + str(max));
     


8. Sort given unsorted array using Python


arr = [5, 2, 8, 7, 1];     
temp = 0;    
     
print("Elements of original array: ");    
for i in range(0, len(arr)):    
    print(arr[i], end=" ");    
     
for i in range(0, len(arr)):    
    for j in range(i+1, len(arr)):    
        if(arr[i] > arr[j]):    
            temp = arr[i];    
            arr[i] = arr[j];    
            arr[j] = temp;    
     
print();    
print("Elements of array sorted in ascending order: ");    
for i in range(0, len(arr)):    
    print(arr[i], end=" ");
    
     

9. Reverse a string


txt =input("Enter String: ")[::-1]
print(txt)


10. Count Character in String


string = "How you doing?"
substring = "i"

count = string.count(substring)

print("The count is:", count)
    


11.  Area and Perimeter of triangle


l=int(input("Length : "))
w=int(input("Width : "))
area=l*w
perimeter=2*(l+w)
print("Area of Rectangle : ",area)
print("Perimeter of Rectangle : ",perimeter)
    

12. Perfect no. 


def perfect_number(n):
    sum = 0
    for x in range(1, n):
        if n % x == 0:
            sum += x
    return sum == n
print(perfect_number(7))
    


13. Check given character is capital, small, digit using Python


ch = input("Please Enter Your Own Character : ")

if(ord(ch) >= 48 and ord(ch) <= 57): 
    print("The Given Character ", ch, "is a Digit") 
elif((ord(ch) >= 65 and ord(ch) <= 90) or (ord(ch) >= 97 and ord(ch) <= 122)):
    print("The Given Character ", ch, "is an Alphabet")
else:
    print("The Given Character ", ch, "is a Special Character")
    

14. Matrix Multiplication


X = [ [4,2],[3,4],[7,8] ]

Y = [ [8,3,5],[4,7,9] ]

result = [ [0,0,0],[0,0,0],[0,0,0] ]

my_list = []

for i in range( len(X) ):
   for j in range(len(Y[0])):
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]
  
for r in result:
   print(r)
  
  
15. Reverse linked text

reverse linked list using Python



class Node:

    def __init__(self, data):
        self.data = data
        self.next = None


class LinkedList:

    def __init__(self):
        self.head = None

    def reverse(self):
        if self.head is None or self.head.next is None:
            return

        prev = None
        cur = self.head

        while cur:
            next_element = cur.next
            cur.next = prev
            prev = cur
            cur = next_element

        self.head = prev

    def push(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

    def print_list(self):
        cur = self.head
        l1 = []
        while cur:
            l1.append(cur.data)
            cur = cur.next
        return l1


head = LinkedList()
head.push(3)
head.push(4)
head.push(5)
head.push(6)
print("Given list is: ", head.print_list())
head.reverse()
print("Reversed list is: ", head.print_list())

    

>>> 
Given list is: [6, 5, 4, 3]
Reversed list is: [3, 4, 5, 6]



16. Sort string alphabetically using Python


my_str = input("Enter String : ")
words = [word.lower() for word in my_str.split()]

words.sort()

print("The sorted words are:")
for word in words:
   print(word)
    


Comments

Popular posts from this blog

SWAG ANIMATION WITH SOUND

Chandrayan 3 Mission Special :Animation

Python Basic Programs By Devansh Mishra.