Week 9: Et Cetera
| Week 9: Practice Code | GitHub |
|---|
Set set()
Sets are used to store multiple items in a single variable.
Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are list(), tuple(), and dict(), all with different qualities and usage.
A set is a collection which is unordered, unchangeable, and unindexed.
- Sets are written with curly brackets.
- They don’t allow duplicate values.
- Set items can appear in a different order every time, and cannot be referred to by index or key.
- The values
Trueand1are considered the same value in sets, and are treated as duplicates. - The values
Falseand0are considered the same value in sets, and are treated as duplicates.
Set items are unchangeable, but you can remove items and add new items
Get the Length of a Set
To determine how many items a set has, use len() func.
Set Items — Data Types
Set items can be of any data type.
String, int, and boolean data types:
A set can contain different data types.
The set() Constructor
It is also possible to use the set() constructor to make a set.
Remove Item
To remove an item in a set, use the remove() or the discard() method.
- You can also use the
pop()method to remove an item, but this method will remove a random item, so you cannot be sure what item gets removed. - The
clear()method empties the set. - The
delkeyword will delete the set completely.
If the item to remove doesn’t exist, remove() will raise an error.
Meanwhile, it the item to remove doesn’t exist, discard() will raise no error.
The global Variable
Variables that are created outside of a function are known as global variables
Global variables can be used by everyone, both inside of functions and outside.
- If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the func. The global variable with the same name will remain as it was, global and with original value.
The global keyword
Normally, when you create a var inside a func, that var is local, and can only be used inside that func.
To create a global var inside a function, you can use global keyword
To change a value of a global variable inside a func, refer to the variable by using global keyword.