Scientific Python 2025-01-28
Scientific Python 2025-01-28
We are going to use figures, examples from these sites and books along
the entire slideshow.
Computer room policy
● In the computer room can only stay students, who are
subscribed to the course
● Eating, drinking is prohibited
● Everyone must use his/her own ID to log-in to the
computers
● In case of malicious damage (hardware / software),
disciplinary proceedings may be instituted
Software environment
● If you want or using Linux on the lessons (despite Python
is a cross-platform language)
● Python version 3 will be covered
● For text editor you can use gedit or vim
● For interactive interpreter we’ll be using iPython
The Zen of Python
● Grab a shell, start an interactive Python
interpreter and enter:
import this
The Zen of Python
Beautiful is better than ugly.
- Simple extension syntax
- Consistent syntax and behaviour
Explicit is better than implicit.
- No “hidden” loop variables
- Use “self” to refer to object inside a method
Simple is better than complex.
- Rely on garbage collection
- Test statements with the interactive interpreter
The Zen of Python
Complex is better than complicated.
- Use the right packages to solve a problem,
without making your code complicated
Flat is better than nested.
- Use modules from the standard library
- Namespaces are flat
- No need to use long module names
Sparse is better than dense.
- Standard lib’s are kept small, use pip to install
the rest.
The Zen of Python
Readability counts.
- Whitespace for block structures
- Minimal punctuation
Special cases aren't special enough to break the
rules.
- Everything is an object
- Methods and functions differ by scope
- Keep most features in external modules
The Zen of Python
Although practicality beats purity.
- Multiple programming models (OOP, Procedural,
Functional)
Errors should never pass silently.
- Exception based error handling
- Tracebacks aid debugging
Unless explicitly silenced.
- Catch exceptions with try:except
- Process / Convert / Ignore
The Zen of Python
In the face of ambiguity, refuse the temptation to
guess.
- Coarse types only when not surprising
- Can not add a String to a number, but you can
multiply a string
There should be one-- and preferably only one --
obvious way to do it.
- Eliminate redundacy
- Easier to learn and remember
The Zen of Python
Although that way may not be obvious at first
unless you're Dutch.
- No comment...
Now is better than never.
- Python2 vs Python3
Although never is often better than *right* now.
- Language moratorium
- Not everything goes into stdlib anymore
The Zen of Python
If the implementation is hard to explain, it's a bad
idea.
http://www.slideshare.net/doughellmann/an-introduction-to-the-zen-of-python
Why do people use
Python?
● Roughly 1 million Python users worldwide
● Main benefits
○ Software quality
○ Developer productivity
○ Program portability
○ Support libraries
○ Component integration
Technical strengths
● Object-Oriented and Functional
● Free
● Portable
● Powerful
● Mixable
● Relatively Easy to Use
● Relatively Easy to Learn
Byte code
● The soure code is first compiled to byte code
(.py -> .pyc)
● The byte code is a lower-level, platform-
independent representation of your source
code
● Byte code is saved in files only for files that
are imported
The Python Virtual
Machine (PVM)
● PVM is the runtime engine of Python
● Iterates through your byte code instructions
Setting up the
environment (Windows)
● https://www.python.org/downloads
● Download latest stable installer from version
3.x.x
● While installing select checkbox “Add Python
to environment variables”
● Make sure install of pip is also selected
● After installing, logout, then login
● Run Command Prompt as Administrator
● Type “python” and hit enter, you should see
the Python3 console
Setting up the
environment (Windows)
● Now, quit from console by calling: quit()
● Install iPython by typing to Command prompt:
pip install ipython
● You may need to run the install command
twice (?)
● Type to Command prompt: ipython
to start iPython console
Setting up the
environment (Debian)
● Make sure Python3 is installed
● Install package “python3-pip”
● Start a Terminal and type “python3”,
you should see the Python3 console
● Exit from the console: Ctrl+D
● To install iPython type:
“sudo pip3 install ipython”
● Type “ipython” to start the interpreter
Setting up the
environment (OSX)
● Make sure Homebrew is installed (as root):
$ ruby -e "$(curl -fsSL
https://raw.githubusercontent.com
/Homebrew/install/master/install)
"
● Install package “python3”
$ brew install python3
● To install iPython type:
$ pip3 install ipython
● Type “ipython” to start the interpreter
Setting up the
environment (OSX)
● Make sure Homebrew is installed (as root):
$ ruby -e "$(curl -fsSL
https://raw.githubusercontent.com
/Homebrew/install/master/install)
"
● Install package “python3”
$ brew install python3
● To install iPython type:
$ pip3 install ipython
● Type “ipython” to start the interpreter
Hello World (in terminal)
● Start iPython and type:
print(‘Hello World!’)
● Run:
$ python hello.py