In [1]:
print("Hello Sir Newton.")


Hello Sir Newton.

What to write in the terminal:

$ ipython
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- 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]: print("Good day, Madam Curie.")
Good day, Madam Curie.

In [2]:

In [2]:
print("Hey Isaac, what's Newton?!")
print("How is it going, Gottfried?")


Hey Isaac, what's Newton?!
How is it going, Gottfried?

In [3]:
h_bar = 1.05457e-34

In [4]:
2plus_forty = 42  # bad


  File "<ipython-input-4-4ce07ef7122d>", line 1
    2plus_forty = 42  # bad
              ^
SyntaxError: invalid syntax

In [5]:
two_plus40 = 42   # good

In [6]:
pi = 3.14159
h = 2 * pi * h_bar
print(h)


6.6260531326e-34

In [7]:
dims = 3                   # int, only digits
ndim = 3.0                 # float, because of the '.'
h_bar = 1.05457e-34        # float, because of the '.' or 'e'
label = "Energy (in MeV)"  # str, quotes surround the text

In [8]:
type(h_bar)


Out[8]:
float

In [9]:
type(42)


Out[9]:
int

In [10]:
float(42)


Out[10]:
42.0

In [11]:
int("28")


Out[11]:
28

In [12]:
int("quark")


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-df7f23f9b45e> in <module>()
----> 1 int("quark")

ValueError: invalid literal for int() with base 10: 'quark'

In [13]:
x = 3
x = 1.05457e-34
x = "Energy (in MeV)"

In [14]:
bool(0)


Out[14]:
False

In [15]:
bool("Do we need Oxygen?")


Out[15]:
True

In [16]:
"Gorgus" / 2.718


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-8cdca6dc67bb> in <module>()
----> 1 "Gorgus" / 2.718

TypeError: unsupported operand type(s) for /: 'str' and 'float'

In [17]:
x = 1  # Create x
del x  # Destroy x

In [18]:
x = (42 * 65) - 1

In [19]:
42 + 65


Out[19]:
107

In [20]:
(42 + 65) + 1


Out[20]:
108

In [21]:
x = "Nature abhors a vacuum"
y = 'but loves a mop!'

In [22]:
p = "proton"

In [23]:
p[1]


Out[23]:
'r'

In [24]:
p[-1]


Out[24]:
'n'

In [25]:
p[len(p)-2]  # also works, but why write len(p) all the time?


Out[25]:
'o'

In [26]:
p[2:5]


Out[26]:
'oto'

In [27]:
p[1:-1]


Out[27]:
'roto'

In [28]:
p[-1:2]


Out[28]:
''

In [29]:
q = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"

In [30]:
q[2:-2:2]


Out[30]:
'BCDEFGHIJKLMNOPQRSTUVWXY'

In [31]:
q[1::2]


Out[31]:
'abcdefghijklmnopqrstuvwxyz'

In [32]:
q[::-3]


Out[32]:
'zYwVtSqPnMkJhGeDbA'

In [33]:
x = "neveroddoreven"
x == x[::-1]


Out[33]:
True

In [34]:
my_slice = slice(3, 1415, 9)  # my slice of the pi
x[my_slice]


Out[34]:
'ee'

In [35]:
"kilo" + "meter"


Out[35]:
'kilometer'

In [36]:
"x^" + str(2)


Out[36]:
'x^2'

In [37]:
"newto" * 10


Out[37]:
'newtonewtonewtonewtonewtonewtonewtonewtonewtonewto'

In [38]:
"H + H"    " -> H2"


Out[38]:
'H + H -> H2'

In [39]:
quote = ("Science is what we understand well enough to explain to a computer. "
         "Art is everything else we do. "
         "-Donald Knuth")

In [40]:
x = "It's easy!"
y = 'The computer said, "Does not compute."'

In [41]:
"Bones said, \"He\'s dead, Jim.\""


Out[41]:
'Bones said, "He\'s dead, Jim."'

In [42]:
"""Humpty, he sat on a wall,
Then Humpty, he had a great fall.
But all the king's horses
And men with their forces
Couldn't render his entropy small.
"""


Out[42]:
"Humpty, he sat on a wall,\nThen Humpty, he had a great fall.\nBut all the king's horses\nAnd men with their forces\nCouldn't render his entropy small.\n"

In [43]:
header = "  temperature  pressure\t value \n"

In [44]:
header.strip()


Out[44]:
'temperature  pressure\t value'

In [45]:
header.upper()


Out[45]:
'  TEMPERATURE  PRESSURE\t VALUE \n'

In [46]:
"10".isdigit()


Out[46]:
True

In [47]:
"10.10".isdigit()


Out[47]:
False

In [48]:
"{0} gets into work & then his {1} begins!".format("Hilbert", "commute")


Out[48]:
'Hilbert gets into work & then his commute begins!'

In [49]:
x = 42
y = 65.0

In [50]:
"x={0} y={1}".format(x, y)


Out[50]:
'x=42 y=65.0'

In [51]:
"x=" + str(x) + " y=" + str(y)


Out[51]:
'x=42 y=65.0'

In [52]:
import constants

two_pi = 2 * constants.pi
h_bar = constants.h / two_pi

In [53]:
from constants import pi, h

two_pi = 2 * pi
h_bar = h / two_pi

In [54]:
import constants as c

constants = 2.71828

two_pi = 2 * c.pi
h_bar = c.h / 2 / c.pi

In [55]:
from constants import pi as PI, h as H

two_pi = 2 * PI
h_bar = H / two_pi