Software Development Exam  >  Software Development Notes  >  Basics of Python  >  Code: Find Largest of 3 Numbers

Code: Find Largest of 3 Numbers | Basics of Python - Software Development PDF Download

Introduction

When working with numbers in Python, it is often necessary to determine the largest value among a given set. In this article, we will explore various approaches to finding the largest of three numbers in Python. We will provide multiple examples and explain the code logic behind each approach. By the end, you will have a clear understanding of how to solve this common programming problem.

Comparing Numbers Using Conditional Statements

One straightforward way to find the largest of three numbers is by using conditional statements. Let's take a look at the following code example:

def find_largest(a, b, c):

    if a >= b and a >= c:

        return a

    elif b >= a and b >= c:

        return b

    else:

        return c

# Test the function

num1 = 10

num2 = 5

num3 = 7

largest_num = find_largest(num1, num2, num3)

print("The largest number is:", largest_num)

Output:

The largest number is: 10

Explanation:

  • We define a function called 'find_largest' that takes three arguments: 'a', 'b', and 'c'.
  • Inside the function, we use conditional statements ('if', 'elif', and 'else') to compare the values of 'a', 'b', and 'c'.
  • The first 'if' statement checks if 'a' is greater than or equal to both 'b' and 'c'. If true, it means a is the largest number, so we return 'a'.
  • The second 'elif' statement checks if 'b' is greater than or equal to both 'a' and 'c'. If true, it means 'b' is the largest number, so we return 'b'.
  • If neither of the above conditions is true, it means 'c' is the largest number, so we return 'c'.

Using the max() Function

Python provides a built-in function called 'max()' that can be used to find the largest number among a sequence of values. Let's see how it works in the following example:

def find_largest(a, b, c):

    return max(a, b, c)

# Test the function

num1 = 10

num2 = 5

num3 = 7

largest_num = find_largest(num1, num2, num3)

print("The largest number is:", largest_num)

Output:

The largest number is: 10

Explanation:

  • We define a function called 'find_largest' that takes three arguments: 'a', 'b', and 'c'.
  • Inside the function, we use the 'max()' function, which takes multiple arguments and returns the largest value among them.
  • We pass 'a', 'b', and 'c' as arguments to the 'max()' function, which automatically finds the maximum value among them and returns it.

Sample Problems and Solutions

Now, let's solve a couple of sample problems using the approaches we discussed.

Problem 1: Find the largest number among 20, 35, and 15.

Using conditional statements:

def find_largest(a, b, c):

    if a >= b and a >= c:

        return a

    elif b >= a and b >= c:

        return b

    else:

        return c

num1 = 20

num2 = 35

num3 = 15

largest_num = find_largest(num1, num2, num3)

print("The largest number is:", largest_num)

Output:

The largest number is: 35

Using the max() function:

def find_largest(a, b, c):

    return max(a, b, c)

num1 = 20

num2 = 35

num3 = 15

largest_num = find_largest(num1, num2, num3)

print("The largest number is:", largest_num)

Output:

The largest number is: 35

Problem 2: Find the largest number among -5, -10, and -2.

Using conditional statements:

def find_largest(a, b, c):

    if a >= b and a >= c:

        return a

    elif b >= a and b >= c:

        return b

    else:

        return c

num1 = -5

num2 = -10

num3 = -2

largest_num = find_largest(num1, num2, num3)

print("The largest number is:", largest_num)

Output:

The largest number is: -2

Using the max() function:

def find_largest(a, b, c):

    return max(a, b, c)

num1 = -5

num2 = -10

num3 = -2

largest_num = find_largest(num1, num2, num3)

print("The largest number is:", largest_num)

Output:

The largest number is: -2

Conclusion

In this article, we explored two approaches to find the largest of three numbers in Python. We used conditional statements and the 'max()' function to accomplish this task. Both methods are effective, and the choice between them depends on personal preference and specific requirements. By understanding these concepts and practicing with sample problems, you can confidently find the largest number among any given set.

The document Code: Find Largest of 3 Numbers | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests
49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

pdf

,

practice quizzes

,

MCQs

,

Previous Year Questions with Solutions

,

video lectures

,

Objective type Questions

,

ppt

,

Code: Find Largest of 3 Numbers | Basics of Python - Software Development

,

Code: Find Largest of 3 Numbers | Basics of Python - Software Development

,

Viva Questions

,

shortcuts and tricks

,

mock tests for examination

,

Summary

,

past year papers

,

Important questions

,

Exam

,

Sample Paper

,

Extra Questions

,

Semester Notes

,

Code: Find Largest of 3 Numbers | Basics of Python - Software Development

,

Free

,

study material

;