Python Champions Flashcards

Mix of String methods, list methods, and common built in functions

1
Q

Adds an element to the end of the list

LIST METHOD

A

@ .append() - Adds an element to end of list

EXAMPLE:

-fruits = [‘apple’, ‘banana’, ‘cherry’]
-fruits.append(“orange”)

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

list.append(element - Required. An element of any type (string, number, object etc.))
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

EXAMPLE 2: @BETTER TO USE EXTEND TO COMBINE 2 LISTS CAUSE IT LOOK WEIRD

-a = [“apple”, “banana”, “cherry”]
-b = [“Ford”, “BMW”, “Volvo”]
-a.append(b)
-print(a)

@output - [‘apple’, ‘banana’, ‘cherry’, [‘Ford’, ‘BMW’, ‘Volvo’]] @WEIRD

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

Removes all elements from the list

LIST METHOD

A

@.clear() - Removes all elements of the list

-fruits = [“apple”, “banana”, “cherry”]
-fruits.clear()

-print(fruits)

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

list.clear()

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

Returns a copy of the list / Copies a list[assign it to a variable I guess]

LIST METHOD

A

@.copy() - Returns a copy of the list

original_list = [1, 2, 3, 4, 5]

Create a copy of the original list using the copy() method
copied_list = original_list.copy()

Modify the copied list
copied_list.append(6)

Print both lists to demonstrate the copy
print(“Original list:”, original_list)
print(“Copied list:”, copied_list)

@output -
Original list: [1, 2, 3, 4, 5]
Copied list: [1, 2, 3, 4, 5, 6]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

list.copy()

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

This method returns the number of elements with the specified value / the number u typed in

LIST METHOD

A

@.count() - Returns the number of elements there are that you typed in

fruits = [“apple”, “banana”, “cherry”]

x = fruits.count(“cherry”)

print(x)

@output - 1 (cherry only showed up once)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

list.count(value - list, tuple, int, string etc…)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

if example.count() > 0

or

numbers = [1, 2, 3, 4, 3, 5, 6, 3, 7]

value_to_count = 3
count = numbers.count(value_to_count)

threshold = 2
if count > threshold:
print(f”{value_to_count} appears more than {threshold} times in the list.”)
else:
print(f”{value_to_count} appears {count} times in the list, which is not more than {threshold}.”)

@output - 3 appears more than 2 times in the list.

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

Adds a iterable[list, tuple, set] to the end of another list

LIST METHOD

A

@.extend - Adds a iterable[list, tuple, set] to the end of another list

fruits = [‘apple’, ‘banana’, ‘cherry’]

cars = [‘Ford’, ‘BMW’, ‘Volvo’]

fruits.extend(cars)

@output - [‘apple’, ‘banana’, ‘cherry’, ‘Ford’, ‘BMW’, ‘Volvo’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
list.extend(iterable)

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

Returns the index number of the first occurrence of the specified value

LIST METHOD

A

@.index() - Returns the index number of the first occurence of the specified value

fruits = [‘apple’, ‘banana’, ‘cherry’, ‘mango’, ‘cherry’]

x = fruits.index(“cherry”)

@output - 2 (cherry is at index 2)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

list.index(Element - String, Int, list)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

fruits = [“apple”, “banana”, “cherry”, “date”, “elderberry”]

fruit_to_find = “grape”

if fruit_to_find in fruits:
position = fruits.index(fruit_to_find)
print(f”The position of {fruit_to_find} in the list is {position}.”)
else:
print(f”{fruit_to_find} is not in the list.”)

@output - Grape is not in the list

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

Adds an element at the specified position

LIST METHOD

A

A
@.insert() - Adds an element at the specified position

fruits = [‘apple’, ‘banana’, ‘cherry’]

fruits.insert(1, “orange”) @1 is the position, orange is what I want to insert in the list

print(fruits)

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

list.insert(POSITION, ELEMENT)

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

Removes the element at the specified position

LIST METHOD

A

@.pop - removes the element at the specified position

fruits = [‘apple’, ‘banana’, ‘cherry’]

fruits.pop(1) @It pops banana and puts it in the pop method so if u just did print(fruits.pop(1)) it will return as banana since its in pop

print(fruits)

@output - [‘apple’, ‘cherry’]

@print(fruits.pop(1)) = just banana instead of apple, cherry
@[GOTTA DO THE POP IN DIFFERENT VARIABLE THEN PRINT THAT VARIABLE IF U WANT TO REMOVE]

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

SYNTAX

list.pop(position - optional, can choose position to pop if left alone it will pop last one, CAN ALSO DO NEGATIVE NUMBERS LIKE -1 WHICH IS LAST ALSO, or -2 WHICH IS SECOND TO LAST)

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

This method gets rid of the first occurrence of the element with the specified value.

LIST METHOD

A

@.remove() - Removes the item with the specified value, first occurence

fruits = [‘apple’, ‘banana’, ‘cherry’]

fruits.remove(“banana”)

print(fruits)

@output - [‘apple’, ‘cherry’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

SYNTAX

list.remove(element - string, number, list)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

fruits = [“apple”, “banana”, “cherry”, “date”, “elderberry”]

fruit_to_remove = input(“Insert fruit: “)

if fruit_to_remove in fruits:
fruits.remove(fruit_to_remove)
print(f”{fruit_to_remove} was removed from the list.”)
else:
print(f”{fruit_to_remove} is not in the list.”)

print(“Updated list:”, fruits)

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

Reverses the order of the list

LIST METHOD

A

@.reverse() - Reverses the order of the list

fruits = [‘apple’, ‘banana’, ‘cherry’]

fruits.reverse()

print(fruits)

@output - [‘cherry’, ‘banana’, ‘apple’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

SYNTAX

list.reverse() @no parameters

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

Organizes the list, can choose criteria[alphabetically, from descending, ascending etc..]

LIST METHOD

A

@.sort() - Sorts the list, can choose criteria[alphabetically from descending, ascending etc..]

cars = [‘Ford’, ‘BMW’, ‘Volvo’]

cars.sort()

print(cars)

@output - [‘BMW’, ‘Ford’, ‘Volvo’] (sorted from alphabetically so BMW went first cause B)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

SYNTAX

list.sort(reverse=True|False, key=myFunc)

reverse=True will sort the list descending. The Default is reverse=False which is ascending.
@key = myFunc means u can put ur function in to choose criteria
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

@EXTRA

cars = [‘Ford’, ‘BMW’, ‘Volvo’]

cars.sort(reverse=True)

@output - Volvo, BMW, Ford - Sorts the list from descending
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

@EXTRA

A function that returns the length of the value:
def myFunc(e):
return len(e)

cars = [‘Ford’, ‘Mitsubishi’, ‘BMW’, ‘VW’]

cars.sort(key=myFunc)

print(cars)

@output - [‘VW’, ‘BMW’, ‘Ford’, ‘Mitsubishi’] (Sorted from least amount of length to most)

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

List Trick #1 [Change an index to whatever string u want, print whats at a certain index]

A

fruits = [“apple”, “banana”, “cherry”]

print(fruits[1])
PRINTS BANANA

fruits = [“apple”, “banana”, “cherry”]
fruits[0] = “kiwi”

@CHANGES APPLE TO KIWI

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

not in[for loop example]

A

old_list = [‘k’,’l’,’k’,’m’,’m’,’n’,’o’]

new_list = []
for each_element in old_list:
if each_element not in new_list:
new_list.append(element)

print(new_list)

@output - [‘k’, ‘l’, ‘m’, ‘o’]
NO DUPLICATES

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

not in [while loop example]

A

exit_command = “quit”

@Initialize a variable for user input
user_input = “”

@Create a while loop that continues until the user enters the exit command

while user_input not in [exit_command, “exit”]:
user_input = input(“Enter a command (type ‘quit’ or ‘exit’ to exit): “)
print(“You entered:”, user_input)

print(“Exited the loop.”)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
We use a while loop with the condition user_input not in [exit_command, “exit”]. This loop will continue as long as the user’s input is not equal to “quit” or “exit.”

Inside the loop, the user is prompted to enter a command, and their input is stored in the user_input variable. The loop continues until the user enters “quit” or “exit.”

When the user enters “quit” or “exit,” the loop exits, and “Exited the loop” is printed.

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

List trick 2: the elements of s[i:j:k] are replaced by those of t

A

s[i:j:k] = t

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

Removes the elements of s[i:j:k]

LIST TRICK

A

del s[i:j:k]

or

foods = [‘apple’, ‘banana’, ‘carrot’,’chicken’, ‘turkey’, ‘ramen’]

foods[1:3] = ‘ ‘ @replace with space

print(foods)

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

slice a list from one element to another replaced by what you want or deleted.

A

foods = [‘apple’, ‘banana’, ‘carrot’,’chicken’, ‘turkey’, ‘ramen’]

foods[1:3] = ‘ ‘ @Do index numbers of the elements u wanna delete/replace IM DELETING IN THIS CASE BY REPLACING WITH SPACE

print(foods)

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

Returns the whole thing to uppercase

STRING METHOD

A

@.upper() - Returns the whole thing to uppercase

txt = “Hello my friends”

x = txt.upper()

print(x)

@output - HELLO MY FRIENDS
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

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

Return true if all characters are uppercase

STRING METHOD

A

@.isupper() - Return true if all characters are uppercase

txt = “THIS IS NoW!”

x = txt.isupper()

print(x) o print(txt.isupper())

@output - False (Not every character is uppercase, o)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
No Parameters
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

text = “HELLO”

if text.isupper():
print(“All characters in the text are uppercase.”)
else:
print(“Not all characters in the text are uppercase.”)

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

Return true if all characters are lowercase

STRING METHOD

A

@.islower() - Return true if all characters are lowercase

a = “Hello world!”
b = “hello 123”
c = “mynameisPeter”

print(a.islower())
print(b.islower())
print(c.islower())
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

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

Returns the whole thing / string to lowercase

STRING METHOD

A

@.lower() - Returns the whole thing to lowercase

txt = “Hello My FRIENDS”

x = txt.lower()

print(x)

@output - hello my friends
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

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

Returns the whole string to lowercase AS WELL AS special characters from different languages like (ß, which is German)

STRING METHOD

A

@.casefold() - Returns string to lowercase as well as language characters

text = “Stra$ße” @ The German word for “street” with a special character

lower_text = text.lower()

casefolded_text = text.casefold()

print(“Original Text:”, text)
print(“Lowercase Text:”, lower_text)
print(“Casefolded Text:”, casefolded_text)

@output -

Original Text: Straße
Lowercase Text: straße
Casefolded Text: strasse
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

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

Capitalizes the first letter of the string

STRING METHOD

A

@.capitalize() - Capitalizes the first letter

txt = “gojo”

x = txt.capitalize()

print (x)

@output - Gojo
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
NO PARAMETERS

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

Searches the string for a specified value and returns the first position of where it was found, could also do a range find (from _ to _)

STRING METHOD

A

@.find() - Tells the first time it is shown as index number

@RETURNS -1 IF VALUE NOT FOUND

@Can do a Range find where you can start and end the search wherever you want [x = txt.find(“e”, 5, 10)]

txt = “Hello, welcome to my world.”

x = txt.find(“e”)

print(x)

@output - 1, because the first e is shown at index 1
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.find(value, start, end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
text = “Hello, World!”

substring = “World”

find outputs a -1 if the value isnt in the string, so we set it so that if its not equal to -1 we print that it is found and if it returns as -1 then it is not found since if it returns as -1 it really is not there

if text.find(substring) != -1 :
print(f”‘{substring}’ is found in the text.”)
else:
print(f”‘{substring}’ is not found in the text.”)

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

Searches the string for a specified value and returns the last occurrence / index of where it was found

STRING METHOD

A

@.rfind() - Tells / Returns the last time it was shown, shows the index(rfind = reversefind)

txt = “Mi casa, su casa.”

x = txt.rfind(“casa”)

print(x)

@output - 12
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.rfind(value, start, end)

26
Q

This method finds the first occurrence / index number of the specified value and returns an ERROR if the value is not found.

STRING METHOD

A

@.index() - The index() method finds the first occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.

txt = “Hello, welcome to my world.”

x = txt.index(“e”)

print(x)

@output - 1
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.index(value, start, end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt = “Hello, welcome to my world.”

print(txt.find(“q”))
print(txt.index(“q”))

@output -

-1
ERROR / EXCEPTION

27
Q

This method finds the last occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.

STRING METHOD

A

@.rindex() - The rindex() method finds he last occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.
txt = “Mi casa, su casa.”

x = txt.rindex(“casa”)

print(x)

@output - 12
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.index(value, start, end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt = “I’m kool and this a really kool world!”

x = txt.rindex(“kool”, 3, 36) @RANGE TYPE SHI….

print(x)

@output - 27

28
Q

Replaces placeholders with a value of your choosing

STRING METHOD

A

@.format() - Replaces placeholders with value of your choosing

name = “Alice”
age = 30

greeting = “Hello, my name is {} and I am {} years old.”.format(name, age)

print(greeting)

@output - Hello, my name is Alice and I am 30 years old.
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.format(value1, value2…)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt1 = “My name is {fname}, I’m {age}”.format(fname = “John”, age = 36)

txt2 = “My name is {0}, I’m {1}”.format(“John”,36)

txt3 = “My name is {}, I’m {}”.format(“John”,36)

29
Q

Turns each word into a list item

STRING METHOD: STRING —> LIST

A

@.split() - Turns each word into a list item

txt = “welcome to the jungle”

x = txt.split()

print(x)

@output - [‘welcome’, ‘to’, ‘the’, ‘jungle’]

SYNTAX

string.split(separator, maxsplit)

You can specify the separator, default separator is any whitespace.

When maxsplit is specified, the list will contain the specified number of elements plus one
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt = “apple&banana&cherry&orange”

setting the maxsplit parameter to 1, will return a list with 2 elements!

x = txt.split(“&”, 1)

print(x)

@output -
[‘apple’, ‘banana&cherry&orange’]

@Used hashtag as seperators in a list,

30
Q

Turns all items in an iterable(Tuple, Dictionary, Set) into a string using a character u choose as a separator

STRING METHOD

A

@.join() - Turns all items in an iterable(Tuple, Dictionary, Set) into a string using a character u choose as a separator:

myTuple = (“John”, “Peter”, “Vicky”)

x = “&”.join(myTuple)

print(x)

@output - John&Peter&Vicky (Seperated with hashtags but you could just do a space or anything else)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.join(iterable)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
myDict = {“name”: “John”, “country”: “Norway”}
mySeparator = “TEST”

x = mySeparator.join(myDict)

print(x)

@output - nameTESTcountry

@for dictionaries it only outputs the keys not the values(name, country) not (John Norway)

31
Q

This method removes any leading(beginning), and trailing(end) whitespaces.

STRING METHOD

A

@.strip() - The strip() method removes any leading(beginning), and trailing(end) whitespaces.
@You can specify which character(s) to remove, if not, any whitespaces will be removed.

txt = “ banana “

x = txt.strip()

print(“of all fruits”, x, “is my favorite”)

output - of all fruits banana is my favorite (I left paramater open but if u put a character in there it will remove it from the beginning and end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.strip(characters)

32
Q

Returns True or False if every character is a digit

STRING METHOD

A

@.isdigit() - Returns True or False if every character is a digit

txt = “50800”

x = txt.isdigit()

print(x)

@output - True (Because all of the characters in txt are digits)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

user_input = input(“Enter a string: “)

if user_input.isdigit():
print(“The input consists of only digits.”)
else:
print(“The input contains non-digit characters.”)

33
Q

Returns True if all characters in string are letters(Spaces are included as characters)

STRING METHOD

A

+.isalpha() - Returns True if all characters in string are letters(Spaces are included as characters)

+output - False (Because there is a space)

txt = “The Owner”

x = txt.isalpha()

print(x)

34
Q

Returns the number of times it appears in the string

STRING METHOD

A

@.count() - Returns number of times it appears in the string

txt = “I love apples, apple are my favorite fruit”

x = txt.count(“apple”)

print(x)

@output - 2
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX -

string.count(value, start, end)
@ITS A RANGE U CAN CHOOSE FROM

35
Q

This method returns True if the string begins with the specified value, otherwise False.

STRING METHOD

A

@.startswith() method returns True if the string starts with the specified value, otherwise False.

txt = “Hello, welcome to my world.”

x = txt.startswith(“Hello”)

print(x)

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

SYNTAX

string.startswith(value, start, end)

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

txt = “Hello, welcome to my world.”

x = txt.startswith(“wel”, 7, 20)

print(x)

@output - True

36
Q

Returns true if the string ends with what u type

STRING METHOD

A

@.endswith() - Returns true if it end with what u type

txt = “My World”

x = txt.endswith(“d”)

print(x)

@output - True, because it ends in a d
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.endswith(value, start, end)

37
Q

Replace a word you choose from the string with a word you want(Can even replace something with spaces)

STRING METHOD

A

@.replace() - Replace a word you choose from the string with a word you want(Can even replace something with spaces)

txt = “I like bananas”

x = txt.replace(“bananas”, “apples”)

print(x)

@output - I like apples
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.replace(oldvalue, newvalue, count)

@count - Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences

txt = “one one was a race horse, two two was one too.”

x = txt.replace(“one”, “three”, 2)

print(x)

@output - three three was a race horse, two two was ONE too.”

38
Q

Decimal Position Trick #1 [Used with the ______ method]

[:.NUMBERf]

A

[:.NUMBERf]

value = 3.14159265
formatted_value = “[:.2f]”.format(value)
print(formatted_value)

@output - 3.14

2 floating decimal places away

39
Q

Length of characters

A

@len() - Length of characters - Function

name = input(“Enter your full name: “)

result = len(name)

print(result) or print(len(name))

len(object) - SYNTAX

40
Q

Swaps all the characters {from lowercase to upper and vice versa}

A

@swapcase() - Swap upper and lower case characters from a string

txt = “Hello My Name Is PETER”

x = txt.swapcase()

print(x)

@output - hELLO mY nAME iS peter

41
Q

Returns the absolute value of an integer

A

@abs()

x = abs(-7.25)
print(x)

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

abs(number)

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

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

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

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

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

47
Q

Converts the specified number into a hexadecimal value.

A

@hex()

x = hex(255)
print(x)

@output - 0xff

Syntax - hex(number)

48
Q

Returns the hash value of the specified object

A

@hash()

hash(42)

@output - UNKNOWN

49
Q

Allows for user input

A

@input()

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

@output - Hello ________ = input

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

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

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

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

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

55
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

56
Q

Creates a set object [items are unordered]

A

@set()

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

print(x)

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

set(iterable)

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

58
Q

Returns a tuple object

A

@tuple()

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

names = tuple(x)

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

59
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

60
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’>