Python
A brief introductory guide
A few things about Python
• Python is a widely used general-purpose, highlevel programming language.
• Python’s main design philosophy:
– Highly readable and simple code
– As few lines of code as possible
– Dynamic type system (explain later)

• Python is used by:
– Google
– NASA
– New York Stock Exchange
Starting up
“Bare” Python is too general for scientific application, so we’re
going to use the distributives with additional modules for
graphing, statistical analysis, matrices etc.
• (OPTION 1) Download Anaconda distributive:
https://store.continuum.io/cshop/anaconda/
• (OPTION 2) Download Python(x, y) distributive:
http://code.google.com/p/pythonxy/

Install (available on Mac, Windows and Linux platforms)
Loading the IDE
IDE is a platform that lets you write code, execute programs and
test them. Both Anaconda and Python(x, y) packages have the
Spyder IDE already installed.
Search for the Spyder in the programs list, run the application.
Alternative: write code in ANY text editor, save it with a “.py”
extension, and run via the console.
Run scripts

Write code

See output

Spyder IDE
Basics: variables
a = 4 # Integer
b = 5.6 # Float (Dynamic type)
c = "hello" # String
a = "4" # rebound to String
Output variables into the console by writing:
print variable_name
Basics: operators
a=1
b=2
print
print
print
print
print
print

a+b #3
a – b # -1
a*b #2
a / b # 0 (careful with that!)
1.0 / 2 # 0.5
b ** 2 # 4 (** is a power operator)
Basics: strings
You can access substrings of a string:
string = ‘Hello World’
print string[0] # H
print string [6:] # World
You also can concatenate two strings:
string2 = ‘ Omega’
pring string + string2 # Hello World Omega
To concatenate strings and integers, use str() function
Lists
A list is a collection of values, stored in one variable.
Some number is assigned for each item in the list.
list1 = *1, 5, 12, ‘Car’+
print list1[2] # 12 (start of numeration from 0)
print len(list1) # 4 (number of elements)
You can append, delete and replace items in the list:
list1.append(10) # *1, 5, 12, ‘Car’, 10]
list1.insert(0, 2) # [2, 1, 5, 12, ‘Car’, 10+
list1.remove(‘Car’) # [2, 1, 5, 12, 10]
Dictionaries
Similar to list, but instead of numeric values, the
identifier of each element is a string:
dic = ,‘Bob’:100, ‘Jake’: 350print dic*‘Jake’+ # 350
dic *‘Henry’+ = 1000
With dictionaries, you can easily check if the specific
value is inside:
print ‘Jake’ in dic # True
Conditional statements
If-else-elif are used to check a condition and do
different stuff depending on the outcome:
dic = ,‘Bob’:100, ‘Jake’: 350if ‘Jake’ in dic:
print ‘Jake is in there’
elif ‘Bob’ in dic:
print ‘Bob is in there’ # won’t execute (why?)
else:
pass # do nothing
Very important: you have to do the indentations!
For/in Loops
Let’s rewrite the previous example:
dic = ,‘Bob’:100, ‘Jake’: 350for item in dic:
print item + ' is in there‘ # item is an index now
print item + ‘ has $’ + str(dic[item])
Bob is in there
Bob has $100
Jake is in there
Jake has $350
While loops
The logic is almost the same:
list = range(1, 101) # generate integers from 1 to 100
count = 0;
while count < len(list):
print list[count]
count += 1 # increment by 1 each time
1
2
…
File input/output
Open a file to read from it:
fin = open(‘foo.txt’)
for line in fin:
print line # manipulate each line
fin.close() # important to do after all manipulations

Write into a file:
fout = open(‘foo.txt’, ‘w’) # set the ‘write’ mode
fout.write(‘hello world’)
fout.close()
Further info
Codeacademy website:
http://www.codecademy.com/ru/tracks/python
Python official documentation:
http://docs.python.org/2/tutorial/index.html

