In [1]:
#a = 2
#b = 1
a, b, c = 2, 3, "Hello World!"
In [2]:
#print a # works in python2 but not python3
print( a )
In [3]:
print ("Hello World!")
In [4]:
print (3*3)
In [5]:
print (3**2)
In [6]:
print (2+2)
You can find more examples at LearnPython & PythonTutorial.
In [7]:
myint = 7
myfloat1 = 7.0; myfloat2 = float(7)
You may separate commands using ";"
In [8]:
print (myint/2); print (myfloat1/2); print (myfloat2*2); print (myfloat2**2)
print myfloat1
In [1]:
mystring1 = 'Hello'
# in R mystring1 <- 'Hello'
mystring2 = "World"
In [2]:
mystring12 = mystring1+" "+mystring2+"!"+"?"+"!"
#add a comment
In [3]:
print (mystring12)
This is a box of comments
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 [4]:
dir()
Out[4]:
In [5]:
del mystring1, mystring2
In [6]:
dir()
Out[6]:
In [7]:
print (mystring12)
In [8]:
print (len(mystring12))
In [9]:
print (mystring12 * 2)
In [11]:
print (3.0 / 2)
print (3 / 2.)
print (3 / 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 [15]:
print (mystring12)
print (mystring12[0])
In [16]:
print (mystring12[1:2])
# 1:n [1:n]
In [32]:
print (mystring12[:])
In [33]:
mylist = [1, 2, 3.]
print (mylist)
In [35]:
print (mylist[0])
Here is a simple example that shows how a list is different with an array.
In [36]:
mylist = [1, 2, 3, "2", "Hello World", [1, 2, 3] ]
In [37]:
print (mylist)
print (mylist[-1])
In [39]:
print (mylist[2]*3)
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 [41]:
firstname= ["Mansoore","Shakib", "Mahdi","Somayeh"]
lastname=["Razmkhah", "Panah", "Alehdaghi","Noshadi"]
In [43]:
print (firstname)
In [44]:
print (lastname)
In [58]:
#The "zip" function pairs several lists of the same size.
firstlast = zip(firstname, lastname)
print (list(firstlast))
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.
In [ ]:
Exercise
Add your new friend:
In [61]:
x = 2
print (x == 2)
print (x < 2)
print (not x < 2)
In [62]:
print( (x == 2)*(x > 1) )
In [73]:
a = 0
if a == 0:
print( "a equals zero" ) # do not use a is equal to zero (bad English)
else:
print( "a is non-zero" ) # do not use a is not equal to zero (bad English)
In [77]:
if x==2:
print( "x equals two" )
else:
print( "x differs" )
In [ ]:
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 [79]:
print( firstname )
if "John" in firstname:
print "John is in the list"
In [82]:
print( range(10) )
print( range(2,6) )
print( range(2,14,4) )
In [83]:
print( range(len(firstname)))
for i in range(len(firstname)):
print( firstname[i] )
In [85]:
for name in firstname:
print( name )
Exercise
Create a new list in the form of firstname_lastname of your 5 friends.
In [87]:
def sq(x):
return x**2
In [88]:
print( sq(75) )
Exercise
Define a function that gives the square of a list elements.
In [89]:
def sqlist1(X):
return [sq(x) for x in X]
In [90]:
sqlist1(range(6))
Out[90]:
In [91]:
def sqlist2(X):
Y=[0]*len(X)
for i in range(len(X)):
Y[i]=sq(X[i])
return Y
In [92]:
sqlist2(range(6))
Out[92]: