Common Functions Flashcards
Returns the absolute value of an integer
@abs()
x = abs(-7.25)
print(x)
@output - 7.25
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
abs(number)
This function returns True if [ALL] items in an iterable are true, otherwise it returns False.
*Also returns True if iterable is empty
@all()
mylist = [True, True, True]
x = all(mylist)
print(x)
@output - True
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
all(iterable - list, tuple, dict)
This function returns True if [ANY] items in an iterable are true, otherwise it returns False.
@any()
mylist = [False, True, False]
x = any(mylist)
print(x)
@output - True
@BEcuase one of the items in the list is True therefore it comes out as True
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
any(iterable - list, tuple, dict)
Returns the boolean value of specified object, will usually return True unless empty, 0, or False
@bool()
x = bool(1)
@output - True
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
bool(object - String, Number, List)
Creates a dictionary, you are able to set the variables and stuff
@dict()
x = dict(name = “John”, age = 36, country = “Norway”)
print(x)
@output - {‘name’: ‘John’, ‘age’: 36, ‘country’: ‘Norway’}
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
dict(key = value, key value, etc…)
Takes a collection(list, tuple, dict) and returns the index number of each element in the collection.
@enumerate()
x = (‘apple’, ‘banana’, ‘cherry’)
y = enumerate(x)
print(list(y))
@output - [(0, ‘apple’), (1, ‘banana’), (2, ‘cherry’)]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
enumerate(iterable, start)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
@EXTRA
x = (‘apple’, ‘banana’, ‘cherry’)
y = enumerate(x, start = 1) @Starts index at 1
print(list(y))
@output - [(1, ‘apple’), (2, ‘banana’), (3, ‘cherry’)]
Converts the specified number into a hexadecimal value.
@hex()
x = hex(255)
print(x)
@output - 0xff
Syntax - hex(number)
Returns the hash value of the specified object
@hash()
hash(42)
@output - UNKNOWN
Allows for user input
@input()
x = input(‘Enter your name: ‘)
print(f’Hello {x}!’)
@output - Hello ________ = input
Returns the item with the lowest value
@min()
example_list = [1,2,3,4,5]
y = min(example_list)
x = min(5, 10)
print(y)
print(x)
@output -
1
5
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
min(n1, n2, n3, …)
or
min(iterable)
Returns the item with the highest value
@max()
example_list = [1,2,3,4,5]
y = max(example_list)
x = max(5, 10)
print(y)
print(x)
@output -
5
10
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
max(n1, n2, n3, …)
or
max(iterable)
Opens a file
@open()
f = open(“demofile.txt”, “r”)
print(f.read())
@output - Welcome User! @The demofile.txt contains this message
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
open(file, mode)
@modes - r[read], w[write], a[append], x[create]
This function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
@range()
x = range(6)
for n in x:
print(n)
@output -
0
1
2
3
4
5
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
range(start, stop, step)
@Step - increments of ___
Returns a reversed iterable
@reversed()
alph = [“a”, “b”, “c”, “d”]
ralph = reversed(alph)
for x in ralph:
print(x, end=’ ‘)
@output - d c b a
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
reversed(sequence)
Returns a rounded decimal / floating point
@round()
x = round(5.76543, 2) @2 = 2 digits from decimal
print(x)
@output - 5.77
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
round(number, digits [optional] )
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
x = round(5.5)
y = round(5)
print(x)
print(y)
@output -
6
5