Collection of Data and File Operation Flashcards
What are the three main basic set methods?
- add()
- remove()
- clear()
What is a set? Explain and demonstrate
- Structure that holds data in unsorted manner.
- Can hold mixed Data Types
- Parentheses {}
example_set = {"hallo", "genau", 8, "wie"}
Can you dublicate elements of the same Value in Sets?
- No
- It will not produce error, but will not be added
How to make sets and other iterables immutable? Explain and demostrate
- By creating a frozen set
- There will not be any changes allowed
- Use () around the iterable
my_frozen_set = frozenset({1, 95, "Frozen")}
Whats the difference between sets and lists?
- Lists allow to duplicate Elements within lists
- Lists are orderer
Demonstrate how a list and a set are created
example_list = ["Hallo", 2, "!"] example_set = {"Hallo", 2, "!"]
Name 5 Methods you can use on Lists
append() insert() remove() clear() count()
Which method does not work on lists and why? Also name alternatives.
add()
- Lists are ordered
Alternatives:
~~~
append() # adds to back
insert() # adds at specified index
~~~
Why do we have both, sets and lists?
- Sets are much more effective and faster of identifying items
- Lists much faster in looping through to view or manipulate items
What is a tupil? Explain and demonstrate
- Behaves like list structure but is immutable
- Sequence of immutable objects
example_tuple = ("Hallo", 3, "!")
What is a Dictionary? Explain and demonstrate
- Collections that are unordered and mutable
- Each value consists of key and value
my_dictionary = {"Hair color": "Brown", "Eye Color : "Hazel"}
There are two ways of obtaining values of dictionary key. Demonstrate.
- Name in []
my_dictionary["Eye color"]
- get()
my_dictionary.get("Age")
How do you open Files in python. Explain and Demonstrate.
- Open file with open method
- Specify filename
- Specify fileoption
my_file = open("myfile.txt", "w")
Which file options exist? Explain
x - “create”
* Creates new file, with specified filename
a - “append”
* If file not exists: Create new one
* If file exists, all will be appended
w - “write”
* Create new file
* Erase all contents
r - “read”
* Default
Write: “First File, here we go” to my_file Demonstrate
my_file = open("myfile.txt", "w") my_file.write("First File, here we go") my_file.close()