Methods Flashcards

1
Q

How do find out more information about a method?

A

help(mylist.insert)

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

What is a method?

A

Methods are essentially functions built into objects

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

What is an object?

A

An object is simply a collection of data (variables) and methods (functions) that act on those data.

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

What is .append()?

Give an example

A

append() allows us to add elements to the end of a list:

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

1st.append(6)

OUTPUT

[1, 2, 3, 4, 5, 6]

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

What is .count()?

Give an example

A

The count() method will count the number of occurrences of an element in a list.

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

1st.count(2)

OUTPUT

1

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

What is .split()?

Give an example

A

Splits a string in a list item

txt = “welcome to the jungle”

x = txt.split()

print(x)
print(x[0])
print(x[0][1])
print(x[:2])

OUTPUT

[‘welcome’, ‘to’, ‘the’, ‘jungle’]
welcome
e
[‘welcome’, ‘to’]

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

What is .join()?

Give example

A

The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:

numList = [‘1’, ‘2’, ‘3’, ‘4’]
separator = ‘, ‘
print(separator.join(numList))

This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:

Example 2:

> > > print(“ “.join([‘Hello’, ‘world’])
OUTPUT
Hello world

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