Squares of even, cubes of odd

Squares of even, cubes of odd

EasyDictionary Programs · 7 of 94

Write a Python program that stores squares of even numbers and cubes of odd numbers in a dictionary using a comprehension.

Input
[4, 5, 6, 2, 1, 7, 11]
Output
{4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}

Approach

  1. A conditional expression picks the power.

Solution

Python
numbers = [4, 5, 6, 2, 1, 7, 11]print({n: n ** 2 if n % 2 == 0 else n ** 3 for n in numbers})
Output
{4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

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

Python String Programs with Solutions

Python Practice › String Programs

Python String Programs with Solutions

106 programs101 easy5 medium0 hard

106 string programs that cover slicing, searching, counting, replacing, splitting and joining text. They are among the most common Python interview questions, and each one has an explained solution with real output.

Before you start

Read the Strings & Methods and String Formatting & f-strings lesson first if the topic is new to you.

All 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