2. Intro To Python Flashcards

(30 cards)

1
Q

What is the library “BeautifulSoup” used for?

A

BeautifulSoup is the Python standard for scraping data from websites

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

How do you write comments?

A

Add a # at the start of the line

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

What is the convention for naming python variables?

A

all lowercase letters, with words separated by underscores,

eg. “assists_per_game”

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

How would you insert variables inside a string?

A

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}!’

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

What string method allows you to capitalize all letters?

A

.upper()

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

How would you replace something in a string?

A

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”

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

What method allows you to remove whitespace from a string?

A

.lstrip()

’ lionel messi’.lstrip()

–> becomes:
‘lionel messi’

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

What is the difference between = and ==?

A

= is an assignment operator

== is equals (checks a variable for a result)

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

What are “strings, integers, floats and bools” called?

A

Primitives
–> They are the basic building block types

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

What are “lists and dicts” called?

A

Containers
(Sometimes also called “collections”)

–> Because they hold other values

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

How can you recognise a list?

A

Uses square brackets,

eg.
roster_list = [‘ruben dias’, ‘gabriel jesus’, ‘riyad mahrez’]

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

How do you return one element from a list?

A

print(roster_list[0])

–> Prints the first item in the list

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

What is a section of a list called?

A

A slice

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

How do you return a section of a list? (a slice?)

A

A slice returns from the first up to the last number,

roster_list[0:2]

–> Prints the first two items in the list

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

What do dicts have?

A

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’

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

What is a “loop”?

A

Loops are a way to “do something” for every item in a collection

17
Q

How would you use a loop to capitalize all elements in a string?

A

roster_list = [‘………’, “……..”, “……”]
roster_list_upper = [’’, ‘’, ‘’]

For name in roster_list:
roster_list_upper[i] = name.title()
i = i + a

18
Q

What are “list comprehensions”?

A

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.

19
Q

Give an example of a “list comprehension”?

A

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

20
Q

What string method turns a string into substrings?

A

split()

full_name = ‘ruben dias’

print(full_name.split(‘ ‘))
–> [‘ruben’, ‘dias’]

21
Q

What is the programming term where you “do something” to each item in a collection?

A

mapping

As in, mapping (____) to each element of (_____)

22
Q

What is the difference when it comes to “dict comprehensions”?

A

Dict comprehensions work similarly to list comprehensions. Except now, the whole thing is wrapped in {} instead of []

23
Q

What function can be used to find the sum of a list?

A

Pass a list through the sum() function

eg.
sum([1, 2, 3])
–> 6

24
Q

How do you define your own functions?

A

def myfunction(argument1, argument2):

25
What is the programming term when you only have access to a variable inside the function?
scope
26
How do you add a default value to an argument in a function?
def myfunction(yellow, red=0): --> In this case yellow NEEDS a value, wheres red already has an assigned value, making it optional
27
What is the python rule regarding optional argument and required arguments?
Optional arguments HAVE to come after required arguments
28
What is a positional argument?
When you pass the arguments in order, or by position.
29
What is a keyword argument?
The alternative to positional arguments. eg. def myfunction(nyellow, nred): --> Instead of passing arguments as (0, 1), so 1 red card and 1 yellow, you can IGNORE the positions are instead pass the arguments as, (nred=1, nyellow=0)
30