Dictionary from two lists

Dictionary from two lists

EasyDictionary Programs · 6 of 94

Write a Python program to create a dictionary from a list of keys and a list of values.

Input
["a", "b", "c", "d", "e"], [12, 23, 24, 25, 15, 16]
Output
{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}

Approach

  1. dict(zip(keys, values)); zip stops at the shorter list.

Solution

Python
keys = ["a", "b", "c", "d", "e"]values = [12, 23, 24, 25, 15, 16]print(dict(zip(keys, values)))
Output
{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}

Line by line

Line 3

The extra value 16 is ignored because zip stops when the shorter list ends.

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Odd and even keys

Odd and even keys

EasyDictionary Programs · 5 of 94

Write a Python program to get lists of the items with even keys and with odd keys.

Input
{1: 25, 5: "abc", 8: "pqr", 21: "xyz", 12: "def", 2: "utv"}
Output
Even Key = [[8, 'pqr'], [12, 'def'], [2, 'utv']]
Odd Key = [[1, 25], [5, 'abc'], [21, 'xyz']]

Approach

  1. Filter items() by key % 2.

Solution

Python
d = {1: 25, 5: "abc", 8: "pqr", 21: "xyz", 12: "def", 2: "utv"}even = [[k, v] for k, v in d.items() if k % 2 == 0]odd = [[k, v] for k, v in d.items() if k % 2 == 1]print("Even Key =", even)print("Odd Key =", odd)
Output
Even Key = [[8, 'pqr'], [12, 'def'], [2, 'utv']]
Odd Key = [[1, 25], [5, 'abc'], [21, 'xyz']]

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Concatenate two dictionaries

Concatenate two dictionaries

EasyDictionary Programs · 4 of 94

Write a Python program to concatenate two dictionaries.

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

Approach

  1. | (Python 3.9+) creates a merged dict; update changes one in place.

Solution

Python
dict1 = {"Name": "Harry", "Rollno": 345, "Address": "Jordan"}dict2 = {"Age": 25, "salary": "$25k"}print(dict1 | dict2)dict1.update(dict2)print(dict1)
Output
{'Name': 'Harry', 'Rollno': 345, 'Address': 'Jordan', 'Age': 25, 'salary': '$25k'}
{'Name': 'Harry', 'Rollno': 345, 'Address': 'Jordan', 'Age': 25, 'salary': '$25k'}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Move items to another dictionary

Move items to another dictionary

EasyDictionary Programs · 3 of 94

Write a Python program to move all items from dict1 to dict2, leaving dict1 empty.

Input
{"name": "john", "city": "London", "country": "UK"}
Output
dict1 = {}
dict2 = {'name': 'john', 'city': 'London', 'country': 'UK'}

Approach

  1. update dict2, then clear dict1.

Solution

Python
dict1 = {"name": "john", "city": "London", "country": "UK"}dict2 = {}dict2.update(dict1)dict1.clear()print("dict1 =", dict1)print("dict2 =", dict2)
Output
dict1 = {}
dict2 = {'name': 'john', 'city': 'London', 'country': 'UK'}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Square of every value

Square of every value

EasyDictionary Programs · 2 of 94

Write a Python program to print the square of every value in a dictionary.

Input
{"a": 5, "b": 3, "c": 6, "d": 8}
Output
a : 25
b : 9
c : 36
d : 64

Approach

  1. Loop over items().

Solution

Python
d = {"a": 5, "b": 3, "c": 6, "d": 8}for key, value in d.items():    print(key, ":", value ** 2)
Output
a : 25
b : 9
c : 36
d : 64

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Add elements to a dictionary

Add elements to a dictionary

EasyDictionary Programs · 1 of 94

Write a Python program to add elements to a dictionary.

Input
{"name": "Asha"}
Output
{'name': 'Asha', 'city': 'Pune', 'age': 29, 'role': 'tester'}

Approach

  1. Assign to a new key, or use update() for several keys.

Solution

Python
person = {"name": "Asha"}person["city"] = "Pune"person.update({"age": 29, "role": "tester"})print(person)
Output
{'name': 'Asha', 'city': 'Pune', 'age': 29, 'role': 'tester'}

Line by line

Line 2

Assigning to a key that does not exist adds it.

Line 3

update adds (or replaces) several keys at once.

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Arithmetic operations on two numbers

Python Practice › Basic Programs › Program 40

Arithmetic operations on two numbers

EasyBasic Programs · 40 of 40

Write a Python program to perform all the basic arithmetic operations on two numbers.

Input
a = 17, b = 5
Output
Addition        22
Subtraction     12
Multiplication  85
Division        3.4
Floor division  3
Remainder       2
Power           1419857

Approach

  1. Apply each arithmetic operator and print the results in a table.

Solution

Python
a, b = 17, 5operations = {    "Addition": a + b,    "Subtraction": a - b,    "Multiplication": a * b,    "Division": a / b,    "Floor division": a // b,    "Remainder": a % b,    "Power": a ** b,}for name, value in operations.items():    print(f"{name:<15} {value}")
Output
Addition        22
Subtraction     12
Multiplication  85
Division        3.4
Floor division  3
Remainder       2
Power           1419857

Line by line

Lines 2-10

A dictionary maps each operation’s name to its result.

Lines 11-12

The loop prints each pair; :<15 pads the name to 15 characters so the values line up.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Volume of a sphere

Python Practice › Basic Programs › Program 39

Volume of a sphere

EasyBasic Programs · 39 of 40

Write a Python program to calculate the volume of a sphere. Volume = 4/3 × π × r³.

Input
r = 6
Output
Volume: 904.78

Approach

  1. Apply the formula with math.pi.

Solution

Python
import math r = 6volume = 4 / 3 * math.pi * r ** 3print("Volume:", round(volume, 2))
Output
Volume: 904.78

Line by line

Line 4

The radius is cubed (r ** 3).

Note

The original exercise wrote the formula as 4/3 × π × r² with π = 3. The correct formula uses r³, and math.pi gives an accurate π.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Square root of a number

Python Practice › Basic Programs › Program 38

Square root of a number

EasyBasic Programs · 38 of 40

Write a Python program to find the square root of a number using the math library.

Input
144, 2
Output
12.0
1.4142
12
4.0

Approach

  1. Use math.sqrt(), or math.isqrt() for a whole-number root.

Solution

Python
import math print(math.sqrt(144))print(round(math.sqrt(2), 4))print(math.isqrt(150))print(16 ** 0.5)
Output
12.0
1.4142
12
4.0

Line by line

Line 3

sqrt always returns a float.

Line 5

isqrt returns the whole-number part of the root (12 for 150).

Line 6

Raising to the power 0.5 also gives the square root.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

LCM of two numbers

Python Practice › Basic Programs › Program 37

LCM of two numbers

EasyBasic Programs · 37 of 40

Write a Python program to find the LCM (lowest common multiple) of two numbers.

Input
12 and 18
Output
LCM: 36

Approach

  1. LCM × HCF = a × b, so LCM = a × b // HCF.

Solution 1: using the HCF

Python
import math a, b = 12, 18lcm = a * b // math.gcd(a, b)print("LCM:", lcm)
Output
LCM: 36

Line by line

Line 4

12 × 18 = 216 and the HCF is 6, so the LCM is 36.

Solution 2: math.lcm

Python
import mathprint(math.lcm(12, 18), math.lcm(4, 6, 10))
Output
36 60

Line by line

Line 2

math.lcm (Python 3.9+) accepts any number of values.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs