Shared Flashcard Set

Details

Week 6
programming principles
12
Computer Science
Post-Graduate
11/09/2017

Additional Computer Science Flashcards

 


 

Cards

Term
How are files used in various programs/software?
Definition
- to retain data
- receive input (read data)
- store output (write data)
Term
Two main types of file
Definition
Text: simply contain text in ASCII or Unicode and can be opened by programs like notepad

Binary: not text-based and designed to only be opened by a program that can use data in that format (images, audio files etc)
Term
List and describe the common modes that can be specified when opening/creating files.
Definition
Read ('r') - allows reading only (file must exist)

write ('w') - allows writing only (creates or overwrites file)

append ('a') - writes to the end of the file

read/write ('rw') - allows reading and writing
Term
3 languages for write mode
Definition
f = open ('example.text', 'w') - python
$f = fopen ('example.text', 'w') = PHP
f = file.open ('example.text', 'w') = Ruby
Term
Why is it important to close a file
Definition
Once you have finished processing the file, closing it severs its connections w the program and releases the resources
Term
Describe the different ways that you can read data from a file.
Definition
- read the entire file at once - gives you a variable containing the whole file.

- use read() method to specify how many bytes or characters to read.
readline() gives the whole line (work through bit by bit)

read() always returns a string - may need to convert to int or float depending on the data
Term
Exception
Definition
an error or condition that occurs while a program is running, causing it to crash or end. Different to syntax error which occur when the code is not valid and wont run at all
Term
Exception handling
Definition
another control structure that anticipates the potential errors and handles them by redirecting the program
Term
Exception handler that terminates the program
Definition
try:
num = (input('Type a number:'))
except ValueError:
print ('Invalid input')
break
print ('You typed:', num)
Term
Exception handler that continues the program
Definition
try:
num = (input('Type a number:'))
except ValueError: #default 7
num = 7
print ('You typed:' num)
Term
NameError

TypeError

ValueError

IndexError
Definition
- raised when you refer to a variable or function that doesn't exist (typo)

- raised when you use wrong data type eg round('string')

- use right data type but the value is not appropriate (outside set range or minus number for age)

- raised when you refer to an index that doesn't exist (eg index[5] in a list of 3 items
Term
Else in exception handling


Finally in exceoption handling
Definition
Else - run if no exceptions occur

Finally - will always run either after the try block if no exceptions or after the exception handler if they do
Supporting users have an ad free experience!