MOD2: H6 - H9 Flashcards
What if you try to use a Negative Number as an Index? Shouldn’t Python throw an Error?
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.
For Sorting - What if a List has both strings and numbers in it?
Python sorts the numbers, then the strings.
How would you count how many times an item is in a list?
count()
How do you add a new item to a list?
append() or insert()
What is the difference between reverse() and sort()?
sort() - will sort in alphabetical or numerical order.
reverse() - takes a list in the order it was created in, and reverses the order.
How would you get a list of names to be sorted in reverse alphabetical order?
First - Sort it Alphabetically. sort()
Then - Reverse Sort it. reverse()
What if I want a Range that’s printed in descending order?
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.
What happens if I change a list while I”m looping through it?
In Python, if you change a list while looping through it, the Loop will work with the change you made.
What is the difference between a FOR Loop and a WHILE Loop?
For Loops run through a List of items.
While Loops keep running until the expression is True.
How would you get a range of even numbers, from 1 to 100?
You would need to use a range function.
range(2, 101, 2)
How do you exit a Loop?
For both FOR and WHILE Loops, the Break Statement is used to exit loops before they would exit on their own.
Can I put a Function in a Function?
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.
Can I call **kwargs and *args something different?
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.
Can Functions call themselves?
Yes - It’s called Recursion, and is more advanced.
How are values passed into Functions?
Parameters - Values are passed into functions through Parameters.