Python Champions Flashcards
Mix of String methods, list methods, and common built in functions
Adds an element to the end of the list
LIST METHOD
@ .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
Removes all elements from the list
LIST METHOD
@.clear() - Removes all elements of the list
-fruits = [“apple”, “banana”, “cherry”]
-fruits.clear()
-print(fruits)
@output - []
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.clear()
Returns a copy of the list / Copies a list[assign it to a variable I guess]
LIST METHOD
@.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()
This method returns the number of elements with the specified value / the number u typed in
LIST METHOD
@.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.
Adds a iterable[list, tuple, set] to the end of another list
LIST METHOD
@.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)
Returns the index number of the first occurrence of the specified value
LIST METHOD
@.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
Adds an element at the specified position
LIST METHOD
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)
Removes the element at the specified position
LIST METHOD
@.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)
This method gets rid of the first occurrence of the element with the specified value.
LIST METHOD
@.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)
Reverses the order of the list
LIST METHOD
@.reverse() - Reverses the order of the list
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.reverse()
print(fruits)
@output - [‘cherry’, ‘banana’, ‘apple’]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
list.reverse() @no parameters
Organizes the list, can choose criteria[alphabetically, from descending, ascending etc..]
LIST METHOD
@.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)
List Trick #1 [Change an index to whatever string u want, print whats at a certain index]
fruits = [“apple”, “banana”, “cherry”]
print(fruits[1])
PRINTS BANANA
fruits = [“apple”, “banana”, “cherry”]
fruits[0] = “kiwi”
@CHANGES APPLE TO KIWI
not in[for loop example]
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
not in [while loop example]
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.
List trick 2: the elements of s[i:j:k] are replaced by those of t
s[i:j:k] = t
Removes the elements of s[i:j:k]
LIST TRICK
del s[i:j:k]
or
foods = [‘apple’, ‘banana’, ‘carrot’,’chicken’, ‘turkey’, ‘ramen’]
foods[1:3] = ‘ ‘ @replace with space
print(foods)
slice a list from one element to another replaced by what you want or deleted.
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)
Returns the whole thing to uppercase
STRING METHOD
@.upper() - Returns the whole thing to uppercase
txt = “Hello my friends”
x = txt.upper()
print(x)
@output - HELLO MY FRIENDS
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters
Return true if all characters are uppercase
STRING METHOD
@.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.”)
Return true if all characters are lowercase
STRING METHOD
@.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
Returns the whole thing / string to lowercase
STRING METHOD
@.lower() - Returns the whole thing to lowercase
txt = “Hello My FRIENDS”
x = txt.lower()
print(x)
@output - hello my friends
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters
Returns the whole string to lowercase AS WELL AS special characters from different languages like (ß, which is German)
STRING METHOD
@.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
Capitalizes the first letter of the string
STRING METHOD
@.capitalize() - Capitalizes the first letter
txt = “gojo”
x = txt.capitalize()
print (x)
@output - Gojo
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
NO PARAMETERS
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
@.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.”)
Searches the string for a specified value and returns the last occurrence / index of where it was found
STRING METHOD
@.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)
This method finds the first occurrence / index number of the specified value and returns an ERROR if the value is not found.
STRING METHOD
@.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
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
@.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
Replaces placeholders with a value of your choosing
STRING METHOD
@.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)
Turns each word into a list item
STRING METHOD: STRING —> LIST
@.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,
Turns all items in an iterable(Tuple, Dictionary, Set) into a string using a character u choose as a separator
STRING METHOD
@.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)
This method removes any leading(beginning), and trailing(end) whitespaces.
STRING METHOD
@.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)
Returns True or False if every character is a digit
STRING METHOD
@.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.”)
Returns True if all characters in string are letters(Spaces are included as characters)
STRING METHOD
+.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)
Returns the number of times it appears in the string
STRING METHOD
@.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
This method returns True if the string begins with the specified value, otherwise False.
STRING METHOD
@.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
Returns true if the string ends with what u type
STRING METHOD
@.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)
Replace a word you choose from the string with a word you want(Can even replace something with spaces)
STRING METHOD
@.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.”
Decimal Position Trick #1 [Used with the ______ method]
[:.NUMBERf]
[:.NUMBERf]
value = 3.14159265
formatted_value = “[:.2f]”.format(value)
print(formatted_value)
@output - 3.14
2 floating decimal places away
Length of characters
@len() - Length of characters - Function
name = input(“Enter your full name: “)
result = len(name)
print(result) or print(len(name))
len(object) - SYNTAX
Swaps all the characters {from lowercase to upper and vice versa}
@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
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
Creates a set object [items are unordered]
@set()
x = set((“apple”, “banana”, “cherry”))
print(x)
@output - {‘cherry’, ‘banana’, ‘apple’}
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
set(iterable)
Returns a portion of an object, you can choose what portion
@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)
Returns a tuple object
@tuple()
x = [“Alice”, “Bob”, “Charlie”]
names = tuple(x)
@ouput - (‘Alice’, ‘Bob’, ‘Charlie’)
Sums the items of an iterable
@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
Returns the type of an object
@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’>