MOD2: H6 - H9 Flashcards

1
Q

What if you try to use a Negative Number as an Index? Shouldn’t Python throw an Error?

A

Not as long as the Number’s an Integer.

If you give Python a Negative Number, it will start at the last item in the list and count backwards.

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

For Sorting - What if a List has both strings and numbers in it?

A

Python sorts the numbers, then the strings.

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

How would you count how many times an item is in a list?

A

count()

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

How do you add a new item to a list?

A

append() or insert()

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

What is the difference between reverse() and sort()?

A

sort() - will sort in alphabetical or numerical order.

reverse() - takes a list in the order it was created in, and reverses the order.

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

How would you get a list of names to be sorted in reverse alphabetical order?

A

First - Sort it Alphabetically. sort()

Then - Reverse Sort it. reverse()

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

What if I want a Range that’s printed in descending order?

A

Start with the Highest Number, end with a number that’s 1 less from where you want to stop, and give it a Step of -1.

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

What happens if I change a list while I”m looping through it?

A

In Python, if you change a list while looping through it, the Loop will work with the change you made.

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

What is the difference between a FOR Loop and a WHILE Loop?

A

For Loops run through a List of items.

While Loops keep running until the expression is True.

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

How would you get a range of even numbers, from 1 to 100?

A

You would need to use a range function.

range(2, 101, 2)

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

How do you exit a Loop?

A

For both FOR and WHILE Loops, the Break Statement is used to exit loops before they would exit on their own.

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

Can I put a Function in a Function?

A

Yes - But think about whether that bit of code could be used on it’s own. If so, you might want to make it its own Function instead.

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

Can I call **kwargs and *args something different?

A

Yes

**kwargs and *args are Standard, if you feel like calling them something different, you can do so. It does make your code a bit hard to read though for others that may want to work with you later, so only do this if it’s absolutely necessary.

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

Can Functions call themselves?

A

Yes - It’s called Recursion, and is more advanced.

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

How are values passed into Functions?

A

Parameters - Values are passed into functions through Parameters.

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

When is a Variable in Scope?

A

Variables are IN Scope when you can reference it, they can be referenced in a LIST.

17
Q

What do *args and **kwargs do?

A

Allows you to pass a varying number of Parameters into a Function.

18
Q

What’s the deal with “pop”? It seems like an odd term. Why not “Remove”?

A

This is Programming terminology - many times when languages are being written, lists are written with pop and push functions.

19
Q

Are Dictionaries used in all programming languages?

A

They aren’t Unique to just Python, but they’re often called different things in other languages.
Hash Maps, Hash Tables, or associative arrays to name some.

20
Q

What pairs do you store in Dictionaries?

A

Keys and Values

21
Q

Which of the following is true?

Keys are always Unique
Values are always Unique
Both Keys and Values must be Unique
Neither Keys nor Values have to be Unique

A

Keys are always Unique

If you store a new Value under an existing Key, the Current Value under that Key will be overwritten.

22
Q

How do you get all the Keys out of a Dictionary?

A

keys()

23
Q

How do you store a new Value in a Dictionary?

A

dict [‘Key’] = ‘NewValue’