Python Intro

Notebooks

What you are reading here is text in a cell in a notebook. A notebook can have many cells. This is a text (really: markdown) cell. It can display text, math, include figures etc. The next cell will contain some python code that you can execute in the browser. You can see this from the In [ ]: prompt.

But before that, let's show off with some math, $c^2 = a^2 + b^2$ and also $$ {{G M(r)} \over {r^2} } = { v^2 \over r} $$ which should look familiar to most of you. We will come back to this.

python variables


In [ ]:
a = 1.0
b = 2
c = "3"
#  let's pause and remember python has dynamic types.
#  float, integer and string are determined automatically from the way we enter it
print(a,b,c)

In order to execute this python code, click on the cell to activate it, then using the SHIFT-ENTER key. Note that as soon as the cell has executed, the In[] (a python list) has obtained a sequence number. More on those later.


In [ ]:
#  another way of output
print("a=",a,"b=",b,"c=",c)

In [ ]:
# yet another way of output
print("a=%g  b=%d  c=%s" % (a,b,c))

In [ ]:
# and yet another way of output
print("a=%s  b=%s  c=%s" % (str(a),str(b),str(c)))

In [ ]:
# find out the types of variables
print(type(a),type(b),type(c))

1st leave heading: Markdown formatting

Making a bullet list

  • a list
  • uses stars
  • for bullets

2nd level heading

Or an enumerated list, but it looks weird in raw format. Double click on this cell!

  1. first
  2. second
  3. third

3rd level heading

And this link is created with [...](...), where you should note the use of the back-ticks.

A little more on python data structures

Next to dynamic typing, one of the powers of python are a number of built-in data structures (lists, dictionaries, tuples and sets) that together with their built-in operators make for a very flexible scripting language. Here is a quick summary how you would initialize them in python:

data type assignment example len(a)
list a = [1,2,3] 3
tuple a = (1,2,3) 3
dictionary a = {'1':1 ,'2':2 , '3':3} 3
set a = {1,2,3,2,1} 3

python list


In [ ]:
# we saw the assignment:
a = [1,2,3]
print(len(a))

# a zero list is ok too
a = []
print(len(a))

# but if you need a lot more, typing becomes tedious, python has a shortcut
a = list(range(10,100,5))
print(a)

# notice something odd with the last number?

In [ ]:
# slicing and copying
b=a[3:6]
b[0]=0
print(b)
print(a)

In [ ]:
a.reverse()
print(a)

Two short notes on python lists:

  • The data types inside of a list don't have to be all the same.
      a = [1.0, 2, "3", [1.0, 2, "3"], range(10,20,2)]
  • Each python object can have a number of member function. In a notebook you can find out about these:

    • google, stackoverflow, e.g. the online manuals https://docs.python.org/2/tutorial/datastructures.html#more-on-lists

    • the inline dir() method in python

      dir(a)

      not very informative, but it does remind you of the names

    • python introspection
      import inspect
      inspect.getmembers(a)
    • Use the ipython or notebook TAB completion: For our object a typing a.<TAB> should show a list of possible completions, move your cursor to the desired one

In [ ]:
a = [1,2,3]
if True:
    print("assigning a slice")
    b=a[:]
else:
    print("simple assignment")
    b=a
b[0] = 0
print("a=",a)
print("b=",b)

Notice in this "if True/else" statement, that indentation is used in python to control the flow. Look at the following C code, which does not do what indentation may indicate:

   if (condition1)
       if (condition2)
           printf("1 and 2");
   else
       printf("when oh when will I print?");

More in https://swcarpentry.github.io/python-second-language/01-basics/


In [ ]: