Common Functions Flashcards

1
Q

Returns the absolute value of an integer

A

@abs()

x = abs(-7.25)
print(x)

@output - 7.25
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

abs(number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

This function returns True if [ALL] items in an iterable are true, otherwise it returns False.

*Also returns True if iterable is empty

A

@all()

mylist = [True, True, True]
x = all(mylist)
print(x)

@output - True
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

all(iterable - list, tuple, dict)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

This function returns True if [ANY] items in an iterable are true, otherwise it returns False.

A

@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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Returns the boolean value of specified object, will usually return True unless empty, 0, or False

A

@bool()

x = bool(1)
@output - True
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

bool(object - String, Number, List)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Creates a dictionary, you are able to set the variables and stuff

A

@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…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Takes a collection(list, tuple, dict) and returns the index number of each element in the collection.

A

@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’)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Converts the specified number into a hexadecimal value.

A

@hex()

x = hex(255)
print(x)

@output - 0xff

Syntax - hex(number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Returns the hash value of the specified object

A

@hash()

hash(42)

@output - UNKNOWN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Allows for user input

A

@input()

x = input(‘Enter your name: ‘)
print(f’Hello {x}!’)

@output - Hello ________ = input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Returns the item with the lowest value

A

@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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Returns the item with the highest value

A

@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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Opens a file

A

@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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

This function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

A

@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 ___

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Returns a reversed iterable

A

@reversed()

alph = [“a”, “b”, “c”, “d”]

ralph = reversed(alph)

for x in ralph:
print(x, end=’ ‘)

@output - d c b a
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

reversed(sequence)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Returns a rounded decimal / floating point

A

@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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Creates a set object [items are unordered]

A

@set()

x = set((“apple”, “banana”, “cherry”))

print(x)

@output - {‘cherry’, ‘banana’, ‘apple’}
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

set(iterable)

17
Q

Returns a portion of an object, you can choose what portion

A

@slice()

a = (“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”)

x = slice(3)

print(a[x])

@output - (‘a’, ‘b’, ‘c’)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

slice(start, stop, step) @step = increments
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

subset = my_list[2:7]

print(“Subset using slice:”, subset)

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

EXTRA

my_list = [2, 4, 6, 8, 10, 12]

my_slice = slice(1, 3)

subset = my_list[my_slice]

print(“Subset using slice object:”, subset)

18
Q

Returns a tuple object

A

@tuple()

x = [“Alice”, “Bob”, “Charlie”]

names = tuple(x)

@ouput - (‘Alice’, ‘Bob’, ‘Charlie’)

19
Q

Sums the items of an iterable

A

@sum()

a = (1, 2, 3, 4, 5)
x = sum(a)
print(x)

@output - 15

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

sum(iterable, extra) @extra number to add
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
EXTRA

a = (1, 2, 3, 4, 5)
x = sum(a, 7)
print(x)

@output - 22 @ 7 + 1 + 2 + 3 + 4 + 5

20
Q

Returns the type of an object

A

@type()

a = (‘apple’, ‘banana’, ‘cherry’)
b = “Hello World”
c = 33

x = type(a)
y = type(b)
z = type(c)

@output -

<class ‘tuple’>
<class ‘str’>
<class ‘int’>

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]