Intro to Python

  • 1.
  • 2.
    A few thingsabout Python • Python is a widely used general-purpose, highlevel programming language. • Python’s main design philosophy: – Highly readable and simple code – As few lines of code as possible – Dynamic type system (explain later) • Python is used by: – Google – NASA – New York Stock Exchange
  • 3.
    Starting up “Bare” Pythonis too general for scientific application, so we’re going to use the distributives with additional modules for graphing, statistical analysis, matrices etc. • (OPTION 1) Download Anaconda distributive: https://store.continuum.io/cshop/anaconda/ • (OPTION 2) Download Python(x, y) distributive: http://code.google.com/p/pythonxy/ Install (available on Mac, Windows and Linux platforms)
  • 4.
    Loading the IDE IDEis a platform that lets you write code, execute programs and test them. Both Anaconda and Python(x, y) packages have the Spyder IDE already installed. Search for the Spyder in the programs list, run the application. Alternative: write code in ANY text editor, save it with a “.py” extension, and run via the console.
  • 5.
    Run scripts Write code Seeoutput Spyder IDE
  • 6.
    Basics: variables a =4 # Integer b = 5.6 # Float (Dynamic type) c = "hello" # String a = "4" # rebound to String Output variables into the console by writing: print variable_name
  • 7.
    Basics: operators a=1 b=2 print print print print print print a+b #3 a– b # -1 a*b #2 a / b # 0 (careful with that!) 1.0 / 2 # 0.5 b ** 2 # 4 (** is a power operator)
  • 8.
    Basics: strings You canaccess substrings of a string: string = ‘Hello World’ print string[0] # H print string [6:] # World You also can concatenate two strings: string2 = ‘ Omega’ pring string + string2 # Hello World Omega To concatenate strings and integers, use str() function
  • 9.
    Lists A list isa collection of values, stored in one variable. Some number is assigned for each item in the list. list1 = *1, 5, 12, ‘Car’+ print list1[2] # 12 (start of numeration from 0) print len(list1) # 4 (number of elements) You can append, delete and replace items in the list: list1.append(10) # *1, 5, 12, ‘Car’, 10] list1.insert(0, 2) # [2, 1, 5, 12, ‘Car’, 10+ list1.remove(‘Car’) # [2, 1, 5, 12, 10]
  • 10.
    Dictionaries Similar to list,but instead of numeric values, the identifier of each element is a string: dic = ,‘Bob’:100, ‘Jake’: 350print dic*‘Jake’+ # 350 dic *‘Henry’+ = 1000 With dictionaries, you can easily check if the specific value is inside: print ‘Jake’ in dic # True
  • 11.
    Conditional statements If-else-elif areused to check a condition and do different stuff depending on the outcome: dic = ,‘Bob’:100, ‘Jake’: 350if ‘Jake’ in dic: print ‘Jake is in there’ elif ‘Bob’ in dic: print ‘Bob is in there’ # won’t execute (why?) else: pass # do nothing Very important: you have to do the indentations!
  • 12.
    For/in Loops Let’s rewritethe previous example: dic = ,‘Bob’:100, ‘Jake’: 350for item in dic: print item + ' is in there‘ # item is an index now print item + ‘ has $’ + str(dic[item]) Bob is in there Bob has $100 Jake is in there Jake has $350
  • 13.
    While loops The logicis almost the same: list = range(1, 101) # generate integers from 1 to 100 count = 0; while count < len(list): print list[count] count += 1 # increment by 1 each time 1 2 …
  • 14.
    File input/output Open afile to read from it: fin = open(‘foo.txt’) for line in fin: print line # manipulate each line fin.close() # important to do after all manipulations Write into a file: fout = open(‘foo.txt’, ‘w’) # set the ‘write’ mode fout.write(‘hello world’) fout.close()
  • 15.
    Further info Codeacademy website: http://www.codecademy.com/ru/tracks/python Pythonofficial documentation: http://docs.python.org/2/tutorial/index.html