round() function in Python | Python Tutorials for Beginners #lec21

round() function in Python | Python Tutorials for Beginners #lec21

Understanding the Round Function in Python

Introduction to BMI Calculation

  • The video begins with a recap of a previous exercise on calculating Body Mass Index (BMI), emphasizing that weight should be in kilograms and height in meters, both as float values.
  • The desired output for BMI is specified as a whole integer rather than a fractional number. For example, inputting 48 kg and 1.5 m should yield an integer result like 21 instead of 21.33333.

Rounding Numbers

  • Instead of converting to an integer directly, rounding off the number is suggested. For instance, rounding 21.67 results in 22 due to standard mathematical rules.
  • The round function in Python will be discussed, including its syntax and various applications, particularly how it handles special cases such as tie-breaking scenarios.

Syntax and Parameters of the Round Function

  • The general syntax for the round function includes two parameters: the number to round off and an optional second parameter indicating the number of decimal places.
  • The first argument (the number) is mandatory while the second argument (number of digits) is optional; if omitted, it defaults to zero which rounds to the nearest integer.

Examples of Using the Round Function

  • Several examples illustrate how the round function operates:
  • Rounding an integer like 7 returns 7 without change.
  • Rounding a float like 2.66 with no specified digits yields 2.67.
  • If n digits are provided but are set to zero, it still returns a float value (e.g., rounding gives 3.0).

Special Cases in Rounding

  • Special cases include tie-breaking situations where numbers ending in .5 can either round up or down based on whether they are even or odd:
  • For example, rounding 7.5 could return either 7 or 8; however, Python's implementation will return the nearest even integer (8).
  • Similarly, rounding numbers like 6.5 also presents two potential outcomes but follows Python's rule by returning an even result when applicable.

This structured overview provides insights into using Python's round function effectively while highlighting key concepts related to numerical operations within programming contexts.

Understanding Rounding in Python

Nearest Even Integer Rounding

  • The nearest even integer rounding method is discussed, where 11.5 rounds to 12 and 12.5 rounds to 12 as well, following the tie-breaking rule.
  • It is emphasized that integers are used for rounding examples, and providing a string will result in an error since only integers or floats are acceptable inputs.

Impact of Digits on Rounding

  • Positive digit values do not affect the number; for example, rounding with a positive value of 2 returns the number unchanged.
  • When using negative digits (e.g., -1), it affects the rounding by determining the closest multiple of ten based on the specified digit count.

Examples of Negative Digit Rounding

  • For -1 digits, it finds the closest multiple of ten (670 in this case), while for -2 digits, it rounds to the nearest hundred (700).
  • If -2 is applied, only one digit remains after rounding off leading zeros; thus, if it's less than five, it retains its value.

Further Clarifications on Rounding Rules

  • A further example illustrates that when rounding with -2 digits results in a six being rounded up to seven due to its proximity to five.
  • If three digits are specified as negative (-3), it leads to zeroes being added until reaching a thousand.

Assignments and Practical Applications

  • The assignment encourages viewers to determine outputs from given cases involving various numbers and their respective digit specifications.
  • An example shows how rounding works with numbers like 66 and how tie-breaking rules apply when dealing with .5 scenarios.

Additional Examples and Assignments

  • The discussion includes negative numbers such as -8/3 which rounds down correctly according to established rules.
  • Viewers are tasked with predicting outcomes for specific decimal values under different conditions related to positive and negative digit counts.

Conclusion: Practical Application of Concepts

  • The session concludes by inviting viewers to explore practical applications through coding exercises related to these concepts.

Understanding the Round Function in Python

Overview of Custom Round Function

  • The speaker introduces a custom round function named round_underscore_function to avoid conflicts with Python's built-in round() function.
  • When rounding an integer like 7, the output remains 7, and its type is confirmed as int.

Rounding Behavior with Decimals

  • If a decimal number such as 7.66 is rounded to two decimal places, it results in 7.67, changing the type to float.
  • In tie-breaking scenarios (e.g., rounding 7.5), the function returns 8 because it rounds to the nearest even integer.

Handling Edge Cases

  • For numbers like 6.5, the function returns 6 instead of 7 due to its tie-breaking rule favoring even integers.
  • When rounding larger numbers or negative values (e.g., -2), specific rules apply based on significant digits.

Rounding Negative Numbers

  • The speaker explains that negative numbers can also be rounded; for instance, -8/3 yields approximately -3.
  • Special cases are discussed where inputs like -1.5 round to -2 and -2.5 round down to -2 due to the nearest even integer rule.

Practical Applications and Conclusion

  • The speaker encourages experimentation with various examples of the round function and invites questions for clarification on its usage across different scenarios including positive, zero, and negative values.
Video description

In this lecture I have discussed everything about round() function like What is round() function, How to use round() function with examples. 🔥Notes of Today's class: https://drive.google.com/file/d/1337QXpVJ9oJ6yBGK32rdn6ecTpT9dEvV/view?usp=sharing 🔥Best Python Tutorials for Beginners: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bZSiqiOL5ta39vSnBxpOPT ********************************************* Connect & Contact Me: Jenny's Lecturers Hindi: https://www.youtube.com/@JennysLecturesHINDI My Second Channel Link: https://bit.ly/354n7C7 Facebook: https://www.facebook.com/Jennys-Lectures-CSIT-Netjrf-316814368950701/ Quora: https://www.quora.com/profile/Jayanti-Khatri-Lamba Instagram: https://www.instagram.com/jayantikhatrilamba/ Twitter: https://twitter.com/KhatriJenny ******************************************* More Playlists: Programming in C Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31a8UcMN9-35ghv8qyFWD9_S C++ Tutorials for beginners: https://www.youtube.com/watch?v=KYxLEDF6kjs&list=PLdo5W4Nhv31YU5Wx1dopka58teWP9aCee Placement Series: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YvlDpJhvOYbM9Ap8UypgEy Data Structures and Algorithms: https: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU Design and Analysis of Algorithms(DAA): https://www.youtube.com/playlist?list=PLdo5W4Nhv31ZTn2P9vF02bkb3SC8uiUUn Dynamic Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31aBrJE1WS4MR9LRfbmZrAQu Operating Systems tutorials: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa DBMS Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31b33kF46f9aFjoJPOkdlsRc #coding #pythonforbeginners #python #jennyslectures #pythonprogramming