Learning doesn’t stop, at any age!
Learning a little each day adds up!
We learned all about strings in our previous lessons, including what a string is, how it works and related functions. Today, I’ll give you an update on what I’ve recently learned about another sort of data, numbers. Let’s get started!
First, if we would need use the math function in Python, we will need import math module. Using below code,
import math
print (math.sqrt(4)) #test if the import works
Note the values after # is comment. If you recall from the prior session. A Python comment is a line of text that appears in a program but is not executed by the program. You can declare a comment using a hashtag (#). Comments can appear on a new line or at the end of an existing line of code.
Depends on the version of your Python, if the above code deosn’t work for you. Try below code to import math module.
from math import*
print (math.sqrt(4)) #test if the import works
Now let’s do some math in Python. Try below code:
print (10+10+10)
You should get the result of: 30.
Python’s math order is based on PEMDAS. so try below code:
print (10+10*3)
The answer is 40! 10*3 goes first = 30 then plus 10 , we got 40. Perfect! Let’s try a complicated one,
print(((10+10)*2+4)/2+3)
You should get 25. If not, leave me message, I will show you how we get it.
Now let’s try another way. How can I print strings and numbers in one print. How can I connect them? Here you go.
age =25
print(“My age is: ” + age)
What answer did you get?? Yet, error. TypeError: can only concatenate str (not “int”) to str
This is becuase the “My age is :” is a string, you can only concate string together. The age variable we defined is number (int format, means integer). We will need convert the age variable into string. We can do this will str() function. Here you go! Try below code.
age=25
print(“My age is: ” + str(age))
You should get output: My age is: 25
Okay, now let’s use functions. Remember we have imported the math functions at beginning? Let’s use it.
print(max(1,2,3,45,90,56,-2,0,34,56)) #find the largest number, you should get 90
print(min(1,2,3,45,90,56,-2,0,34,56)) #find the smallerst number,you should get -2
print(math.pow(2,4)) #find the result of 2 to the power of 4, you should get 16
print(math.sqrt(16)) #find the square root of 16.you should get 4
print(52/36+sqrt(2)) # you should get 2.8586580068175396
print(round(52/36+sqrt(2))) # You should get 3
print(abs(-3)) #get the absolute value
Some of you might be confused about the Round function. Here is an exlaination.
Round () is a built-in function available with python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.
That’s try some advanced functions.
print(math.ceil(22.333)) #return the smallest integer value,greater than or equal to the specified number, 23
print(math.floor(22.333)) # rounds a number down to the nearest integer. 22
print(math.factorial(5)) # returns factorial of the number. here is 5! which is 54321=120
There are many more of math functions. If you are interested, google will help.
Thank you for joining me today! I’ll show you more when I learn something new!
Happy Learning!!
Pingback: Learn all the basics of python – Python beginners, let’s learn together (6) – XRKnows