In [1]:
print("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?")
In [3]:
h_bar = 1.05457e-34
In [4]:
2plus_forty = 42 # bad
In [5]:
two_plus40 = 42 # good
In [6]:
pi = 3.14159
h = 2 * pi * h_bar
print(h)
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]:
In [9]:
type(42)
Out[9]:
In [10]:
float(42)
Out[10]:
In [11]:
int("28")
Out[11]:
In [12]:
int("quark")
In [13]:
x = 3
x = 1.05457e-34
x = "Energy (in MeV)"
In [14]:
bool(0)
Out[14]:
In [15]:
bool("Do we need Oxygen?")
Out[15]:
In [16]:
"Gorgus" / 2.718
In [17]:
x = 1 # Create x
del x # Destroy x
In [18]:
x = (42 * 65) - 1
In [19]:
42 + 65
Out[19]:
In [20]:
(42 + 65) + 1
Out[20]:
In [21]:
x = "Nature abhors a vacuum"
y = 'but loves a mop!'
In [22]:
p = "proton"
In [23]:
p[1]
Out[23]:
In [24]:
p[-1]
Out[24]:
In [25]:
p[len(p)-2] # also works, but why write len(p) all the time?
Out[25]:
In [26]:
p[2:5]
Out[26]:
In [27]:
p[1:-1]
Out[27]:
In [28]:
p[-1:2]
Out[28]:
In [29]:
q = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
In [30]:
q[2:-2:2]
Out[30]:
In [31]:
q[1::2]
Out[31]:
In [32]:
q[::-3]
Out[32]:
In [33]:
x = "neveroddoreven"
x == x[::-1]
Out[33]:
In [34]:
my_slice = slice(3, 1415, 9) # my slice of the pi
x[my_slice]
Out[34]:
In [35]:
"kilo" + "meter"
Out[35]:
In [36]:
"x^" + str(2)
Out[36]:
In [37]:
"newto" * 10
Out[37]:
In [38]:
"H + H" " -> H2"
Out[38]:
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]:
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]:
In [43]:
header = " temperature pressure\t value \n"
In [44]:
header.strip()
Out[44]:
In [45]:
header.upper()
Out[45]:
In [46]:
"10".isdigit()
Out[46]:
In [47]:
"10.10".isdigit()
Out[47]:
In [48]:
"{0} gets into work & then his {1} begins!".format("Hilbert", "commute")
Out[48]:
In [49]:
x = 42
y = 65.0
In [50]:
"x={0} y={1}".format(x, y)
Out[50]:
In [51]:
"x=" + str(x) + " y=" + str(y)
Out[51]:
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