A Crash Course in Python for Scientists

Why Python?

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.

Topic

  • Data types
    • Numbers
    • Strings
    • Printing
    • Lists
    • Dictionaries
    • Booleans
    • Tuples
    • Sets
  • Comparison Operators
  • if,elif, else Statements
  • for Loops
  • while Loops
  • range()
  • list comprehension
  • map and filter
  • functions
  • lambda expressions
  • map and filter

Data types

Numbers and Arithmetics


In [1]:
2 + 2


Out[1]:
4

In [2]:
2 * 3


Out[2]:
6

In [3]:
1 / 2


Out[3]:
0.5

In [4]:
7 // 2


Out[4]:
3

In [5]:
4 ** 3


Out[5]:
64

In [6]:
23 % 4


Out[6]:
3

In [7]:
var = 2 
var2 = 54

In [8]:
var3 = var2 - var

In [9]:
var3


Out[9]:
52

String


In [10]:
'Hello world'


Out[10]:
'Hello world'

In [11]:
"Hello world"


Out[11]:
'Hello world'

Print


In [12]:
print('Hello world')


Hello world

In [13]:
name = 'sadegh'
course = 'Machine learning'

In [14]:
print('My name is {} this is a {} course'.format(name,course))


My name is sadegh this is a Machine learning course

In [15]:
print('My name is {myname} this is a {name_course} course'.format(name_course = course, myname = name))


My name is sadegh this is a Machine learning course

Lists


In [16]:
[1, 2, 3]


Out[16]:
[1, 2, 3]

In [17]:
[1 , [3, 4], 'hello :)']


Out[17]:
[1, [3, 4], 'hello :)']

In [18]:
myList = [1 ,2 ,3, 4, 5, 6]

In [19]:
myList.append(12)

In [20]:
myList


Out[20]:
[1, 2, 3, 4, 5, 6, 12]

In [21]:
myList[2]


Out[21]:
3

In [22]:
myList[1:3]


Out[22]:
[2, 3]

In [23]:
myList [1:]


Out[23]:
[2, 3, 4, 5, 6, 12]

In [24]:
myList[0:5:2]


Out[24]:
[1, 3, 5]

In [25]:
myList[-1]


Out[25]:
12

In [26]:
myList[0]


Out[26]:
1

In [27]:
myList[0] = 'wow'

In [28]:
myList


Out[28]:
['wow', 2, 3, 4, 5, 6, 12]

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]:
9

In [20]:
import numpy as np
np.array(myList)


Out[20]:
array([ 1,  2,  3,  4,  5,  6, 12])

Dictionaries


In [32]:
dictinary = {'key1':'item1','key2':'item2'}

In [33]:
dictinary


Out[33]:
{'key1': 'item1', 'key2': 'item2'}

In [34]:
dictinary['key1']


Out[34]:
'item1'

Tuples


In [35]:
t = (1, 2, 3)

In [36]:
t[0]


Out[36]:
1

In [37]:
t[0] = 5


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-8d3d8637cb4b> in <module>()
----> 1 t[0] = 5

TypeError: 'tuple' object does not support item assignment

Comparison Operators ## Logic Operators

bigger than, les than, equal and ,,,

and, or


In [ ]:

if,elif, else Statements


In [38]:
if 23 < 34:
    print('hello')


hello

In [39]:
if 5 < 2:
    print('first')
else:
    print('second')


second

In [40]:
if 1 == 2:
    print('first')
elif 3 == 3:
    print('second')
else:
    print('third')


second

for Loops


In [41]:
seq = ['hello', 'python', 'good']

In [42]:
for i in seq:
    print(i)


hello
python
good

In [43]:
seq = [1, 2, 3, 4, 5]

In [44]:
for i in seq:
    print (i ** 2)


1
4
9
16
25

range()


In [45]:
range(5)


Out[45]:
range(0, 5)

In [46]:
list(range(5))


Out[46]:
[0, 1, 2, 3, 4]

In [47]:
for i in range(2,5):
    print(i)


2
3
4

while Loops


In [48]:
i = 1
while i < 5:
    print('number i is: {}'.format(i))
    i += 1


number i is: 1
number i is: 2
number i is: 3
number i is: 4

list comprehension


In [49]:
x = [1, 2, 3, 4]

In [50]:
output = list()
#output = []
for i in x :
    output.append(i + 2)
output


Out[50]:
[3, 4, 5, 6]

In [51]:
[i + 2 for i in x]


Out[51]:
[3, 4, 5, 6]

functions


In [52]:
def func(param1):
    print(param1)

In [53]:
func(param1='hello')


hello

In [54]:
def func2(number):
    return number * 2

In [55]:
func2(2)


Out[55]:
4

map and filter


In [56]:
seq = [1,2,3,4,5]

In [57]:
map(func2,seq)


Out[57]:
<map at 0x7fa42815bf60>

In [58]:
list(map(func2,seq))


Out[58]:
[2, 4, 6, 8, 10]

lambda expressions


In [59]:
list(map(lambda var: var*2,seq))


Out[59]:
[2, 4, 6, 8, 10]

In [60]:
filter(lambda item: item%2 == 0,seq)


Out[60]:
<filter at 0x7fa42815bda0>

In [61]:
list(filter(lambda item: item%2 == 0,seq))


Out[61]:
[2, 4]

Other


In [ ]: