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.”)