Data processing with Python

Alexander Sapozhnikov,

Data processing with Python

Python

Part 1

Python

Why Python?

How to run standalone Python interpreter

Which version should we use?

Let's try to launch Python in terminal

$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Oops! Wrong version here

$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

There's a prompt

$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

How to exit?

>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit

How to exit?

Let's try again

$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Type something

>>> help
Type help() for interactive help, or help(object) for help about object.

Get help

>>> help()
Welcome to Python 3.7's help utility!
 
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.
 
Enter the name of any module, keyword, or topic to get help on writing

Return from help() — press Ctrl+D

help>
You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
>>>

Type more

>>> 2*2
4

Type more

>>> 2*2
4
>>> 2*4+(3/5)**2
8.36

REPL

Read–eval–print loop

Get help

>>> help(*)
  File "", line 1
    help(*)
          ^
SyntaxError: invalid syntax

help() expects string

String must be enclosed with quotation marks

'string'
"string"

Get help again

>>> help('*')
Operator precedence
*******************
 
The following table summarizes the operator precedence in Python, from
[ skipped ]
:

Some program

>>> counter = 42
>>> print("I've already wrote", counter, 'programs')
I've already wrote 42 programs

Save our program to file

Now we can execute it

$ python3 program-counter.py
I've already wrote 42 programs

Use .py extension

$ python3 program-counter.py
I've already wrote 42 programs

Alternative way

Search for Python interpreter

$ which python3
/usr/bin/python3

Alternative way

Add shebang line as first line of your file

#!/usr/bin/python3
counter = 42
print("I've already wrote", counter, 'programs')

Alternative way

Make your program executable

$ chmod +x program-counter.py

Now you can run it

$ ./program-counter.py
I've already wrote 42 programs

Editors and IDE

Interactive programming environment

repl.it

Basic syntax

Error messages

Libraries overview

Further reading

Alexander Sapozhnikov

https://as.susu.ru