Python Programming Math Tutorial with Stone River eLearning

When we think of user friendly programming languages, the first programming language that comes to the mind of most of the enthusiastic programmers is Python. Created during 1985-1990, Python depicts how deeply and efficiently the Object Oriented Paradigm has crawled into the programming languages.  Python’s USP is its highly interactive and easy to learn nature along with the portability. Python is considered the perfect language for a beginner in the world of coding the programming. Python can be easily integrated with C, C++ or JAVA and it provides high-level data type and supports dynamic data type checking.

The use of English words for performing operations and the fewer syntactical construction as compared to other languages has made Python an instant hit among the programmer’s fraternity. We will be discussing various functions and operations that are useful in the language during these blog post series on Python programming and what better way to start than describing through mathematical functions.

Remember the good old days of C programming when writing a simple function to add two numbers would take around 10-15 line of code? Now add all the mathematical operations to it, multiplication, division, subtraction, all in one. That’s not it; now imagine if the numbers were all in different formats, some float, some normal integers, some double. Can you think what length of code will it take to derive a correct mathematical result with these constraints? Well, actually, wait, save the answer, what if I tell you that you can do each of these operations in just one single line and yes you can do it using the Python language.

Can we perform the mathematical calculations in the print statement? No? Well, now we can. As we mentioned in the starting of this blog post, the power of Python to simplify various operations is amusing and highly efficient. Building on that, the print statement is not just a way to display content; even it has evolved to something better. Let us have a look at an example to understand better.

Eg:

print (1+3)

print(1-3)

print (1/3)

print(1*3)

The output of the above program in Python is:

4

-2

0.333333333

3

All we need to do it put the numbers inside the print statement and Python performs the calculations on its own. Further, if the numbers are floating point numbers, or if one of the numbers is a floating point number, we do not have to convert an integer value into float by putting in a 0 after the decimal.

Now before we conclude this post about mathematical operations in Python, we are going to discuss performing the square and cube operation. We do not need the math.h header nor do we have to import the math class, all we have to do is write a simple print command and the square or cube of a number is generated.

Eg:

print(4**2)

print(4**3)

Note:

The first statement squares the integer 4 while the second statement cubes the integer. However; it is important to note that the following statement is equivalent to writing 4*4 or 4*4*4 as the number after the “*” symbol in the expression give the power of the number before it.

 

GET YOUR FREE PYTHON EBOOK!

(Visited 133 times, 1 visits today)