In [1]:
a = ["Hello", "World", 42]
Type a., then press tab to see attributes:
In [ ]:
Alternatively, use the dir(a) command to see the attributes (ignore everything starting with __):
In [ ]:
dir(a)
Imagine we want to find out what the append attribute is: use help(a.append) or a.append? to learn more about an attribute:
In [3]:
help(a.append)
Let's try this:
In [4]:
print(a)
In [5]:
a.append("New element")
In [6]:
print(a)
In [7]:
d = 20e-9 # distance in metres
In [8]:
import math
math.sin(0)
Out[8]:
In [9]:
import math as m
m.sin(0)
Out[9]:
In [10]:
def greet(greeting, name):
"""Optional documentation string, inclosed in tripple quotes.
Can extend over mutliple lines."""
print(greeting + " " + name)
In [11]:
greet("Hello", "World")
In [12]:
greet("Bonjour", "tout le monde")
In above examples, the input argument to the function has been identified by the order of the arguments.
In general, we prefer another way of passing the input arguments as
In [13]:
greet(greeting="Hello", name="World")
In [14]:
greet(name="World", greeting="Hello")
Note that the names of the input arguments can be displayed intermittently if you type greet( and then press SHIFT+TAB (the cursor needs to be just to the right of the opening paranthesis).
In [15]:
def say_hello(name): # function
print("Hello " + name)
In [16]:
# main program starts here
names = ["Landau", "Lifshitz", "Gilbert"]
for name in names:
say_hello(name=name)
In [ ]: