Python is the programming language of choice for many scientists to a large degree because it offers a great deal of power to analyze and model scientific data with relatively little overhead in terms of learning, installation or development time. It is a language you can pick up in a weekend, and use for the rest of one's life.
Please note, is not meant to be a comprehensive overview of Python or programming in general, if you have no programming experience,I suggest to read Python for You and Me.
In [1]:
2 + 2
Out[1]:
In [2]:
2 * 3
Out[2]:
In [3]:
1 / 2
Out[3]:
In [4]:
7 // 2
Out[4]:
In [5]:
4 ** 3
Out[5]:
In [6]:
23 % 4
Out[6]:
In [7]:
var = 2
var2 = 54
In [8]:
var3 = var2 - var
In [9]:
var3
Out[9]:
In [10]:
'Hello world'
Out[10]:
In [11]:
"Hello world"
Out[11]:
In [12]:
print('Hello world')
In [13]:
name = 'sadegh'
course = 'Machine learning'
In [14]:
print('My name is {} this is a {} course'.format(name,course))
In [15]:
print('My name is {myname} this is a {name_course} course'.format(name_course = course, myname = name))
In [16]:
[1, 2, 3]
Out[16]:
In [17]:
[1 , [3, 4], 'hello :)']
Out[17]:
In [18]:
myList = [1 ,2 ,3, 4, 5, 6]
In [19]:
myList.append(12)
In [20]:
myList
Out[20]:
In [21]:
myList[2]
Out[21]:
In [22]:
myList[1:3]
Out[22]:
In [23]:
myList [1:]
Out[23]:
In [24]:
myList[0:5:2]
Out[24]:
In [25]:
myList[-1]
Out[25]:
In [26]:
myList[0]
Out[26]:
In [27]:
myList[0] = 'wow'
In [28]:
myList
Out[28]:
In [29]:
myList = [[[1, 2 ,3], [4, 5, 6], [7, 8, 9]], [[-1, -2, -3], [-4, -5, -6], [-7, -8 , -9]]]
In [30]:
myList[0][2][2]
Out[30]:
In [20]:
import numpy as np
np.array(myList)
Out[20]:
In [32]:
dictinary = {'key1':'item1','key2':'item2'}
In [33]:
dictinary
Out[33]:
In [34]:
dictinary['key1']
Out[34]:
In [35]:
t = (1, 2, 3)
In [36]:
t[0]
Out[36]:
In [37]:
t[0] = 5
In [ ]:
In [38]:
if 23 < 34:
print('hello')
In [39]:
if 5 < 2:
print('first')
else:
print('second')
In [40]:
if 1 == 2:
print('first')
elif 3 == 3:
print('second')
else:
print('third')
In [41]:
seq = ['hello', 'python', 'good']
In [42]:
for i in seq:
print(i)
In [43]:
seq = [1, 2, 3, 4, 5]
In [44]:
for i in seq:
print (i ** 2)
In [45]:
range(5)
Out[45]:
In [46]:
list(range(5))
Out[46]:
In [47]:
for i in range(2,5):
print(i)
In [48]:
i = 1
while i < 5:
print('number i is: {}'.format(i))
i += 1
In [49]:
x = [1, 2, 3, 4]
In [50]:
output = list()
#output = []
for i in x :
output.append(i + 2)
output
Out[50]:
In [51]:
[i + 2 for i in x]
Out[51]:
In [52]:
def func(param1):
print(param1)
In [53]:
func(param1='hello')
In [54]:
def func2(number):
return number * 2
In [55]:
func2(2)
Out[55]:
In [56]:
seq = [1,2,3,4,5]
In [57]:
map(func2,seq)
Out[57]:
In [58]:
list(map(func2,seq))
Out[58]:
In [59]:
list(map(lambda var: var*2,seq))
Out[59]:
In [60]:
filter(lambda item: item%2 == 0,seq)
Out[60]:
In [61]:
list(filter(lambda item: item%2 == 0,seq))
Out[61]:
In [ ]: