Question

Hi, Looking for some help with this Python question as follows: Consider the following Python program:...

Hi, Looking for some help with this Python question as follows:

Consider the following Python program:

def fun(x, y):

return x * y

a = fun(2, 3)

b = fun("2", 3)

print(a, b)

What does the function evaluate to? What would happen if we replace the last statement print a, b with print a + b?

Thanks for any help.

John

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def fun(x, y):

return x * y

a = fun(2, 3)

b = fun("2", 3)

print(a, b)

here a will become 2*3 i.e. 6

b will become "2"*3 i.e. repeat string 2 three times.Hence "222".

Hence function will multiply x by y if it is number .But if is string it just append x to itself y times.

What would happen if we replace the last statement print a, b with print a + b?

It will give error because we cant just add int and string

Output of above is

(6, '222')

Add a comment
Know the answer?
Add Answer to:
Hi, Looking for some help with this Python question as follows: Consider the following Python program:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following Python program: def fun(x, y): return x + y # [2] # [1]...

    Consider the following Python program: def fun(x, y): return x + y # [2] # [1] a = fun(2, 3) b = fun("2", 3) print a, b What does it evaluate to? Replace the last statement print a, b with print a + b and explain the traceback. What's wrong? Now eliminate the line marked [1] and change line [2] to read return x + y. Run the program and explain the traceback. Consider the following definition: def fun(n, m):...

  • Hello! I'm looking for help with this assignment. Complete a program in pseudocode and Python that...

    Hello! I'm looking for help with this assignment. Complete a program in pseudocode and Python that performs the following tasks. Open the file called M4Lab1ii.py linked below these instructions in your M4 Content module in IDLE. Save as M4Lab1ii.py. Replace ii with your initials. [example for someone with the initials cc: M4Lab1cc.py] Complete Steps 1-7 as requested within the code’s comments. Run your program and test all four calculations, then exit the program. Save your program as M4Lab1ii.py using your...

  • I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import...

    I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import arithmetic as a def multiply(num1, num2):     product = num1 * num2     result = a.add(product, product)     return result     def main():     num1 = 4     num2 = 3     answer = multiply(num1, num2)     print("The answer is", answer) if __name__ == "__main__":     main() arithmetic module: def add(x, y):     z = x + y     return z Refer to Code...

  • Hi, I need some help finishing the last part of this Python 1 code. The last...

    Hi, I need some help finishing the last part of this Python 1 code. The last few functions are incomplete. Thank you. The instructions were: The program has three functions in it. I’ve written all of break_into_list_of_words()--DO NOT CHANGE THIS ONE. All it does is break the very long poem into a list of individual words. Some of what it's doing will not make much sense to you until we get to the strings chapter, and that's fine--that's part of...

  • IN PYTHON Functions f(), g(), and h() are defined as follows: def f(x):     return 2*g(x)...

    IN PYTHON Functions f(), g(), and h() are defined as follows: def f(x):     return 2*g(x) def g(x):     return h(x)**2 def h(x):     return x//2 Consider the execution of function call f(2). Show the state of the program stack just prior to executing statement return x//2.

  • Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in...

    Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in PYTHON, new student. Please, please, I'm begging, Kindly answers all the questions. I'm hoping to grant my request. Thanks in advanced. 1.) What is the output of the following snippet? l1 = [1,2] for v in range(2): l1.insert(-1,l1[v]) print(l1)        a.) [1, 2, 2, 2]        b.) [1, 1, 1, 2]        c.) [1, 2, 1, 2]        d.) [1,...

  • Need some help doing and writing this project in python. The purpose of this assignment is...

    Need some help doing and writing this project in python. The purpose of this assignment is to assess your ability to Evaluate properties of modular arithmetic relations Utilize modular arithmetic to encrypt and decrypt plain text Research the Vigenere cipher. Write a program that contains an encode and a decode function. The encode function should take a plaintext message and a key. It should return the encoded message. The decode function should take the encoded message and the key and...

  • Hi! I need help with this for phython Temperature conversion. Complete the program by writing and...

    Hi! I need help with this for phython Temperature conversion. Complete the program by writing and calling a function that converts a temperature from Celsius into Fahrenheit. Use the formula F = C x 9/5 + 32. Test your program knowing that 50 Celsius is 122 Fahrenheit. def c_to_f(): # FIXME return # FIXME: Finish temp_c = float(input('Enter temperature in Celsius: ')) temp_f = None # FIXME: Call conversion function # temp_f = ?? # FIXME: Print result # print('Fahrenheit:'...

  • Please help on my homework Q i got wrong: PYTHON Question 1 def higherOrderFilter(data, p) :...

    Please help on my homework Q i got wrong: PYTHON Question 1 def higherOrderFilter(data, p) :     res = []     for d in data :         if p(d) :             res.append(d)     return res def FilterR(data, p):     if data == []:         return []     return [data[0]] + FilterR(data[1:], p) if p(data[0]) else FilterR(data[1:], p) Both of these functions produce the same output given the same data and same filtering condition p. True False    Question 2...

  • Please convert the following python program to LINE BY LINE python pseudocode. Please convert LINE BY...

    Please convert the following python program to LINE BY LINE python pseudocode. Please convert LINE BY LINE (typed) class vector: def __init__(self,x,y): self.x = x # Assigning x to vector's x self.y = y # Assigning y to vector's y def __str__(self): return '('+str(self.x)+","+str(self.y)+')' # This returns the value of vector in bracket format def __add__(self,other): x = self.x+other.x # values of x of vector1 and vector2 are added forming new x value y = self.y+other.y # same is done...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT