Collection of Data and File Operation Flashcards

1
Q

What are the three main basic set methods?

A
  • add()
  • remove()
  • clear()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a set? Explain and demonstrate

A
  • Structure that holds data in unsorted manner.
  • Can hold mixed Data Types
  • Parentheses {}
example_set = {"hallo", "genau", 8, "wie"} 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you dublicate elements of the same Value in Sets?

A
  • No
  • It will not produce error, but will not be added
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to make sets and other iterables immutable? Explain and demostrate

A
  • By creating a frozen set
  • There will not be any changes allowed
  • Use () around the iterable
my_frozen_set = frozenset({1, 95, "Frozen")}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Whats the difference between sets and lists?

A
  • Lists allow to duplicate Elements within lists
  • Lists are orderer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Demonstrate how a list and a set are created

A
example_list = ["Hallo", 2, "!"]
example_set = {"Hallo", 2, "!"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name 5 Methods you can use on Lists

A
append()
insert()
remove()
clear()
count()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which method does not work on lists and why? Also name alternatives.

A
add()
  • Lists are ordered

Alternatives:
~~~
append() # adds to back
insert() # adds at specified index
~~~

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

Why do we have both, sets and lists?

A
  • Sets are much more effective and faster of identifying items
  • Lists much faster in looping through to view or manipulate items
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a tupil? Explain and demonstrate

A
  • Behaves like list structure but is immutable
  • Sequence of immutable objects
example_tuple = ("Hallo", 3, "!")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Dictionary? Explain and demonstrate

A
  • Collections that are unordered and mutable
  • Each value consists of key and value
my_dictionary = {"Hair color": "Brown", "Eye Color : "Hazel"}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

There are two ways of obtaining values of dictionary key. Demonstrate.

A
  1. Name in []
my_dictionary["Eye color"]
  1. get()
my_dictionary.get("Age")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you open Files in python. Explain and Demonstrate.

A
  1. Open file with open method
  2. Specify filename
  3. Specify fileoption
my_file = open("myfile.txt", "w")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which file options exist? Explain

A

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

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

Write: “First File, here we go” to my_file Demonstrate

A
my_file = open("myfile.txt", "w")
my_file.write("First File, here we go")
my_file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a filelock. Explain

A

Prevents two (or more) applications from writing to the same files at the same time.

17
Q

How to read the file myfile.txt? Demonstrate

A

Opening the file “myfile.txt!” in read mode

# Opening the file in read mode
my_file = open("myfile.txt!", "r")

Reading data from the file
data = my_file.read()

Printing the data
print(data)

Closing the file
my_file.close()
17
Q

I want to open and write to a file called “thatfile.txt”. If the file does not exist, I want to create it but if it does exist, I want to write at the end of the file, preserving what’s already in the file. How do I do that? Demonstrate

A
my_file = open("thatfile.txt", "a")
my_file.write("Your Data\n")
file.close()