1/54
Looks like no tags are added yet.
Function
A group of statements that performs a specific task
Python, as well as other programming languages, provides
Library of functions
Built-in functions
Always available in the Python interpreter
abs(x)
Returns the absolute value for x
max(x1, x2, …)
Returns the largest among x1, x2, …
min(x1, x2, …)
Returns the smallest among x1, x2, …
pow(a, b)
Returns ab. Same as a ** b
round(x)
Returns an integer nearest to x (if x is equally close to two integers, the even one is returned)
round(x, n)
Returns the float value rounded to n digits after the decimal point
Some of the most popular mathematical functions are defined
in the Python math module
Including trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc
fabs(x)
Returns the absolute value for x as a float
ceil(x)
Rounds x up to its nearest integer and returns that integer
floor(x)
Rounds x down to its nearest integer and returns that integer
exp(x)
Returns the exponential function of x(ex)
e = (Euler's number) that is approximately equal to 2.71828
log(x)
Returns the natural logarithm of x
log(x, base)
Returns the logarithm of x for the specified base
sqrt(x)
Returns the square root of x
sin(x)
Returns the sine of x; x would represent an angle in radians
asin(x)
Returns the angle in radians for the inverse of sine
cos(x)
Returns the cosine of x; x would represent an angle in radians
acos(x)
Returns the angle in radians for the inverse of cosine
tan(x)
Returns the tangent of x; x would represent an angle in radians
degrees(x)
Converts angle x from radians to degrees
radians(x)
Converts angle x from degrees to radians
String
A sequence of characters and can include text and numbers
String values must be enclosed in
Matching single quotes (') or double quotes (")
Double quotes (") for a string with
More than one character
Single quotes (') for a string with
A single character or an empty string
Escape sequence
Backslash (\) followed by a letter or a combination of digits
\b
Backspace
\t
Tab
\n
Newline, line break, or end-of-line (EOL) character; signifies the end of a line
\’
Single quote
\”
Double quote
\f
Character that forces the printer to print from the next page/line
\r
Character that is used to move the cursor to the first position on the same line
When you use the print function, it automatically prints
A linefeed (\n) to cause the output to advance to the next line
‘+’ is an operator that can be used to add two numbers and
Be used to concatenate (join) two strings
The augmented assignment += operator can be used for
String concatenation
You can use the format function (.f) to format a number or
A string and return the result as a string
Width specifier
The width is automatically set to the size needed for formatting the number (defaults to 0)
Conversion code %
To format a number as a percentage
The format of a number is
Right justified
When putting the symbol ‘<‘ in the format specifier, it will
Specify that the item be left-justified in the resulting format within the specified width
Conversion code ‘d’
To format an integer in decimal
Conversion code ‘s’
To format a string with a specified width
To right-justify a string,
Put the symbol ‘>’ in the format specifier
By default, a string is
Left justified
If the string is longer than the specified width,
The width is automatically increased to fit the string
10.2f
Format the float item with width 10 and precision 2
5d
Format the integer item in decimal with width 5
10.2%
Format the number in decimal
50s
Format the string item with width 50
<10.2f
Left-justify the formatted item
>10.2f
Right-justify the formatted item