Getting a Pass
Getting a Pass
Introduction to Passing a Statement
Overview
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():
pass
evaluate()
print("==================================")
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 pass print("============================================") print("\tWatts' A Loan") print('--------------------------------------------') print(f"Loan Amount: {loan_amount}") print(f"Interest Rate: {interest_rate:.2f}%") print(f"Periods: {periods} Months") pass print('-------------------------------------------') print(f"Interest Amount: {interest_amount:.2f}") print(f"Future Value: {future_value:.2f}") process_loan() print("===========================================")
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:
pass
Passing a Class
Introduction
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:
pass
After doing this, you can declare a variable of the class. Here is an example:
class Report:
pass
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:
pass
rpt = Report()
rpt.revenues = 1528665
rpt.salaries = 535775
rpt.supplies = 12545
rpt.rent = 12500
rpt.other_expenses = 327448
total_expenses = rpt.salaries + rpt.supplies + rpt.rent + rpt.other_expenses
net_income = rpt.revenues - total_expenses
print("==================================================")
print("\t=+=+= Fun Department Store =+=+=")
print("==================================================")
print("Revenues")
print("\tAll Revenues Sources:\t", rpt.revenues)
print('--------------------------------------------------')
print("Expenses")
print("\tSalaries:\t", rpt.salaries)
print("\tSupplies:\t", rpt.supplies)
print("\tRent:\t\t", rpt.rent)
print("\tOther Expenses:\t", rpt.other_expenses)
print('--------------------------------------------------')
print("\tTotal Expenses:\t\t", total_expenses)
print("Net Income:\t\t\t", net_income)
print("==================================================")
This would produce:
================================================== =+=+= Fun Department Store =+=+= ================================================== Revenues All Revenues Sources: 1528665 -------------------------------------------------- Expenses Salaries: 535775 Supplies: 12545 Rent: 12500 Other Expenses: 327448 -------------------------------------------------- Total Expenses: 888268 Net Income: 640397 ================================================== Press any key to continue . . .
Practical Learning: Ending the Lesson
|
|||
Previous | Copyright © 2021-2024, FunctionX | Tuesday 06 August 2024, 17:10 | Next |
|