Python Flashcards

1
Q

read()

A

The read() method returns the specified number of bytes from the file. Default is -1 which means the whole file.

f = open(“demofile.txt”, “r”)
print(f.read())

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

close()

A

The close() method closes an open file.

You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

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

readline()

A

The readline() method returns one line from the file.

You can also specified how many bytes from the line to return, by using the size parameter.

The number of bytes from the line to return. Default -1, which means the whole line.

f = open(“demofile.txt”, “r”)
print(f.readline())

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

truncate

A

empties the file

The truncate() method resizes the file to the given number of bytes.

If the size is not specified, the current position will be used.

f = open(“demofile2.txt”, “a”)

f. truncate(20)
f. close()

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

write(‘stuff’)

A

writes “stuff” to the file

The write() method writes a specified text to the file.

Where the specified text will be inserted depends on the file mode and stream position.

“a”: The text will be inserted at the current file stream position, default at the end of the file.

“w”: The file will be emptied before the text will be inserted at the current file stream position, default 0

f = open(“demofile2.txt”, “a”)

f. write(“See you soon!”)
f. close()

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

seek(0)

A

moves the read/write location to the beginning of the file

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

print

A

The print() function prints the specified message to the screen, or other standard output device.

The message can be a string, or any other object, the object will be converted into a string before written to the screen.

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

(

A

opening parenthesis

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

)

A

closing parenthesis

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

#

A

Use ‘hashtag’ or ‘pound’ for commenting

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

+

A

plus sign. addition

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

/

A

slash. division

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

>

A

grater than

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

less than

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

A

single quote

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

*

A

asterisk. multiplication

17
Q

A

double quote. defines beginning and end of strings

18
Q

’’’

A

triple simple quote. for opening multiple lines of text

19
Q

{}

A

curly brackets. variables inside strings

20
Q

[]

A

square brackets. for building lists

21
Q

format()

A

passes arguments filling placeholders marked as {}

22
Q

\n

A

new line

23
Q

\t

A

tab (indents in string)

24
Q

import

A

imports a module

25
Q

argv

A

module that requests an argument to function

26
Q

int()

A

converts string into an integer

27
Q

float()

A

converts a string into a number with decimal

28
Q

input()

A

request input from user

29
Q

open()

A

The open() function opens a file, and returns it as a file object.

open(“demofile.txt”, “r”)

30
Q

truncate()

A

The truncate() method resizes the file to the given number of bytes.

If the size is not specified, the current position will be used.

file.truncate(size)

31
Q

def

A

defines a function. this is how every function should begin/

def name_of_function(argument1, argument2):

32
Q

readline()

A

The readline() method returns one line from the file.

You can also specified how many bytes from the line to return, by using the size parameter.

file.readline(size)