Friday, 12 September 2025

Python Coding challenge - Day 728| What is the output of the following Python Code?

 


Code Explanation:

1. Importing the json module
import json

The json module in Python allows us to work with JSON data.

It provides methods to serialize (convert Python objects → JSON string) and deserialize (convert JSON string → Python objects).

2. Creating a Python dictionary
data = {"a": 1, "b": 2}

Here, data is a dictionary with two key-value pairs:

"a": 1

"b": 2

3. Converting dictionary to JSON string
js = json.dumps(data)

json.dumps(data) converts the dictionary into a JSON-formatted string.

Example: {"a": 1, "b": 2} → '{"a": 1, "b": 2}' (notice it becomes a string).

4. Converting JSON string back to dictionary
parsed = json.loads(js)

json.loads(js) parses the JSON string back into a Python dictionary.

So parsed again becomes: {"a": 1, "b": 2}.

5. Adding a new key-value pair
parsed["c"] = parsed["a"] + parsed["b"]

A new key "c" is added to the dictionary.

Its value is computed as 1 + 2 = 3.

Now parsed = {"a": 1, "b": 2, "c": 3}.

6. Printing dictionary length and value
print(len(parsed), parsed.get("c", 0))

len(parsed) → counts number of keys in the dictionary = 3.

parsed.get("c", 0) → safely retrieves the value of "c". If "c" didn’t exist, it would return 0.

Here, "c" exists with value 3.

Final Output:
3 3

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)