In [2]:
a, b, c = 2, 3, "Hello World!"
In [3]:
print a, b, c
In [4]:
print "Hello World!"
In [5]:
print 3*3
In [6]:
print 3**2
In [7]:
print 2+2
You can find more examples at LearnPython & PythonTutorial.
In [8]:
myint = 7
myfloat1 = 7.0; myfloat2 = float(7)
You may separate commands using ";"
In [9]:
print myint/2; print myfloat1/2; print myfloat2*2; print myfloat2**2
In [10]:
print myfloat1
In [11]:
mystring1 = 'Hello'
mystring2 = "World"
mystring12 = mystring1+" "+mystring2+"!"
You may define strings differently, and attach them using "+". You can remove a variable from the memory when you do not need, using "del"
In [12]:
del mystring1, mystring2;
In [13]:
dir()
Out[13]:
In [14]:
print mystring12
In [15]:
print len(mystring12)
In [16]:
print mystring12 * 2
Index in list starts from zero (0) unlike R. A string is actually a list. -1 shows the end of the list but the last. Range of indices is like [start, end[ so you do not get access to the last element.
In [17]:
print mystring12[-1:]
In [18]:
print mystring12[0:-1]
In [19]:
print mystring12[:]
In [20]:
mylist = [1,2,3]
print mylist
In [21]:
print mylist[0]
Here is a simple example that shows how a list is different with an array.
In [1]:
mylist=[1,2,3,"2", "HelloWorld", [1,2,3] ]
In [5]:
print mylist
print mylist[-1]
In [24]:
print mylist[-1]*3
The "zip" function pairs several lists of the same size.
Exercise
Make two lists. First, a list of 4 of your best friends' first name.
Second, a list of their family name. Then print their first name and last name.
In [25]:
firstname= ["John","Bruno", "Alan","Ali"]
lastname=["Smith", "Agard", "Turing","Bendavid"]
In [26]:
print firstname
In [27]:
print lastname
In [28]:
print zip(firstname,lastname)
You may insert several other elements to the list using "append", "insert", "index" and several others. See below for for more details
list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].
list.extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
list.index(x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
list.count(x)
Return the number of times x appears in the list.
list.sort(cmp=None, key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
list.reverse()
Reverse the elements of the list, in place.
Exercise
Add your new friend:
In [29]:
firstname.insert(2,"Martin")
print firstname
In [30]:
lastname.insert(2,"Trepanier")
print lastname
In [31]:
x = 2
print x == 2
print x<2
print not x<2
In [32]:
if x==2:
print "x equals to two"
else:
print "print x differs from two"
In [33]:
#a=1
#if a==1:
#print "ye ye a equals to one"
#else:
#print "nop a differs from one"
Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.
In [34]:
print firstname
if "John" in firstname:
print "John is in the list"
In [35]:
print range(len(firstname))
for i in range(len(firstname)):
print firstname[i]
In [36]:
for name in firstname:
print name
Exercise
Create a new list in the form of firstname_lastname of your 5 friends.
In [37]:
first_last=[""]*len(firstname)
print first_last
for i in range(len(firstname)):
first_last[i]=firstname[i]+"_"+lastname[i]
print first_last
In [38]:
for name, family in zip (firstname, lastname):
print name+"_"+family
The "range" function is a very useful command in loops.
In [39]:
print range(6)
In [40]:
print range(2,6)
In [41]:
print range(2,14,4)
In [42]:
def sq(x):
return x**2
In [43]:
print sq(4)
Exercise
Define a function that gives the square of a list elements.
In [44]:
def sqlist1(X):
return [sq(x) for x in X]
In [45]:
sqlist1(range(6))
Out[45]:
In [46]:
def sqlist2(X):
Y=[0]*len(X)
for i in range(len(X)):
Y[i]=sq(X[i])
return Y
In [47]:
sqlist2(range(6))
Out[47]: