Deprecated: Required parameter $query follows optional parameter $post in /var/www/html/wp-content/plugins/elementor-extras/modules/breadcrumbs/widgets/breadcrumbs.php on line 1215
Tutorial on Python Math Module: Definition, Usecase & Examples
[PictoBloxExtension]

Python math Module

Python-Programming
Extension Description
Built-in module for mathematical functions for programming in Python.

Introduction

Python Math Module is a built-in module that contains a large number of mathematical functions. This module is useful for dealing with mathematics-related tasks such as statistics, complex numbers, etc. The math module will also provide useful mathematical constants such as pi.

Definition and Usecase

The math module defines a wide range of useful mathematical functions that can be used directly in any Python program. The use case for Math Module ranges from basic arithmetic operations (such as addition, subtraction, division, etc.), to more complex operations (such as trigonometric functions, complex numbers, etc.).Examples with Output

Examples with Output

Example 1

Below is an example of using the math module to calculate the absolute value of a number.

import math 

# initializing the number 
number = -2.45 

# calculating absolute value of number 
absolute_value = math.fabs(number) 

# printing the absolute value 
print("Absolute value of",number,"is",absolute_value)

Output:

Absolute value of -2.45 is 2.45

Example 2

Below is the example of using math module to calculate the natural logarithm of a number.

import math

# initializing the number
number = 50

# calculating natural log of number
natural_log = math.log(number)

# printing the natural log of number
print("The natural log of", number, "is", natural_log)

Output:

The natural log of 50 is 3.912023005428146

Read More

Python Functions

The function sends the specified data to the selected feed as a string.
Syntax: adaio.createdata(feedname = “feed name”, data = “0”)
The function makes the servo motors connected to the wheels orient towards the right. This orientation is used for making the robot turn right in a circle.
Syntax: setrightturnangle(Angle = 40)
The function sets the motors of a device to a specified speed. It uses a scale of -100 to 100, where -100 is the motor running in reverse at full speed, 100 is the motor running in the forward direction at full speed, and 0 is when the wheel is stopped.
Syntax: runallmotors(FL speed = 50, BL speed = 50, BR speed = 50, FR speed = 50)
The function sets the specified motor of the Quarky Expansion board to the specified direction (“FORWARD” or “BACKWARD”) and specified speed.
Syntax: runmotor(motor port = 1, direction = “FORWARD”, speed = 100)
The function is used to compute the arc tangent of the given angle in radians.
Syntax: math.atan(x)
This function takes four input parameters, the X, Y, and Z coordinates of the desired end position, and the amount of time to reach that position. It then divides the motion into smaller line segments, allowing the robotic arm to accurately and efficiently reach the specified location. The function then moves the robot arm from its current position to the specified end position within the given time frame.
Syntax: roboticArm.movexyzinline(XPOS = 0, YPOS = 150, ZPOS = 70, TIME = 1000)
Select the left and right IR sensors for line following, choosing between onboard Quarky IR sensors or external sensors connected to analog pins.
Syntax: setlinefollowerparameter(set_sensor = ‘left’, set_pin = ‘A1’)
This function is used to analyze the image received as input from the stage, for faces.
Syntax: analysestage()
The function sets its sprite’s X and Y position to the specified value. This block has no animation in its movement — it is the simplest way to move a sprite around the screen without displaying any animation (i.e. gliding).
Syntax: gotoxy(x = 100, y = 100)
The function changes the backdrop to the next in the list of backdrops, but if the current backdrop is the last in the list, the block will loop to the first.
Syntax: nextbackdrop()
The function clears any sound effects currently in place. However, it does not stop the sound currently being played.
Syntax: clearsoundeffects()
The function reports the mouse pointer’s current X position on the stage.
Syntax: mousex()
The function changes the specified value by the number input.
Syntax: changecolor(parameter = “color”, value = 10)
The function sets the RGB LED display matrix to display the animation specified: “happy”, “nerdy”, “thinking”, “angry”, “contempt”, “blink”, “fear”, “surprise”, “wink”, “wave”, and “crying”.
Syntax: showanimation(animation = “happy”)
The function initializes the ultrasonic sensor with specified echo and trig pins. 
Syntax: setultrasonicpins(sensor_number = 1, trig_pin = “D1”, echo_pin = “D2”)
The function sets the orientation of the robot to align the left and right movement of the robot in horizontal robot position and vertical robot position.
Syntax: setorientation(orientation = “HORIZONTAL”)
This function is used to analyze the image received as input from the specified URL of the image, for the feature.
Syntax: analyseURL(url = “https://ai.thestempedia.com/wp-content/uploads/2022/02/Robert-and-Chris.jpg”)
This function is used to analyze the image received as input from the image URL specified, for the handwritten and printed text.
Syntax: analyseURL(url = “https://ai.thestempedia.com/wp-content/uploads/2022/07/Hello-World.png”)
The function moves the servo motors of the pick and place robot to the place angle specified by the user.
Syntax: place()
The function sets the selected servo motor angle to the specified angle in the specified time. This creates a smooth motion for the servo motor from the current angle to the specified angle.
Syntax: movelimb(servo motor number = 1, angle = 90, time = 1000)
The function reports the state of the LDR (Light) sensor connected to the selected pin. The block returns 1 when the analog reading of the sensor is greater than the threshold value set by the user. Else it reports 0.
Syntax: ldrstatus(pin = “A1”)
The function returns the last data value of the specified feed as a string.
Syntax: adaio.getdata(feedname = “feed name”)
The function sets the servo motors of the Mars Rover to the specified angles.
Syntax: setwheelsangle(Front Left = 90, Front Right = 90, Back Left = 90, Back Right = 90)
The function sets the servo motors of the humanoid hip and leg to the specified angles at the specified speed.
Syntax: moveleg(time = 1000, servo angles = [90,90,90,90])
The function controls the speed of a selected motor, allowing it to run at a specified speed.
Syntax: runmotor(motor = “fl”, speed = 100)
The function stops the specified motor of the Quarky Expansion Board.
Syntax: stopmotor(motor port = 1)
The function takes two arguments – y-coordinate and x-coordinate – and produces an angle in radians from -(pi) to +(pi).
Syntax: math.atan2(y, x)
This function is used to move a robotic arm from its current position to a specific X, Y, and Z coordinate position over a specified period of time. It takes in multiple parameters, such as the current position of the arm, the coordinates to move to, and the interval in which to move. The function then determines how many steps must be taken in each axis to reach the destination point, and moves in the X axis first, then the Y axis, and lastly the Z axis before returning the successful result.
Syntax: roboticArm.movexyzonebyone(XPOS = 0, YPOS = 150, ZPOS = 70, TIME = 1000)
This function sets the IR threshold for either the default sensor or one connected to an analog pin.
Syntax: setirthreshold(set_sensor = ‘left’, set_value = 750)
This function displays the total number of faces detected in the camera feed or the stage.
Syntax: count()
All articles loaded
No more articles to load
Table of Contents