We'll start with the first option.
Start it by typing python
on the command line:
$ python
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Start it by typing ipython
on the command line:
$ ipython
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
From now on, play along in your own IPython interpreter.
17
17
(17 + 4) * 2
42
3.2 * 18 - 2.1
55.5
36. / 5
7.2
Scientific notation:
1.3e20 + 2
1.3e+20
1.3 * 10**20
1.3e+20
35 / 5
7
Division is a bit weird: if you give it integer arguments, the result will also be an integer.
36 / 5
7
35 / 5
7
36 / 5
7
Give floating point arguments instead of integer arguments.
36. / 5.
7.2
35 / 5
7
36 / 5
7
From Python 3 onwards, division behaves differently. You can actually get that behaviour in Python 2.7:
from __future__ import division
36 / 5
7.2
a = 1.3e20
b = 2
a
1.3e+20
c = a + 1.5e19 * b
c * 2
3.2e+20
Every value has a type, view it using type
:
type(27)
int
type(3.0 * 2.7)
float
type(a)
float
Another example of a builtin datatype is str
, we'll see more later:
type('I am a homo sapiens')
str
Some operations are defined on more than one type, possibly with different meanings.
'beer' * 5 + 'whiskey'
'beerbeerbeerbeerbeerwhiskey'
Dynamic typing means that variables can be assigned values of different types during runtime.
a
1.3e+20
type(a)
float
a = 'spezi'
type(a)
str
Python is strongly typed, meaning that operations on values with incompatible types are forbidden.
'beer' + 34
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-24-ec918fbfdf41> in <module>() ----> 1 'beer' + 34 TypeError: cannot concatenate 'str' and 'int' objects
from IPython.display import HTML
def css_styling():
styles = open('../styles/custom.css', 'r').read()
return HTML('<style>' + styles + '</style>')
css_styling()