Question

In: Computer Science

What is the difference between local and global variable? Functions are also called ____________ In python,...

  1. What is the difference between local and global variable?
  2. Functions are also called ____________
  3. In python, to import functions from modules we use the keyword ‘import’, assume that you have a module named “mySelf” the mySelf module has “getMyName”, “getMyage” , and many more functions. 3 points.

Write a statement that imports

3.a. Only getMyName function

3.b all other functions from the module

  1. How do you differentiate a variable from a function?
  1. Write a function named times_ten. The function should accept an argument and display the product of its argument multiplied by a negative ten (-10) 2 points
  2. Write a python program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payments, insurance, gas, oil, tires, and maintenance. The program then should display the total monthly cost of their expenses and the total annual cost of these expenses. 7 points

Requirement: you will be writing two functions

  • Function_one will be used as a module
  • Function_two will be used as a driver(main) function that imports and uses attributes from function 1

Submission: Turn in this document with answers and two python files for question # 6

Solutions

Expert Solution

Please find the answers below.

1) What is the difference between local and global variable?

A global variable is a variable that can be accessible globally.

A local variable aredeclared within a fuction block. The scope of this variable is limited to the current function block only.

Please find the example below,

q = "I am a global variable" # global variable
def f():
p = "I am a local variable" # local variable
return p   
print(f())
print (q)

OUTPUT :

2) Functions are also called __method__________.

3) In python, to import functions from modules we use the keyword ‘import’, assume that you have a module named “mySelf” the mySelf module has “getMyName”, “getMyage” , and many more functions.

3.a. Only getMyName function.

The statement will be from mySelf import getMyName

3.b all other functions from the module.

The statement will be import mySelf

4. How do you differentiate a variable from a function?

In python we used variable to store a value. For example , in the above python code , q and p are two variables . Whereas, in order to define functions in python we can use the def keyword . A function can hold multiple variables and those variables are used to execute the function block. In the above example : f() is a function which is defined as def f().

5. Write a function named times_ten. The function should accept an argument and display the product of its argument multiplied by a negative ten (-10)

def times_ten(n):
#printing the product of the given number with -10
print("The product is {}".format(-10*n))

#Calling the function
times_ten(3)

OUTPUT :

6. Write a python program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payments, insurance, gas, oil, tires, and maintenance. The program then should display the total monthly cost of their expenses and the total annual cost of these expenses.

So, we will first create function1.py file and then define the function Function_one().

def Function_one():
loan_payments=0
insurance=0
gas=0
oil=0
tires=0
maintenance=0

Now we will create another .py file and we will define Function_two() there and then we will use all the attributes of Function_one() .

# importing the function1 from function1.py
from function1 import Function_one
# taking user inputs
def Function_two():
Function_one.loan_payments=float(input("Enter the loan amount : "))
Function_one.insurance=float(input("Enter the insurance amount : "))
Function_one.gas=float(input("Enter the gas amount : "))
Function_one.oil=float(input("Enter the oil amount : "))
Function_one.tires=float(input("Enter the tires amount : "))
Function_one.maintenance=float(input("Enter the maintenance amount : "))
monthly_expense=(Function_one.loan_payments+Function_one.insurance+Function_one.gas+Function_one.oil+Function_one.tires+Function_one.maintenance)
# Printing the monthly expense and annual expense
print("\nThe total monthly_expense : {}".format(monthly_expense))
print("The total annual_expense : {}".format(monthly_expense*12))
  
#under the main function we are calling Function_two
if __name__=="__main__":
Function_two()


OUTPUT:


Related Solutions

What is the difference between the inRange() and threshold() functions in OpenCV in Python? Thorough answer...
What is the difference between the inRange() and threshold() functions in OpenCV in Python? Thorough answer please!
What is the difference between what is called the matrix and what is called the scaffold?...
What is the difference between what is called the matrix and what is called the scaffold? What is the nuclear matrix/scaffold and what does it consist of? What is a MARs and what are its general characteristics? What are some nuclear activities that are believed to be associated with the nuclear matrix?
What is the main difference between numeric variable and categorical variable; the main difference between ordinal...
What is the main difference between numeric variable and categorical variable; the main difference between ordinal variable and nominal variable; the main difference between ratio variable and interval variable?
h) What is the difference between fixed costs and variable costs? What is the difference between...
h) What is the difference between fixed costs and variable costs? What is the difference between accounting costs and economic costs? What is the differ - ence between private costs and external costs? Name all the categories that comprise economic costs. i) How do we determine the profit-maximizing level of production using analy - sis of total costs and total revenues? What is the rule for profit maximization using marginal analysis? What happens to economic profits in a perfectly competitive...
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
What is the difference between a discrete random variable and a continuous random variable?
What is the difference between a discrete random variable and a continuous random variable?
What is the difference between a fixed and a variable cost? What is meant by the...
What is the difference between a fixed and a variable cost? What is meant by the term relevant range? How is relevant range applicable to CVP analysis? What is operating leverage and how/why would companies use operating leverage?
Why are global interest rates so low? Also describe the difference between real and nominal interest...
Why are global interest rates so low? Also describe the difference between real and nominal interest rates. Briefly describe the difference between real world and risk-neutral pricing.
What is the difference between general anesthesia, local anesthesia, and sedation?
What is the difference between general anesthesia, local anesthesia, and sedation?
How to transfer the below two global varibale into local variable where this two variable are...
How to transfer the below two global varibale into local variable where this two variable are used. PLEASE HELP // Read in the user's height int customerAge = 0; double customerHeight = 0; // ******************************************* // Functions // ******************************************* void readHeight(); void printCustomerInfo(); bool askToDoAnother(); int main() { bool keepReading = 1; while (keepReading) { // Read in the customer info: // ... Read the height readHeight(); // ... Read Age cout << "Enter their age: "; cin >> customerAge;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT