Python For GCSE
Section G Exercises
Remember to test your text files after each task to makes sure that you have the text you expected.
Task 1
Write a program that creates a text file called task1.txt which contains only the text Hello World.
Task 2
Write a program that creates a text file called task2.txt which contains the phrases Hello World and Goodbye World, written on separate lines.
Task 3
Write a program that opens your task2.txt file in append mode. Write some text to the end of the file.
Task 4
Copy the following.
h = "Hello" w = "World" g = "Goodbye"
Write a program that creates a file called task4.txt, containing the same content as the one you made for task 2, this time by using the contents of the variables.
Task 5
Study the following,
a = open("a.txt","w") for i in range(1,13): a.write(str(i) + "x" + " some other stuff" + "\n") a.close()
It contains enough in the way of hints to allow you to create a program that outputs the 2 times table to a text file called task5.txt.
Task 6
Use your experience in the previous task to output all of the times tables (2s to 12s) into a single text file called task6.txt.
Task 7
names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin", "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]
Write a program that outputs these names, one per line, to a text file called task7.txt.
Task 8
The chr() function allows you to output a character from an ASCII code. The ASCII code for an A is 65. The rest of the alphabet follows. Write a program that outputs the alphabet to a text file called task8.txt, one letter per line.