2. Intro To Python Flashcards
(30 cards)
What is the library “BeautifulSoup” used for?
BeautifulSoup is the Python standard for scraping data from websites
How do you write comments?
Add a # at the start of the line
What is the convention for naming python variables?
all lowercase letters, with words separated by underscores,
eg. “assists_per_game”
How would you insert variables inside a string?
Do that using an f-string (add the letter f just preceding the quotation mark), with variables between curly brackets
eg.
f’{description} by {starting_fwd}!’
What string method allows you to capitalize all letters?
.upper()
How would you replace something in a string?
Use the .replace() method.
eg.
‘Cristiano Ronaldo, Man U’.replace(‘Man U’, ‘Real Madrid’)
This will output a string that literally replaces “Man U” by “Real Madrid”
What method allows you to remove whitespace from a string?
.lstrip()
’ lionel messi’.lstrip()
–> becomes:
‘lionel messi’
What is the difference between = and ==?
= is an assignment operator
== is equals (checks a variable for a result)
What are “strings, integers, floats and bools” called?
Primitives
–> They are the basic building block types
What are “lists and dicts” called?
Containers
(Sometimes also called “collections”)
–> Because they hold other values
How can you recognise a list?
Uses square brackets,
eg.
roster_list = [‘ruben dias’, ‘gabriel jesus’, ‘riyad mahrez’]
How do you return one element from a list?
print(roster_list[0])
–> Prints the first item in the list
What is a section of a list called?
A slice
How do you return a section of a list? (a slice?)
A slice returns from the first up to the last number,
roster_list[0:2]
–> Prints the first two items in the list
What do dicts have?
Keys and values,
roster_dict = {‘CB’: ‘ruben dias’, ‘CF’: ‘gabriel jesus’, ‘RW’: ‘riyad mahrez’}
In this case ‘CB’ is a key and ‘ruben dias is a value’
What is a “loop”?
Loops are a way to “do something” for every item in a collection
How would you use a loop to capitalize all elements in a string?
roster_list = [‘………’, “……..”, “……”]
roster_list_upper = [’’, ‘’, ‘’]
For name in roster_list:
roster_list_upper[i] = name.title()
i = i + a
What are “list comprehensions”?
When you want to go from one list to another, different list you should be thinking comprehension.
All list comprehensions take the form [a for b in c] where c is the list you’re iterating over (starting with), and b is the variable you’re using in a to specify exactly what you want to do to each item.
Give an example of a “list comprehension”?
roster_list = [‘ruben dias’, ‘gabriel jesus’, ‘riyad mahrez’]
roster_list_proper = [x.title() for x in roster_list]
–> In this case, “x.title()” is A, “x” is B and “roster_list” is C
What string method turns a string into substrings?
split()
full_name = ‘ruben dias’
print(full_name.split(‘ ‘))
–> [‘ruben’, ‘dias’]
What is the programming term where you “do something” to each item in a collection?
mapping
As in, mapping (____) to each element of (_____)
What is the difference when it comes to “dict comprehensions”?
Dict comprehensions work similarly to list comprehensions. Except now, the whole thing is wrapped in {} instead of []
What function can be used to find the sum of a list?
Pass a list through the sum() function
eg.
sum([1, 2, 3])
–> 6
How do you define your own functions?
def myfunction(argument1, argument2):