Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, June 14, 2026

Quiz on "Modeling data distributions" (Unit 4, Jun 14th 2026)


See All: Questions For Statistics From Khan Academy
« Previously    Next »
1:

Code:
mean = 170.4
sd = 10

l = 145
lz = (l - mean) / sd
print(lz)

import statistics
lz_area = statistics.NormalDist(mu=0, sigma=1).cdf(lz)
print(lz_area)

h = 171
hz = (h - mean) / sd
print(hz)

hz_area = statistics.NormalDist().cdf(hz)

area_req = round(hz_area - lz_area,4)
print(area_req)



2:


mean = 80
sd = 9

proportion = 0.4

import statistics
z = statistics.NormalDist().inv_cdf(proportion)

print(z)

x = z * sd + mean

print(x)



3:

Code:
mean = 13.1
sd = 1.5

sd1 = (mean - sd, mean + sd)
print(sd1)

sd2 = (mean - 2 * sd, mean + 2 * sd)
print(sd2)

sd3 = (mean - 3 * sd, mean + 3 * sd)
print(sd3)

sd2_area = 0.95
sd3_area = 0.997

area_req = (sd3_area - sd2_area) / 2

print(area_req)

percentage_wise = round(area_req * 100, 4)
print(percentage_wise)

out = """
(11.6, 14.6)
(10.1, 16.1)
(8.6, 17.6)
0.02350000000000002
2.35
"""



4:

Code:
b = 2
h = 0.6

area = 0.5 * b * h

percentage_of_area = area * 100

print(percentage_of_area)



5:


Code:
mean_sales = 8000
sd_sales = 1500

mean_salary = 2000 + 0.3 * mean_sales

sd_salary = sd_sales * 0.3

print("mean_salary, sd_salary")
print(mean_salary, sd_salary)



6:




7:


area = 1
b = 6
h = area * 2 / b
print(h)



8:

def area_of_trapezium(b1, b2, h):
    return 0.5 * (b1 + b2) * h

b1 = 0.5
b2 = 0.75
h = 1

a = area_of_trapezium(b1, b2, h)
print(a)

print(round(a*100, 4))


b1 = 0.25
b2 = 0.5
h = 1

a = area_of_trapezium(b1, b2, h)
print(a)

print(round(a*100, 4))



9:




10:


mean = 1497
sd = 322

proportion = 0.85

import statistics
z = statistics.NormalDist().inv_cdf(proportion)

x = z * sd + mean

print(round(x, 4))

See All: Questions For Statistics From Khan Academy
« Previously    Next » Tags: Python,Mathematical Foundations for Data Science,Data Analytics,

Wednesday, June 10, 2026

What is a Class Factory?


All Questions From This Chapter    « Previously


explain this text from a book in layman terms (it's completely bouncing off of me):

The default class factory is type. When you write

class Dog:
    pass

Python roughly does:

Dog = type('Dog', (), {})




This is one of those Python lines that looks mysterious until you realize it's just showing what Python does behind the scenes.

Let's start with the code you normally write:

Python
class Dog:
pass

Most people think this creates a class using some special Python magic.

What actually happens is that Python asks another object called type to create the class.

Behind the scenes, Python roughly does:

Python
Dog = type('Dog', (), {})

Let's decode each piece.


Step 1: What is type?

You've probably used it like this:

Python
x = 5
print(type(x))

Output:

Python
<class 'int'>

So it seems like type() tells you the type of an object.

But here's the twist:

Python
print(type(int))

Output:

Python
<class 'type'>

Even classes themselves are objects.

And the thing that creates classes is type.

Think of it like this:

Dog object  <-- created by -- type
Cat object <-- created by -- type
Person object <-- created by -- type

type is the "class factory."


Step 2: The first argument

Python
type('Dog', (), {})

First argument:

Python
'Dog'

This is simply the class name.

Equivalent to:

Python
class Dog:
pass

Step 3: The second argument

Python
()

This is a tuple containing parent classes.

Example:

Python
class Dog:
pass

has no explicit parent class, so:

Python
()

means "no parent classes specified."

Another example:

Python
class Dog(Animal):
pass

would roughly become:

Python
Dog = type('Dog', (Animal,), {})

Notice the tuple contains Animal.


Step 4: The third argument

Python
{}

This dictionary contains everything inside the class body.

Example:

Python
class Dog:
species = "Canine"

def bark(self):
print("Woof")

becomes approximately:

Python
Dog = type(
'Dog',
(),
{
'species': 'Canine',
'bark': bark
}
)

The dictionary is basically:

attribute name -> value
method name -> function

Visualizing the process

When Python sees:

Python
class Dog:
pass

it internally thinks something like:

1. Create an empty dictionary {}
2. Put class variables and methods into it
3. Call type()
4. Store the result in Dog

So:

Python
class Dog:
pass

is roughly:

Python
Dog = type('Dog', (), {})

Proof

These two are essentially equivalent:

Normal syntax

Python
class Dog:
def bark(self):
print("Woof")

Using type

Python
def bark(self):
print("Woof")

Dog = type(
'Dog',
(),
{'bark': bark}
)

Both work:

Python
d = Dog()
d.bark()

Output:

Python
Woof

The big idea

There are objects, classes, and something that creates classes.

Think of a car factory:

Factory ---> creates Cars

In Python:

type ---> creates Classes
Class ---> creates Objects

Example:

Python
Dog = type('Dog', (), {})
type

Dog class

Dog()

dog object

This idea becomes important later when learning metaclasses, because a metaclass is simply a custom replacement for type that can control how classes themselves are created.


All Questions From This Chapter    « Previously