Introduction to Passing a Statement


A program contains many sections of code that perform various types of operations. You as the programmer create those sections and write code for what a section is supposed to do. Sometimes, when you are creating a section of code, you don't yet have or know what you want the section to do. In this case, you can set the body of that section with a special keyword named pass.

Passing a Function

When creating a function, if you don't have the code for it, write the pass keyword in its body. Here is an example:

def evaluate():


This would produce:

    Press any key to continue . . .

In reality, you can write the pass keyword in the body of a function and that keyword would not have any effect. Here is an example:

def process_loan():
    loan_amount   : float = 7495.95
    interest_rate : float = 17.75
    periods       : int   = 40

    iRate           : float = interest_rate / 100.00
    years           : float = periods / 12
    interest_amount : float = loan_amount * iRate * years
    future_value    : float = loan_amount + interest_amount


    print("\tWatts' A Loan")
    print(f"Loan Amount:      {loan_amount}")
    print(f"Interest Rate:    {interest_rate:.2f}%")
    print(f"Periods:          {periods} Months")


    print(f"Interest Amount:  {interest_amount:.2f}")
    print(f"Future Value:     {future_value:.2f}")


This would produce:

    Watts' A Loan
Loan Amount:      7495.95
Interest Rate:    17.75%
Periods:          40 Months
Interest Amount:  4435.10
Future Value:     11931.05
Press any key to continue . . .

So, as stated in the beginning, the real rearson you would use the pass keyword is if you want to create an empty function, a function for which you don't have a content.

Passing a Condition

When creating a conditional statement, if you don't want to specify what to do in it, specify its statement as pass. Here is an example:

nbr = 248.68
if nbr < 500:

Passing a Class


When you are creating a class, sometimes you don't yet have what to put in the class or you are not yet prepared to populate the class. In this case, you can start with an empty class. To do this, when creating the class, specify the content of the class with the pass keyword. Therefore, to create an empty class, specify its whole body as pass. Here is an example:

class Report:

After doing this, you can declare a variable of the class. Here is an example:

class Report:

rpt = Report()

Adding Member Variables to an Empty Class

After creating an empty class, you can add members to it. You can do this using a variable of the class. You add a member as if you were accessing it from an object of the class. If the member is a variable, you can assign the desired value to it. You can then use the member variable like we have done so far. Here are examples:

class Report:

rpt = Report()

rpt.revenues = 1528665
rpt.salaries = 535775 = 12545     = 12500
rpt.other_expenses = 327448

total_expenses = rpt.salaries + + + rpt.other_expenses
net_income     = rpt.revenues - total_expenses

print("\t=+=+= Fun Department Store =+=+=")
print("\tAll Revenues Sources:\t", rpt.revenues)
print("\tSalaries:\t", rpt.salaries)
print("\tOther Expenses:\t", rpt.other_expenses)
print("\tTotal Expenses:\t\t", total_expenses)
print("Net Income:\t\t\t", net_income)

This would produce:

        =+=+= Fun Department Store =+=+=
        All Revenues Sources:    1528665
        Salaries:        535775
        Supplies:        12545
        Rent:            12500
        Other Expenses:  327448
        Total Expenses:          888268
Net Income:                      640397
Press any key to continue . . .

Practical LearningPractical Learning: Ending the Lesson

Previous Copyright © 2021-2022, FunctionX Monday 06 September 2021 Next