Input And Output Flashcards

1
Q

Conversion specifiers

A

Tells Python how to render the corresponding value .

Specifiers Meaning
d integer
o Octal (base 8) value
x Lowercase hexadecimal (base 16)
X Uppercase hexadecimal
e Lowercase float hexadecimal
E Uppercase float hexadecimal
F Float
s String
% % character

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

Conversion specifiers example

What does this output?

> > > a, b, c = ‘cat’, 3.14, 6

> > > s = ‘There's %d %ss older than %.2f years ‘ % (c, a, b)

A

> > > s

“There’s 6 cats older than 3.14 years “

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

String formatting

A

format string function

format(value, format_spec)

Use “{ }” curly brackets, anything within them is replaced
“Named replacement “

Replacement by position- use 0, 1 etc

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

A _______ is a named collection of bits stored on a secondary storage device such as a hard disk, USB drive, flash memory stick, eyc.

A

File

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

What are the two categories of files?

A

Text files and binary files

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

Text file characteristics

A

Essentially “strings on disk”
Can be edited with any text editor
Can be difficult for programs to read
Larger than binary files

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

Binary file characteristics

A

Not human readable
Take up less space
Easier for programs to use

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

________ are used to store files

A

Folders or directories

Hierarchical folder structure

A pathname is used to identify a file or folder.

Windows pathnames use a “" backslash

Mac and Linux systems use “/” forward slash

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

cwd

A

current working directory

The one directory that has been designated as the default directory.

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

os.getcwd()

A

File and folder functions

Returns the name of the current working directory

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

os.listdir(p)

A

File and folder functions

Returns a list of strings of the names of all the files and folders in the folder specified by the path p.

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

os.chdir(p)

A

File and folder functions

Sets the current working directory to be path p.

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

os.path.isfile(p)

A

File and folder functions

Returns True just when path p specifies the name of a file, and False otherwise

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

os.path.isdir(p)

A

Returns True just when path p specifies the name of a folder, and False otherwise

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

os.stat(fname)

A

File and folder functions

Returns information about fname, such as its size in bytes and the last modification time

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

String interpolation

A

Approach to string formatting borrowed from C language.

Ex

Number of decimal places

x = 1/81
print(‘value: %.2f % x)
value: 0.01

17
Q

What are Python’s main file modes?

A
'r' = open for reading (default)
'w' = open for writing 
'a' = open for appending to the end of the file 
'b' = binary mode
't' = text mode(default)
'+' = open a file for reading and writing
18
Q

Opening a file

A

f = open(fname, ‘r’). # opens in read mode

For text files can simply use f = open (fname)

19
Q

Closing a file

A

f.close()

20
Q

Reading a file

A

f.read()

21
Q

Writing a file

A

f.write()