Function
|
Description/Example
|
abs(x)
|
Returns the absolute value of a number. The argument may be an integer, a floating point number, or an object implementing __abs__(). If the argument is a complex number, its magnitude is returned.
Example:
x = 5
y = -5
print(abs(x))
print(abs(y))
Output:
5
5
|
chr(i)
|
Returns the string representing a character whose Unicode code point is the integer i.
Example: Use the ASCII table above for reference:
print(chr(35), end="")
print(chr(43), end="")
print(chr(53), end="")
print(chr(64), end="")
print(chr(65), end="")
print(chr(90), end="\n")
Output:
#+5@AZ
|
dir(object)
|
Without arguments, returns the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
Example:
x = 10
myList = [1,2,3,4,5,6,7,8,9,0]
print(dir())
Output:
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'myList', 'x']
|
format(value, format_spec='')
|
Converts a value to a “formatted” representation, as controlled by format_spec. The interpretation of format_spec will depend on the type of the value argument. See the format_spec table below for a list of all the format specifiers.
Example:
name = "Alice"
age = 25
person = {"name": "Bob", "age": 30}
pi = 3.14159265359
print("My name is {} and I am {} years old.".format(name, age))
print("My name is {name} and I am {age} years old.".format(**person))
print("{0} is {1} years old and {0}'s favorite color is {2}.".format("Alice", 25, "blue"))
print("The {fruit} is {adjective}.".format(fruit="apple", adjective="delicious"))
print("Pi is approximately {:.2f}.".format(pi))
Output:
My name is Alice and I am 25 years old.
My name is Bob and I am 30 years old.
Alice is 25 years old and Alice's favorite color is blue.
The apple is delicious.
Pi is approximately 3.14.
format_spec
|
Description
|
:< | Left aligns the result (within the available space) |
:> | Right aligns the result (within the available space) |
:^ | Center aligns the result (within the available space) |
:= | Places the sign to the left most position |
:+ | Use a plus sign to indicate if the result is positive or negative |
:- | Use a minus sign for negative values only |
: | Use a space to insert an extra space before positive numbers (and a minus sign before negative numbers) |
:, | Use a comma as a thousand separator |
:_ | Use a underscore as a thousand separator |
:b | Binary format |
:c | Converts the value into the corresponding unicode character |
:d | Decimal format |
:e | Scientific format, with a lower case e |
:E | Scientific format, with an upper case E |
:f | Fix point number format |
:F | Fix point number format, in uppercase format (show inf and nan as INF and NAN) |
:g | General format |
:G | General format (using a upper case E for scientific notations) |
:o | Octal format |
:x | Hex format, lower case |
:X | Hex format, upper case |
:n | Number format |
:% | Percentage format |
|
int(x=0) or (x, base=10)
|
Returns an integer object constructed from a number or string x, or return 0 if no arguments are given.
Example:
stringNumber = "55"
print(int(stringNumber))
print(int(stringNumber) + 10)
Output:
55
65
|
len(s)
|
Returns the length (the number of items) of an object.
Example:
lst = [33, 59, 10, 82, 44, 39]
print(len(lst))
strng = "United States of Ameria"
print(len(strng))
Output:
6
23
|
max(iterable, *, key=None) or (iterable, *, default, key=None) or (arg1, arg2, *args, key=None)
|
Return the largest item in an iterable or the largest of two or more arguments.
Example:
lst = [33, 59, 10, 82, 44, 39]
print(max(lst))
Output:
82
|
min(iterable, *, key=None) or (iterable, *, default, key=None) or (arg1, arg2, *args, key=None)
|
Return the smallest item in an iterable or the smallest of two or more arguments.
Example:
lst = [33, 59, 10, 82, 44, 39]
print(min(lst))
Output:
10
|
print(*objects, sep=' ', end='\n', file=None, flush=False)
|
Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments.
Example:
print("Name & SSN:")
print("Bob", end=" ")
print("Smith")
print("111","22","3333", sep="-")
Output:
Name & SSN:
Bob Smith
111-22-3333
|
str(object='') or (object=b'', encoding='utf-8', errors='strict')
|
Returns a string version of the specified object.
Example:
pi = 3.14
print(pi)
print(str(pi))
print("The value of PI is " + str(pi))
Output:
3.14
3.14
The value of PI is 3.14
|
sum(iterable, /, start=0)
|
Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.
Example:
nums = [2,4,6,8,10]
print(sum(nums))
nums = (2,4,6,8,10)
print(sum(nums))
print(sum(nums,10)) # Add 10 to the sum of nums
Output:
30
30
40
|
type(object) or (name, bases, dict, **kwds)
|
With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.
Example:
x = 10
y = 3.14
s = "Bob"
c = 'A'
lst = [1,2,3,4,5]
tup = (1,2,3,4,5)
print(type(x))
print(type(y))
print(type(s))
print(type(c))
print(type(lst))
print(type(tup))
Output:
< class 'int' >
< class 'float' >
< class 'str' >
< class 'str' >
< class 'list' >
< class 'tuple' >
|