Python input and output (basics)
Tags: python, beginners
Published: 2020-03-07 19:48:40

The <a href="https://python.org">Python</a> 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 (<a href="https://pythonprogramminglanguage.com/repl/">repl</a>) 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.

```python
>>> 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 <a href="https://pythonbasics.org/for-loops/">for loops</a> 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. 

```python
>>> 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 <a href="https://gitpress.io/u/1155/lesson4-ForLoops">interactive</a>, by calling the `input()` method in each cycle or before the cycles start.

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

```python
>>> '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*).

```python
>>> 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 <a href="https://pythonbasics.org/dictionary/">dictionary</a>, you can output it like a table:

```python
>>> 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:

```python
>>> 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. 

```python
>>> 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.:

```python
>>> f.readline()
```

To write a line into a file

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

If you want to write an integer to a file:

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