Popular Posts

Saturday, August 15, 2020

Python Beginner: Working with Numbers

 Hello, developer!


Are you familiar with functions? A function is useful way to reuse codes and reduce the effort to develop the same codes. Don't worry even if you don't fully understand the concept of functions. You will learn more & more about it proceeding this course! :)


Let's learn several methods of conversion numbers. We will use the numbers type variables and math functions in the exercise program.


Internal Math functions

- abs(number): Convert minus number into plus number(i.e.: abs(

- str(number): Convert numbers type into strings type

- pow(number1, number2): number1 multiply number1 until number2 times

- max(number1, number2): Compare two numbers and show a bigger number

- min(number1, number2): Compare two numbers and show a smaller number

- round(number): Round down or up of a number(i.e.: round(3.2) > 3, round(3.7) > 4)


Python supports importing external modules so developers can easily use external functions. In this program, we will import the math module for using more math functions. Here is the math module importing command. 


from math import *


I showed the math module functions with purpose and examples.


External Math module functions

- floor(number): Return closet smaller integer value from input decimal(i.e.: floor(4.2) > 4)

- ceil(number): Return closet bigger integer value from input decimal(i.e.: ceil(4.2) > 5)

- sqrt(number): Return square value from input decimal(i.e.: sqrt(36) > 6.0)


I input the below codes and confirmed results like the following screenshot.

from math import *
my_num = 5
my_num2 = -5
print(2)
print(-2.0987)
print(3+4.5)
print(3*4+5)
print(3*(4+5))
print(10 % 3) # remainder
print(10 /3)
print(my_num)
print(str(my_num) + " my favorite number.")
print(abs(my_num2))
print(pow(4,6))
print(max(4,6))
print(min(4,6))
print(round(3.2))
print(round(3.7))
print(floor(3.7))
print(ceil(3.2))
print(sqrt(36))

 


Have you seen the same result on the console window? If yes, congratulations! Now you can import external modules for extended math functions and use math functions as well.


If you are interested in the math functions, please refer below link for your reference. 

https://docs.python.org/3/library/math.html?highlight=math%20function


I encourage you to refer to the Python Document References. You can search for functions that you want to use in your program and use searched functions in your codes. These efforts will lead you to understand Python coding and develop codes effectively.


Alright! See you next in the next post, developer! :)







  

No comments:

Post a Comment

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...