To find the largest among three numbers in Python, you can use the following code:
# define a function to find the largest number def find_largest(num1, num2, num3): # check if num1 is greater than num2 and num3 if num1 > num2 and num1 > num3: return num1 # check if num2 is greater than num1 and num3 elif num2 > num1 and num2 > num3: return num2 # if the above conditions are not met, return num3 else: return num3 # take input from the user num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) num3 = int(input("Enter the third number: ")) # call the function and print the result largest = find_largest(num1, num2, num3) print("The largest number is", largest)
This code first defines a function
find_largest() that takes three numbers as arguments and returns the largest number among them. The function checks if each number is greater than the other two numbers, and returns the number if it is. If none of the numbers is greater than the other two, it returns the last number.
After defining the function, the code takes three numbers as input from the user using the
input() function and stores them in variables
num3. It then calls the
find_largest() function and passes these three numbers as arguments. The returned value (the largest number) is stored in the variable
largest and is printed using the
Thanks for reading. Happy coding!