- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Trigonometric Functions
acos() function
The acos() function returns the arc cosine of x in radians.
Syntax
Following is the syntax for acos() function −
acos(x)
Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object.
Parameters
x − This must be a numeric value in the range -1 to 1. If x is greater than 1, then it will generate 'math domain error'.
Return Value
This method returns arc cosine of x, in radians. The result is between 0 and pi.
Example
The following example shows the usage of the acos() method −
from math import acos x = 0.5 val = acos(x) print ("x: ",x, "acos(x): ", val) x = 0.0 val = acos(x) print ("x: ",x, "acos(x): ", val) x = -1 val = acos(x) print ("x: ",x, "acos(x): ", val) x = 1 val = acos(x) print ("x: ",x, "acos(x): ", val)
When we run the above program, it produces the following output −
x: 0.5 acos(x): 1.0471975511965979 x: 0.0 acos(x): 1.5707963267948966 x: -1 acos(x): 3.141592653589793 x: 1 acos(x): 0.0
asin() Function
The asin() function returns the arc sine of x (in radians).
Syntax
Following is the syntax for the asin() function −
asin(x)
Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object.
Parameters
x − This must be a numeric value in the range -1 to 1. If x is greater than 1, then it will generate 'math domain error'.
Return Value
This method returns arc sine of x, in radians. The result is between -pi/2 and pi/2.
Example
The following example shows the usage of the asin() method.
from math import asin x = 0.5 val = asin(x) print ("x: ",x, "asin(x): ", val) x = 0.0 val = asin(x) print ("x: ",x, "asin(x): ", val) x = -1 val = asin(x) print ("x: ",x, "asin(x): ", val) x = 1 val = asin(x) print ("x: ",x, "asin(x): ", val)
When we run the above program, it produces the following output −
x: 0.5 asin(x): 0.5235987755982989 x: 0.0 asin(x): 0.0 x: -1 asin(x): -1.5707963267948966 x: 1 asin(x): 1.5707963267948966
atan() Function
The atan() function returns the arc tangent of x, in radians.
Syntax
Following is the syntax for atan() function −
atan(x)
Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object.
Parameters
x − This must be a numeric value.
Return Value
This function returns arc tangent of x, in radians. The result is between -pi/2 and pi/2.
Example
The following example shows the usage of the atan() method −
from math import atan x = 0.5 val = atan(x) print ("x: ",x, "atan(x): ", val) x = 0.0 val = atan(x) print ("x: ",x, "atan(x): ", val) x = -1 val = atan(x) print ("x: ",x, "atan(x): ", val) x = 1 val = atan(x) print ("x: ",x, "atan(x): ", val)
When we run the above program, it produces the following output −
x: 0.5 atan(x): 0.4636476090008061 x: 0.0 atan(x): 0.0 x: -1 atan(x): -0.7853981633974483 x: 1 atan(x): 0.7853981633974483
atan2() Function
The atan2() function returns atan(y / x), in radians. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.
Syntax
Following is the syntax for atan2() function −
atan2(y, x)
Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object.
Parameters
y − This must be a numeric value in radians.
x − This must be a numeric in radians.
Return Value
This function returns atan(y / x), in radians. The result is between -pi and pi.
Example
The following example shows the usage of atan2() method −
from math import atan2 x,y = (-0.50,-0.50) val = atan2(x,y) print ("x: ",x, "y:",y, "atan2(x,y): ", val) x,y = (0.50,0.50) val = atan2(x,y) print ("x: ",x, "y:",y, "atan2(x,y): ", val) x,y= (5,5) val = atan2(x,y) print ("x: ",x, "y:",y, "atan2(x,y): ", val) x,y = (-10,10) val = atan2(x,y) print ("x: ",x, "y:", y, "atan2(x,y): ", val) x,y = (10,20) val = atan2(x,y) print ("x: ",x, "y:", y, "atan2(x,y): ", val)
When we run the above program, it produces the following output −
x: -0.5 y: -0.5 atan2(x,y): -2.356194490192345 x: 0.5 y: 0.5 atan2(x,y): 0.7853981633974483 x: 5 y: 5 atan2(x,y): 0.7853981633974483 x: -10 y: 10 atan2(x,y): -0.7853981633974483 x: 10 y: 20 atan2(x,y): 0.4636476090008061
cos() function
The cos() function returns the cosine of x radians.
Syntax
Following is the syntax for cos() function −
cos(x)
Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object.
Parameters
x − This must be a numeric value in radians.
Return Value
This function returns a numeric value between -1 and 1, which represents the cosine of the angle.
Example
The following example shows the usage of cos() method −
from math import cos, pi x = 3 val = cos(x) print ("x: ",x, "cos(x): ", val) x = -3 val = cos(x) print ("x: ",x, "cos(x): ", val) x = 0 val = cos(x) print ("x: ",x, "cos(x): ", val) x = pi val = cos(x) print ("x: ",x, "cos(x): ", val) x = 2*pi val = cos(x) print ("x: ",x, "cos(x): ", val)
When we run the above program, it produces the following output −
x: 3 cos(x): -0.9899924966004454 x: -3 cos(x): -0.9899924966004454 x: 0 cos(x): 1.0 x: 3.141592653589793 cos(x): -1.0 x: 6.283185307179586 cos(x): 1.0
dist() Function
This function returns the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates. The two points must have the same dimension. The Euclidean distance between two points in the plane with coordinates (x, y) and (a, b) is given by $\mathrm{dist \: ((x,y),(a,b)) \: = \: \sqrt{(x − a)^2 + (y − b)^2}}$
Syntax
math.dist(p, q)
Parameters
p and q − iterables with two numeric operands.
Return value
This function returns the Euclidean distance between two points.
Example
from math import dist p = [3,5] q = [6,9] val = dist(p,q) print ("p: ",p, "q:", q, "dist(p,q): ", val) p = [0,0] q = [3,3] val = dist(p,q) print ("p: ",p, "q:", q, "dist(p,q): ", val)
It will produce the following output −
p: [3, 5] q: [6, 9] dist(p,q): 5.0 p: [0, 0] q: [3, 3] dist(p,q): 4.242640687119285
hypot() Function
The function hypot() return the Euclidean norm, sqrt(x*x + y*y). This is length of vector from origin to point (x,y)
Syntax
Following is the syntax for hypot() function −
hypot(x, y)
Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.
Parameters
x − This must be a numeric value.
y − This must be a numeric value.
Return Value
This function returns Euclidean norm, sqrt(x*x + y*y).
Example
The following example shows the usage of hypot() function −
from math import hypot x =3 y =2 val = hypot(x,y) print ("x: ",x, "y:", y, "hypot(x,y): ", val) x = -3 y = 3 val = hypot(x,y) print ("x: ",x, "y:", y, "hypot(x,y): ", val) x =0 y =2 val = hypot(x,y) print ("x: ",x, "y:", y, "hypot(x,y): ", val)
When we run the above program, it produces the following output −
x: 3 y: 2 hypot(x,y): 3.605551275463989 x: -3 y: 3 hypot(x,y): 4.242640687119285 x: 0 y: 2 hypot(x,y): 2.0
sin() Function
The sin() function returns the sine of x, in radians.
Syntax
Following is the syntax for sin() function −
math.sin(x)
Note − This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object.
Parameters
x − This must be a numeric value.
Return Value
This function returns a numeric value between −1 and 1, which represents the sine of the parameter x.
Example
The following example shows the usage of sin() method −
from math import sin, pi x = 3 val = sin(x) print ("x: ",x, "sin(x): ", val) x = −3 val = sin(x) print ("x: ",x, "sin(x): ", val) x = 0 val = sin(x) print ("x: ",x, "sin(x): ", val) x = pi val = sin(x) print ("x: ",x, "sin(x): ", val) x = pi/2 val = sin(x) print ("x: ",x, "sin(x): ", val)
When we run the above program, it produces the following output −
x: 3 sin(x): 0.1411200080598672 x: -3 sin(x): -0.1411200080598672 x: 0 sin(x): 0.0 x: 3.141592653589793 sin(x): 1.2246467991473532e-16 x: 1.5707963267948966 sin(x): 1.0
tan() Function
The tan() function returns the tangent of x radians.
Syntax
Following is the syntax for tan() function −
tan(x)
Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.
Parameters
x − This must be a numeric value.
Return Value
This function returns a numeric value between -1 and 1, which represents the tangent of the parameter x.
Example
The following example shows the usage of tan() function −
from math import tan, pi x = 3 val = tan(x) print ("x: ",x, "tan(x): ", val) x = -3 val = tan(x) print ("x: ",x, "tan(x): ", val) x = 0 val = tan(x) print ("x: ",x, "tan(x): ", val) x = pi val = tan(x) print ("x: ",x, "tan(x): ", val) x = pi/2 val = tan(x) print ("x: ",x, "tan(x): ", val)
When we run the above program, it produces the following output −
x: 3 tan(x): -0.1425465430742778 x: -3 tan(x): 0.1425465430742778 x: 0 tan(x): 0.0 x: 3.141592653589793 tan(x): -1.2246467991473532e-16 x: 1.5707963267948966 tan(x): 1.633123935319537e+16
To Continue Learning Please Login
Login with Google