Q. Write a program to have following functions :

(i) A function that takes a number as argument and calculates cube for it. The function does not return a value. If there is no value passed to the function in function call, the function should calculate cube of 2.

(ii)  A function that takes two char arguments and returns True if both the arguments are equal otherwise False.

Test both these functions by giving appropriate function call statements.


You can understand by Watching video :-



Answer :-

(i)

def cube( a = 2 ) :
    print( "Cube of ", a ,"="  , a ** 3 )

num = input("Enter a number (For empty press Enter ) :")

if num == "" :
    cube()
else :
    cube( int (num) )


Output

Enter a number (For empty press Enter ) :9
Cube of  9 = 729

>>>

Enter a number (For empty press Enter ) :
Cube of  2 = 8

>>>


(ii)

def chr(char1,char2) :
    if char1 == char2 :
        return "True"
    else:
        return"False "

char1 = input("Enter a Char 1 : ")
char2 = input("Enter a Char 2 : ")
print(chr(char1,char2))


Output :-

Enter a Char 1 : Path
Enter a Char 2 : Walla
False

>>> 

Enter a Char 1 : Path
Enter a Char 2 : Path
True

>>>



20 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. print("Ture")
    **(True)

    ReplyDelete
  2. If print("True") is written after if statement and if we remove print in last line then it wont give error right?

    ReplyDelete
  3. appreciate dedication for making video for each and every question thank you very much

    ReplyDelete
  4. def chr(char1,char2):
    return(char1,char2)
    char1 = input("Enter Char1 :")
    char2 = input("Enter Char2 :")
    if char1 == char2:
    print("True")
    else:
    print("False")

    ReplyDelete
    Replies
    1. def cube():
      n = input("enter number :")
      if n == "":
      print("Cube of 2 =",2**3)
      else:
      print("Cube of",int(n),"=",int(n)**3)
      cube()

      Delete
    2. Your answer of first question is correct but second one is wrong because you are returning char itself.

      Delete
  5. def abhi(a , b):
    if a == b :
    return 1
    else:
    return 2

    a = str(input("Enter 2nd Chr:- "))
    b= str(input("Enter 1st Chr:- "))
    c = abhi(a,b)
    if c ==1:
    print("True")
    else:
    print("False")

    ReplyDelete
  6. Are you a software engineer? What is your name ?

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post