Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 1_collections.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# # generic built-in functions
# generic built-in functions
numbers = [45, 66, 12, 3, 99, 3.142, 42]
print("min:", min(numbers), "max:", max(numbers))
print("sum:", sum(numbers))
Expand Down
Empty file added 4b_practice.py
Empty file.
47 changes: 47 additions & 0 deletions Daphs_practice_read_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
my_practice = open('sample_files/sisy.txt', 'r') # determine the location of the file and open file for reading
print(my_practice.read())

my_practice = open('sample_files/sisy.txt', 'r')
print(my_practice.read(6)) # read first 6 characters

my_practice = open('sample_files/sisy.txt', 'r')
print(my_practice.readline()) # reads the first line

print("#" * 20)
for line in range(2): # reads the 2nd line as specified
print(my_practice.readline())
print("#" * 20)

new = open('sample_files/sisy.txt', 'r').read() # slurping: reads the entire file in one go
print(new)
print("#" * 20)

new_list = open('sample_files/sisy.txt', 'r').read().splitlines() # turn file into a list
print(new_list)

print("#" * 20)
for line in open('sample_files/sisy.txt', 'r'):
print(line[:-2]) # exclude the last two characters of u and ?

my_practice.close() # closes the file

# file writing.....in reading, writing or appending, always open the file for reading after altering it
my_practice = open('sample_files/sisy.txt', 'a') # "a" - Append - will append to the end of the file
item1 = my_practice.write('New content added')
my_practice = open('sample_files/sisy.txt', 'r')
print(my_practice.read()) # prints the new content added

print("#" * 20)
practice_3 = open('sample_files/sisy.txt', 'w') # "w" - Write - will overwrite any existing content
practice_3.write('oops! whole file changed')
practice_3 = open('sample_files/sisy.txt', 'r')
print(practice_3.read())

print("#" * 20)
# You can create a new file
# "x" - Create - will create a file, returns an error if the file exists
#
# "a" - Append - will create a file if the specified file does not exists
#
# "w" - Write - will create a file if the specified file does not exists

34 changes: 34 additions & 0 deletions Exercise11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python

# Belgium is a variable attached to a string
# 012345678901234567890123456789012340123456789012345678901234567890123401234567890123456789012345678901234
Belgium = 'Belgium,10445852,Brussels,737966,Europe,1830,Euro,Catholicism,Dutch,French,German'

# Print a line of hyphens the same length as the Belgium string
# Use the len() function to return the number of elements in an object
print("Length of a string:", len(Belgium))
print("Length of a string:", len(Belgium) * '-') # print a line of hyphens the same length as the Belgium string

# Replace the comma (,) separators in the string by colons (:)
print("String with replacement:", Belgium.replace(",", ":"))

# Define the population of Belgium(the first field)
Belgium_population = (Belgium[8:16])
belgium_list = Belgium.split(',')
print(belgium_list[1])
print(belgium_list[3])


# Converting Belgium_population string into an integer in order to add it up
num = int(Belgium_population)
print(num)

# Define the population of the capital city (the fourth field)
Brussels_population = (Belgium[26:32])

# Converting Brussels_population string into an integer in order to add it up
num2 = int(Brussels_population)
print(num2)

# Adding the population of Belgium and Brussels to find the total
print(num + num2)
Empty file added Reanna Daphne.py
Empty file.
Empty file added sample_files/sisy.txt
Empty file.