Sunday, 2 November 2025

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

 


Code Explanation:

Importing Required Libraries
import pandas as pd
import statistics as st

pandas (imported as pd) is used for handling tabular data in DataFrames (like an Excel sheet).

statistics (imported as st) provides mathematical functions for mean, median, etc.

Together, they let us work with data and perform simple statistical calculations.

Creating a DataFrame
df = pd.DataFrame({
    "A": [10, 20, 30, 40],
    "B": [2, 4, 6, 8]
})

A DataFrame is created with two columns — A and B.

Column A: [10, 20, 30, 40]

Column B: [2, 4, 6, 8]

So the DataFrame looks like this:

A B
0 10 2
1 20 4
2 30 6
3 40 8

Creating a New Column “C”
df["C"] = df["A"] / df["B"]

This divides each value in column A by the corresponding value in column B.

Row by row:

10 / 2 = 5.0

20 / 4 = 5.0

30 / 6 = 5.0

40 / 8 = 5.0

So column C becomes [5.0, 5.0, 5.0, 5.0].

Now the DataFrame looks like:

A B C
0 10 2 5.0
1 20 4 5.0
2 30 6 5.0
3 40 8 5.0

Calculating the Mean of Column “C”
avg = st.mean(df["C"])

st.mean() calculates the average (arithmetic mean) of all values in column C.

Since all values are 5.0,
mean=(5+5+5+5)/4=5.0

So, avg = 5.0.

Printing the Result
print(int(avg + df["C"].median()))

df["C"].median() returns the middle value in column C.

All values are 5.0, so median = 5.0.

Add mean and median: 5.0 + 5.0 = 10.0

Convert to integer: int(10.0) → 10

Finally, it prints 10.

Final Output
10

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 (343) 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)