DEV Community

Cover image for Python input and output (basics)
Python64
Python64

Posted on

Python input and output (basics)

The Python input and output functions let you grab keyboard input, output to the screen, output to the files and more.

If you just start Python, it will open the interpreter (repl) and there you can type instructions.

In this article we cover some basics, you may or may not know.

String formatting

These are some basic things you can do in the python shell. From string output to calculations.

>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...

You can also use for loops in here, this repeats one or more lines of code. After a for loop, you must use four spaces as a way to show the begining and end of the block to repeat.

>>> for x in range(1, 11):
...     print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

For loops can be interactive, by calling the input() method in each cycle or before the cycles start.

You can use zfill(n) to define the number of digits:

>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'

The format() function lets you add variables into your string (you can also use f-strings).

>>> print('Nevertheless {} coded in "{}!"'.format('Alice', 'Python'))
Nevertheless Alice coded in Python!

Dictionary

An interesting data structure supports in Python is the dictionary. This is a hashmap in computer science. Each key corresponds to a value. This is similar to a phonebook, which does a a map of name to number.

If you have a dictionary, you can output it like a table:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
...     print('{0:10} ==> {1:10d}'.format(name, phone))
...
Jack       ==>       4098
Dcab       ==>       7678
Sjoerd     ==>       4127

For a more simple output you can use:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

Files

Did you know you work with real computer files in Python?
To open a file, you can use the functions open() and to read read().

A file can open for writing 'w', reading 'r', 'w+' writing and creating and more.

>>> f = open('/tmp/workfile', 'w')
>>> f.read()
'This is the entire file.\n'

You can also read a single line, instead of the whole file. In order for this to work, the file must already be opened.:

>>> f.readline()

To write a line into a file

>>> f.write('This is a test\n')

If you want to write an integer to a file:

>>> value = ('the answer', 42)
>>> s = str(value)
>>> f.write(s)

Top comments (2)

Collapse
 
delta456 profile image
Swastik Baranwal

Nice tutorial but probably you should also to close the file.

Collapse
 
days_64 profile image
Python64

Yes you're right