Program to get a list of odd and even keys from the dictionary

In this python dictionary program, we will get a list of odd and even keys from the dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over the keys in the dictionary.
  3. Use an if-else statement separate odd and even keys from the dictionary.
  4. Print the output.
				
					dict1 = {1:25,5:'abc',8:'pqr',21:'xyz',12:'def',2:'utv'}

list1 = [[val,dict1[val]] for val in dict1 if val%2 == 0]
list2 = [[val,dict1[val]] for val in dict1 if val%2 != 0]

print("Even key = ",list1)
print("Odd key = ",list2)
				
			

Output :

				
					Even key =  [[8, 'pqr'], [12, 'def'], [2, 'utv']]
Odd key =  [[1, 25], [5, 'abc'], [21, 'xyz']]
				
			

concatenate two dictionaries

create a dictionary from two lists.

Program to concatenate two dictionaries

In this python dictionary program, we will concatenate two dictionaries to form a one dictionary.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Concatenate two dictionaries to form a single dictionary using update().
  3. Print the output.
				
					dict1 = {'Name':'Harry','Rollno':345,'Address':'Jordan'}
dict2 = {'Age':25,'salary': '$25k'}

dict1.update(dict2)

print(dict1)
				
			

Output :

				
					{'Name': 'Harry', 'Rollno': 345, 'Address': 'Jordan', 'Age': 25, 'salary': '$25k'}
				
			

move items from dict1 to dict2.

get a list of odd and even keys from the dictionary

Move items from one dictionary to another.

In this python dictionary program, we will move items from one dictionary to another dictionary.

Steps to solve the program
  1. Take one dictionary as input and create another empty dictionary.
  2. Use for loop to iterate over input dictionary.
  3. Add values from the input dictionary to the empty dictionary.
  4. Print the output.
				
					dict1 = {'name':'john','city':'Landon','country':'UK'}
dict2 = {}
temp = dict1.copy()

for val in temp:
    v1 = dict1.pop(val)
    dict2[val] = v1

print("dictionary 2 :", dict2)
print("dictionary 1 :", dict1)

				
			

Output :

				
					dictionary 2 : {'name': 'john', 'city': 'Landon', 'country': 'UK'}
dictionary 1 : {}
				
			

print the square of all values in a dictionary.

concatenate two dictionaries

Print the square of all values in a dictionary.

In this python dictionary program, we will print the square of all values in a dictionary.

Steps to solve the program
  1. Take a dictionary as input.
  2. Use for loop to iterate over the dictionary.
  3. During iteration print the key and square of its value using **2.
  4. Print the output.
				
					dictionary = {'a': 5, 'b':3, 'c': 6, 'd': 8}

for val in dictionary:
    print(val,":", dictionary[val]**2)
				
			

Output :

				
					a : 25
b : 9
c : 36
d : 64
				
			

add elements to the dictionary.

move items from dict1 to dict2.

Python program to add elements to the dictionary. 

In this python dictionary program, we will add elements to the dictionary.

Steps to solve the program
  1. Create an empty dictionary by using {}.
  2. Add elements to the dictionary.
  3. To add elements use the following format: Dictionary_name[key_name] = value.
  4. Print the output.

Output :

				
					dictionary = {}
dictionary["Name"] = "Ketan"
dictionary["Age"] = "21"

print(dictionary)
				
			
				
					{'Name': 'Ketan', 'Age': '21'}
				
			

print the square of all values in a dictionary.

Program to perform mathematical operations on two numbers.

In this python basic program, we will perform mathematical operations on two numbers defined by the user.

Steps to solve the program
  1. Take two numbers and a mathematical operation of your choice (+,-,*,/) as input.
  2. Using if-else statements perform the corresponding operations on numbers.
  3. Print the output.
				
					num1 = float(input("Enter number 1: "))
num2 = float(input("Enter number 2: "))
operation = input("Enter operation of your choice: ")

if operation == "+":
    print(num1+num2)
elif operation == "-":
    print(num1-num2)
elif operation == "/":
    print(num1/num2)
elif operation == "*":
    print(num1*num2)
else:
    print("Invalid operation")
				
			

Output :

				
					Enter number 1: 25
Enter number 2: 5
Enter operation of your choice: /
5.0
				
			

calculate the volume of a sphere.

Python program to calculate the volume of a sphere

In this python basic program, we will calculate the volume of a sphere using the formula (4/3*pi*r^2).

Steps to solve the program
  1. Take radius as input through the user.
  2. Calculate the volume of a sphere using the formula (4/3*pi*r^2).
  3. Where pi is equal to 3.14 and r is the radius of the sphere.
  4. Print the output.
				
					r = float(input("Enter radius of the Sphere: "))
volume = 4/3*(3.14*r**2)

print("Volume of Sphere: ",volume)
				
			

Output :

				
					Enter radius of the Sphere: 5
Volume of Sphere:  104.66666666666666
				
			

find the square root of a number.

perform mathematical operations on two numbers.

Python program to find the LCM of two numbers.

In this python basic program, we will find the LCM two numbers defined by the user.

Steps to solve the program
  1. Take two numbers as input through the user.
  2. Using an if-else statement check which number is greater. 
  3. Use While loop.
  4. Inside the While loop check whether both numbers divide the greater number or not.
  5. If yes then break the loop otherwise, add 1 to the greater number and let the loop continue.
  6. At the end print the output.
				
					num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

if num1 > num2:
       greater = num1
else:
    greater = num2

while(True):
    if((greater % num1 == 0) and (greater % num2 == 0)):
        lcm = greater
        break
    greater += 1
    
print(f"L.C.M of {num1} and {num2}: {lcm}")
				
			

Output :

				
					Enter number 1: 150
Enter number 2: 200
L.C.M of 150 and 200: 600
				
			

Program to find HCF

find the square root of a number.

Python program to find HCF of two numbers.

In this python basic program, we will find the HCF of two numbers defined by users.

Steps to solve the program
  1. Take two numbers as input through the user.
  2. Using an if-else statement check which number is smaller.
  3. Use for loop with the range function to iterate over numbers from 1 to the smaller number.
  4. During iteration check whether the iterated number divides both input numbers, if yes then assign that number to the variable.
  5. After the loop is finished print the output.
				
					num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

if num1 > num2:
    smaller = num2
else:
    smaller = num1

for i in range(1, smaller+1):
    if((num1 % i == 0) and (num2 % i == 0)):
        hcf = i 

print(f"H.C.F of {num1} and {num2}: {hcf}")
				
			

Output :

				
					Enter number 1: 150
Enter number 2: 200
H.C.F of 150 and 200: 50
				
			

find the sum of natural numbers.

Program to find LCM