What will be the output of the following python code - codebugfree

 In this section we see the which will be the output of following code. It is QnA section. First we see the output of following code. Than see the code of python. From basic to intermediate.

What will be the output of the following python code

What will see the output of following python code

We see the output of following python code are listed below.

1) Simple addition program
2) Sort the number
3) Reverse the number
4) Error handling


Simple addition program

OUTPUT OF CODE

Enter your first number: 68908
Enter your second number: 57978
first number 68908 + second number 57978 = 126886
class 'str'

Here we first get input from user. First get two number get input from user. Using the help of f string inset the variable in string. print the addition of user input value. Again print the type of output.

CODE

user_firstnumber = int(input("Enter your first number: "))#getting input
user_second_number = int(input("Enter your second number: "))#getting input
#use f string insert variable in string
now_addition = f"first number {user_firstnumber} + second number {user_second_number} = {user_firstnumber+user_second_number}"
print(now_addition)
print(type(now_addition))

Sort the number

We get the value [0, 8, 34, 34, 45, 45, 45, 56, 56, 67, 67, 87, 89, 98, 345, 5690, 235234]
class 'list'

Take a specific number and use the f string. Print the sorted value and it types.

here_the_sorting_num = [45,87,98,34,67,8,45,89,0,34,5690,45,345,67,235234,56,56]
here_the_sorting_num.sort()
print(f"We get the value {here_the_sorting_num}")
print(type(here_the_sorting_num))

Reverse the number

[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
class 'list'

Her we have three types to reverse the ant list Built in function, slicing, using for loop for reverse. Here we use the string slicing technique to perform this task. first we make the list with 1 to 20. than reverse and print it types.


reversing_the_number = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]#list number
#reversing the number with string slicing
print(reversing_the_number[::-1])
print(type(reversing_the_number))

See the error handling

Enter your age: codebugfree
you input a alphbet not a number

Her we get user age from user input. length is detect as if else condition minimum and maximum length of age is 2. If age is equal length of 2 than print age or not eqal than print else. If use input the alphbet than program handle the error.

try:
    user_age = int(input("Enter your age: "))
    if user_age ==2:
        print(f"Your age is {user_age}")
    else:
        print("you are to small or you doesnot put your age carefully") 
except Exception as e:
    e = "you input a alphbet not a number"
    print(e)

Post a Comment

0 Comments