None of the Values
None of the Values
None Fundamentals
Introduction
Python is an inferred language. This means that, when you declare a variable, if you want the compiler to know what amount of computer memory it must reserve for the variable, you should (must) initialize that variable. Here is an example:
item_name = "Premium Comfort Khaki Pant"
After declaring the variable, you can use it as you see fit. For example, you can pass it to a function or you can display its values to a user. Here is an example:
item_name = 'Premium Comfort Khaki Pant' print('Item Name:', item_name) print("=====================================")
Sometimes, at the time you are declaring a variable, you don't have a value for it. You only know that you will need that variable. In this case, you can simply indicate the variable by providing its value, but you must not use that variable until you initialize it (if you use a variable that has not yet been initialized, you would receive an error; we haven't studied yet how to deal with errors in code).
Sometimes you are declaring a variable, you know that you will need a value but you are not ready to specify that value. In this case, to indicate that you simply want the compiler to reserve an area of the computer memory but you don't (yet) have the necessary value for the variable, initialize that variable with a keyword named None. This can be done as follows:
something = None
We already know that, at any time, to display the value of a variable, you can pass it to the print() function. This also applies to a variable that holds None. This can be done as follows:
something = None print("Something:", something) print("=================================")
This would produce:
Something: None ================================= Press any key to continue . . .
Comparing Something to None
We have seen that, when declaring a variable, if you are not ready to specify its value, you can assign None to it. Once you have a value for the variable, you can update that variable by assigning it the desired value. At any time too, if you don't want the variable to hold a value, you can assign None to it. Here are examples:
item_name = None print("Item Name:", item_name) print("----------------------------------------------------") item_name = "Padded Shoulder Midi Dress" print("Item Name:", item_name) print("----------------------------------------------------") item_name = None print("Item Name:", item_name) print("----------------------------------------------------") item_name = "Dri-FIT Challenger Hybrid Running Shorts" print("Item Name:", item_name) print("====================================================")
This would produce:
Item Name: None ---------------------------------------------------- Item Name: Padded Shoulder Midi Dress ---------------------------------------------------- Item Name: None ---------------------------------------------------- Item Name: Dri-FIT Challenger Hybrid Running Shorts ==================================================== Press any key to continue . . .
Because of this situation, sometimes, before processing the value of a variable, you may want to check the status of that variable. In some cases, you want to know whether the variable currently holds a valid value. To do this, you can use the == operator to find out whether the value of the variable is, or is not, None. Here is an example:
item_name = None if item_name == None: print("To start, the item doesn't have a name.") print("----------------------------------------------------") if item_name == None: item_name = "Padded Shoulder Midi Dress" print("Item Name:", item_name) print("----------------------------------------------------") item_name = None if item_name == None: print("At this time, the item has no identification.") print("----------------------------------------------------") item_name = "Dri-FIT Challenger Hybrid Running Shorts" print("Item Name:", item_name) print("====================================================")
This would produce:
To start, the item doesn't have a name. ---------------------------------------------------- Item Name: Padded Shoulder Midi Dress ---------------------------------------------------- At this time, the item has no identification. ---------------------------------------------------- Item Name: Dri-FIT Challenger Hybrid Running Shorts ==================================================== Press any key to continue . . .
Checking that Somthing is not None
The != operator is used to find out that something is not the value it's supposed to be. One way to use this operator is to find out whether a variable is currently holding a valid value. You can do this by comparing a variable to None. Here is an example:
item_name = None if item_name != None: print("Item Name:", item_name) print("----------------------------------------------------") if item_name == None: item_name = "Padded Shoulder Midi Dress" print("Item Name:", item_name) print("----------------------------------------------------") item_name = None if item_name != None: print("Item Name:", item_name) print("----------------------------------------------------") item_name = "Dri-FIT Challenger Hybrid Running Shorts" print("Item Name:", item_name) print("====================================================")
This would produce:
Item Name: Padded Shoulder Midi Dress ---------------------------------------------------- Item Name: Dri-FIT Challenger Hybrid Running Shorts ==================================================== Press any key to continue . . .
None With Functions
Checking What a Variable is
As another way to find out the status of a variable, you can use an operator named is. You can use this operator in place of the == operator. The formula to find out whether a variable is None is:
instance-object is None
You can use a conditional statement to obtain this information. Once you know the status of a variable, you can take an appropriate action. Here is an example:
item_name = None if item_name is None: print("To start, the item doesn't have a name.") print("----------------------------------------------------") if item_name is None: item_name = "Padded Shoulder Midi Dress" print("Item Name:", item_name) print("----------------------------------------------------") item_name = None if item_name is None: print("At this time, the item has no identification.") print("----------------------------------------------------") item_name = "Dri-FIT Challenger Hybrid Running Shorts" print("Item Name:", item_name) print("====================================================")
Checking Whether a Variable is NOT None
For the opposite to checking whether a variable is currently holding a valid value, you can use an operator named not. To apply it, you can use it after the is operator. Once you get the result, you can take an appropriate action. Here is an example:
item_name = None if item_name is not None: print("Item Name:", item_name) print("----------------------------------------------------") if item_name is None: item_name = "Padded Shoulder Midi Dress" print("Item Name:", item_name) print("----------------------------------------------------") item_name = None if item_name is not None: print("Item Name:", item_name) print("----------------------------------------------------") item_name = "Dri-FIT Challenger Hybrid Running Shorts" print("Item Name:", item_name) print("====================================================")
Indicating None Returned
Most of the functions you create perform their operations and stop there. This means that those functions do something that other sections of code can refer to, but those functions don't produce a result that other functions need for their internal use. Such functions are said to produce nothing. As seen in previous sections, the Nothing concept is represented in the Python language with the word None. Therefore, to indicate that a certain function is not meant to produce any significant value, specify its return type as None. Here are two examples:
def identify_employee() -> str: fname : str = "" lname : str = "" print("Employee Identification") fname = input("First Name: ") lname = input("Last Name: ") return fname + " " + lname def get_gross_salary() -> float: sal : float = 0.00 sal = float(input("Gross Salary: ")) return sal def evaluate_tax() -> None: print("============================================") print("\t - Idaho - State Income Tax -") print("============================================") added_amount : float tax_rate : float gross_salary : float print("Enter the information for tax preparation") employee : str = identify_employee() gross_salary : float = get_gross_salary() if gross_salary >= 11_760: tax_rate = 6.925 added_amount = 543.90 if gross_salary < 11_760: tax_rate = 6.625 added_amount = 284.20 if gross_salary < 7_840: tax_rate = 5.625 added_amount = 196.00 if gross_salary < 6_272: tax_rate = 4.625 added_amount = 123.48 if gross_salary < 4_704: tax_rate = 3.625 added_amount = 66.64 if gross_salary < 3_136: tax_rate = 3.125 added_amount = 17.64 if gross_salary < 1_568: tax_rate = 1.125 added_amount = 0 tax_amount = added_amount + (gross_salary * tax_rate / 100.00) net_pay = gross_salary - tax_amount present(employee, gross_salary, tax_rate, added_amount, tax_amount, net_pay) def present(staff, gross_salary, tax_rate, added_amount, tax_amount, net_pay) -> None: print("============================================") print("\t- Idaho - State Income Tax -") print("============================================") print("Employee Information") print(f"Employee Name: {staff}") print("============================================") print("Tax Information") print("--------------------------------------------") print(f"Gross Salary: {gross_salary:7.2f}") print(f"Tax Rate: {tax_rate}%") print(f"Amount Added: {added_amount:7.2f}") print(f"Tax Amount: {tax_amount:7.2f}") print(f"Net Pay: {net_pay:7.2f}") evaluate_tax() print("===========================================")
Returning None
You can create a function to indicate that it produces no value. To do this, in the body of the function, type return None. Here is an example:
def conserve():
return None
thing = conserve()
print("Returned Value: ", thing)
print("=================================")
This would produce:
Returned Value: None ================================= Press any key to continue . . .
Consider the following code:
def accounce():
print("Where are we and what are we doing here")
print("We are in the wonderful world of Python programming.")
accounce()
print("=================================")
This would produce:
Where are we and what are we doing here We are in the wonderful world of Python programming. ==================================================== Press any key to continue . . .
When you write a return None statement in a function, you are asking the function to stop its processing from that point. Consider the following code:
def accounce():
print("Where are we and what are we doing here")
return None
print("We are in the wonderful world of Python programming.")
accounce()
print("====================================================")
This would produce:
Where are we and what are we doing here ==================================================== Press any key to continue . . .
You can use this feature of the None keyword to control the flow of code processing.
|
|||
Previous | Copyright © 2021-2024, FunctionX | Friday 31 December 2021 | Next |
|