2019년 9월 6일 금요일

Python[3 day]: Basic operators for mathematical calculations and mathematical functions

   Maybe, there is no need to explain the operators of + (addition), - (subtration), * (multiplication), / (division). In my tests, it was confirmed that the calculation of division returns the real number value even though you calculate only with integer numbers.


   In Python, modulus operator of % returns the remainder after the division calculation. If you type 100%3, Python interpreter divides 100 by 3 and returns the remainder of 1. Floor division operator of // returns the quotient after the division calculation. If you type 100//3, Python interpreter divides 100 by 3 and returns the quotient of 33.0. In other word, in the floor division, Python interpreter throws away the remainder after the division calculation.

    
   The operator of ** is used for the exponential (power) calculation in Python.


   Python provides many mathematical functions the same as other programming language. However, some mathematical functions available in Python are supported by the module called math, therefore, to use these functions, you should import math module within your program before they are called in your program.
   The method to import math module is very simple. All you have to do is that you type the command of import math before the function supported by math module is used in your program. The below table shows the mathematical functions required math module.


   However, if you use any function requiring math module, you should be careful with the notation of the function. For example, when you use sqrt function, its name is not sqrt, but math.sqrt. That is, module name(math)+period(.)+function name(sqrt). The way of this notation should be also kept whenever you import any other module provided in Python.


   From the above table, it is seen that the exponential calculation can be done not only by the operator of **, but also by the function of pow(x,y). The below is the calculation example to calculation of square root.


   Trigonometric functions such as sin, cos, tan etc. are also the important mathematical functions in science and engineering calculations.
   As the same as the previously explained mathematical functions, these trigonometric functions are also required math module for using them in a Python program. The below table shows the trigonometric functions supported by Python math module.


   When using the basic trigonometric functions (sin, cos, tan) in a Python program,  the angle for the trigonometric functions should be radian (not degree), which is the same with all other programming languages.
   Python is a very kind (user friendly) programming language because the last two functions are supported to convert radian to degree and vice versa. However, in my personal opinion, it is questionable whether the last two functions for converting angles are really necessary.
   Anyway, to use the trigonometric functions, never forget to input import math first, and then write math.sin( ). It is noticeable that Python provides the function of hypot(x,y), which is not seen in other programming languages. hypot(x,y) returns the hypotenuse length of a right angle triangle from Pythagorean theorem.



Python[2 day]: input( ), int( ), float( ) functions

   Sometimes, you would like to input any value into Python interpreter while a Python program is running. For this, Python provides you with input function.
   The syntax of input function is very simple and just type the command of x=input( ). In ( ), you can insert your favorite message, however your message should be enclosed by the quotation marks in ( ). Then, Python interpreter will display your message on a monitor and wait your input.


   Since the integer of 5 is stored in the variable of x by the command of x=input( ), you can try to add other integer to the variable of x.


   However, you will see the error message as seen in the above figure, which is resulted from the fact that the data of 5 stored in the variable of x is not integer.
   Since Python interpreter recognizes the data stored by the input function as string, it is not possible to apply a mathematical operation to the stored data. In the above example, your input data of 5 is not integer, but string. Whether or not the data stored in a variable can be checked by the type( ) function in Python. If you use type( ) function, Python inform you of the class of data. Class is the classification of data type.


   In Python, any real number can be converted to the corresponding integer number by using the function of int( ), and vise versa by using the function of float( ).


   Then, if Python interpreter receives the data from input( ) function and converts it to an integer or a real number, it may be possible that any mathematical operation can be applied to the variable having the converted numeric data.

Before testing this example, check your Python version first. Under the version of 3.7, this trial would show an error message. 


   Python version used in this post is 3.7.4. The contents of this post will not applied to the earlier version of Python.

2019년 9월 5일 목요일

Python[1 day]: First Python programming [print( ) function and variable]

   This post assumes that Python over Ver. 3.0 is already installed on your computer, otherwise you can visit https://www.python.org/ for Python installation.
   The most important thing for beginners to learn programming is to find it interesting. Maybe, a good way to do this is to learn programming by checking the output, just before he input some commands into a computer, displayed on a monitor.
   This post gives a brief overview of print function that makes the commands entered by the user appear on the monitor for whom wants to learn Python.

   print ( ) function displays your commands inserted in ( ) on a monitor.


   The mark of >>> on your monitor is the Python prompt, which will wait and do nothing before you input any other command. When you input any command, you never insert blank just after the prompt of >>>.



   If you want to display your words or a sentence on a monitor, you should use the quotation marks of “~~” or ‘~~’. If you don’t use the quotation marks with print function for displaying any word or sentence, you will see certainly an error message.


   If you want any numeric value, character, word or sentence into any place of memories of a computer, you should assign the name of any specific memory space of a computer for your input data. We call the name of the specified memory space a variable. However, we don’t need to care how the memory space is assigned and where the memory space is for the variable. Just keeping in mind is that the variable is the name of a memory space for assigned for any input data.



   You can also store your words or sentence into the variable named by you. However, you should use the quotation marks when you assign a string to a variable. Now, we look into the process that the data is assigned to a variable.
   If you type x=5, Python interpreter will secure any memory space and named x for that memory. After being secured a memory, the value of 5 is recorded into that memory. Therefore, the equality mark of = is quite different of mathematics as seen the following example.