diff --git a/1_collections.py b/1_collections.py index 8f9dffd..edc68e4 100644 --- a/1_collections.py +++ b/1_collections.py @@ -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)) diff --git a/4b_practice.py b/4b_practice.py new file mode 100644 index 0000000..e69de29 diff --git a/Daphs_practice_read_write.py b/Daphs_practice_read_write.py new file mode 100644 index 0000000..af5ae09 --- /dev/null +++ b/Daphs_practice_read_write.py @@ -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 + diff --git a/Exercise11.py b/Exercise11.py new file mode 100644 index 0000000..2dddd9f --- /dev/null +++ b/Exercise11.py @@ -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) \ No newline at end of file diff --git a/Reanna Daphne.py b/Reanna Daphne.py new file mode 100644 index 0000000..e69de29 diff --git a/sample_files/sisy.txt b/sample_files/sisy.txt new file mode 100644 index 0000000..e69de29