Skip to main content

Standard Normal Distribution with examples using Python

Tutorial on Python Classes with Example

Tutorial on Python Classes with Example

In the previous post, we introduced the topic of classes in Python. We will continue to develop these ideas in this post. We need to calculate the newspaper,egg and milk expenses for any person. Using this information, we will finally calculate the total expenses. Let's see this in action. Recall that our __init__ method allowed us to initialize name of the person, the kind of milk, eggs and newspaper puchased by the person.


def __init__(self,name,milk_type,newspaper_type,egg_type):
    self.name = name
    self.milk_type= milk_type
    self.newspaper_type = newspaper_type
    self.egg_type = egg_type

To calculate the egg expenses incurred, we define a method called calculate_egg_expenses. Recall that a function defined within a class is called a method. Code block below explains this.


def calculate_egg_expenses(self,number_of_eggs_per_day):
    if self.egg_type =="small":
      self.egg_expenses = Expenses.small_egg_rate*number_of_eggs_per_day * Expenses.num_days
    elif self.egg_type== "large":
      self.egg_expenses = Expenses.large_egg_rate * number_of_eggs_per_day * Expenses.num_days

We first check the type of egg which is opted for by the person. Accordingly, egg expenses can be calculated based on egg rate, number of eggs per day purchased and number of days in a month. For ease of explanation, we have assumed number of days in a month as 31.

Notice the self keyword passed to the method. It helps in creating egg_expenses specific to the class instance. Also , we can see that variables num_days, small_egg_rate, large_egg_rate are being accessed using the Expenses class. As explained in the previous post, we call these variables as class variables. However, it turns out, if you use self keyword instead of Expenses, it behaves in a similar way.

Why is that? The reason is, class variables by default become part of the instance of the class. So we can either use class name or self to access these variables. Code block below shows this.


def calculate_egg_expenses(self,number_of_eggs_per_day):
    if self.egg_type =="small":
      self.egg_expenses = self.small_egg_rate*number_of_eggs_per_day * self.num_days
    elif self.egg_type== "large":
      self.egg_expenses = self.large_egg_rate * number_of_eggs_per_day * self.num_days

In a similar way, we can calculate newspaper and milk expenses by creating below methods.


def calculate_newspaper_expenses(self):
    if self.newspaper_type == "deccan":
      self.newspaper_expenses = Expenses.deccan_herald_rate* Expenses.num_days
    elif self.newspaper_type == "toi":
      self.newspaper_expenses = Expenses.toi_rate* Expenses.num_days

def calculate_milk_expenses(self,litres_of_milk_per_day):
    if self.milk_type=="blue":
      self.milk_expenses= Expenses.blue_milk_rate* litres_of_milk_per_day * Expenses.num_days
    elif self.milk_type == "green":
      self.milk_expenses = Expenses.green_milk_rate * litres_of_milk_per_day * Expenses.num_days

We now define a method called total_expenses. It takes as input two parameters, litres_of_milk_per_day and number_of_eggs_per_day. When this method is called, it internally calls calculate_egg_expenses , calculate_milk_expenses and calculate_newspaper_expenses. After completion of each method, results are stored in egg_expenses , milk_expenses and newspaper_expenses . To reiterate, these variables are associated with the instance of the class as expenses can differ for different people. Final step is to calculate expenses which is a sum of the three variables just calculated.


def total_expenses(self,litres_of_milk_per_day, number_of_eggs_per_day):
    self.calculate_egg_expenses(number_of_eggs_per_day)
    self.calculate_milk_expenses(litres_of_milk_per_day)
    self.calculate_newspaper_expenses()
    self.expenses = self.egg_expenses+ self.newspaper_expenses+ self.milk_expenses

Finally, we need a method to see what was the expenses incurred under different sections. So, we just create a helper function to display this information.


def display_expenses(self):
    print(f"Milk expenses equal Rs {self.milk_expenses}")
    print(f"Egg expenses equal Rs {self.egg_expenses}")
    print(f"Newspaper expenses equal Rs {self.newspaper_expenses}")
    print(f"Total expenses equal Rs {self.expenses}")

This is how we would run a sequence of Python commands to display this information.


Rohit_expenses = Expenses(name = "Rohit Rudrapatna" , milk_type = "green", newspaper_type = "deccan",  egg_type = "small")
Rohit_expenses.total_expenses(litres_of_milk_per_day = 2,number_of_eggs_per_day= 2)
Rohit_expenses.display_expenses()
--->Milk expenses equal Rs 1302
    Egg expenses equal Rs 496
    Newspaper expenses equal Rs 186
    Total expenses equal Rs 1984

Hope this post gives you a gentle introduction to classes. In the upcoming posts, we will get into more details about OOPs.

Comments

Popular posts from this blog

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 2

In the previous post , we set up the context to utilize embeddings for categorical features. In this post, we will figure out how to create these embeddings and combine them with other continuous features to build a neural network model. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also available in Kaggle . Metadata about this dataset is available on the respective websites. To follow this post, it is recommended to download the dataset from Kaggle. Most of the features are self explanatory. Embedding Creation A few definitions first. Levels in a categorical feature represent unique values available for that categorical feature. For e.g. MARRIAGE has levels 0,1,2,3. Each level of a categorical feature is represented by a vector of numbers. So, if you stack up all the levels together and all the vectors together, you can imagine levels to be a colum

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1 In this post, we will talk about using embeddings for categorical features using PyTorch. This post will be broken down into following parts. Dataset Download Data Understanding Data Preprocessing Embedding Creation Define Dataset and Dataloaders in PyTorch Neural Network definition in PyTorch The Training Loop Model Validation The idea about using Embeddings from Categorical Features was first mooted during a Kaggle contest and a paper was also published on this. In the context of NLP and word embeddings, we represent each word in an n dimesnional vector space. In a similar way, we can represent any categorical feature in an n dimesnional vector space as well. 1. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also av

Standard Normal Distribution with examples using Python

Standard Normal Distribution with examples In our previous post, we talked about Normal Distribution and its properties . In this post, we extend those ideas and discuss about Standard Normal Distribution in detail. What is a Standard Normal Distribution? A Normal Distribution with mean 0 and standard deviation 1 is called a Standard Normal Distribution . Mathematicallty, it is given as below. Fig 1:Standard Normal Probability Distribution Function For comparison, have a look at the Normal Probability Distribution Function. If you substitute mean as 0 ,standard deviation as 1, you derive the standard normal probability distribution function Fig 2: Normal Probability Distribution Function Need for a standard normal probability distribution function We need to extract probability information about events that we are interested in. For this, first we need to convert any normal random variable