Python Introduction for Programmers

Python Introduction for Programmers

Python Introduction for Programmers Edwin van der Helm Python Scripts Python Introduction for Programmers Variables Math and Numbers Strings and Lists Indentation Logic Edwin van der Helm Functions Parameters Sterrewacht Leiden Advanced tricks Classes Januari 21, 2014 Packages Numpy Astropy PyRAF Pyplot Amuse Help Python Introduction for Programmers Edwin van der Helm Python Scripts Variables Math and Numbers Strings and strw.leidenuniv.nl/∼vdhelm/teaching.php Lists Indentation Logic Functions Parameters Advanced tricks Classes Packages Numpy Astropy PyRAF Pyplot Amuse Help Python Introduction for Programmers Edwin van der Helm Python Scripts Variables Math and Numbers Strings and Lists Indentation Logic Functions Parameters Advanced tricks Classes Packages Numpy Astropy PyRAF Pyplot Amuse Help Python Introduction Scripts for Programmers Edwin van der Helm Python 1 print"Hello World" Scripts Variables Math and Numbers Strings and Lists Indentation Logic $ python Functions >>> print"Hello World" Parameters Advanced tricks Hello World Classes Packages $ python hello_world.py Numpy Hello World Astropy PyRAF Pyplot Amuse Help Python Introduction Variables for Programmers Edwin van der Helm Python 1 x = 2 Scripts 2 y = 2 + 2 Variables 3 z = x * 9 Math and Numbers 4 Strings and Lists 5 x = y = z = 0 Indentation Logic 6 Functions 7 x, y = 3, 4 Parameters Advanced tricks 8 y, x = x, y Classes 9 Packages 10 x = str (x) Numpy 11 Astropy PyRAF 12 print"y:", y Pyplot Amuse Help Python Introduction Math and Numbers for Programmers Edwin van der Helm 1 print 2+3 #=5 2 print 2-3 #= -1 Python 3 print 10*3 #= 30 Scripts 4 print 12/3 #=4 Variables Math and 5 print 10%3 #=1 Numbers Strings and 6 print 10**3 #= 1000 Lists Indentation 7 Logic 8 print10+3*5 # first *,/ then +,- Functions Parameters 9 print (10 + 3) * 5 Advanced tricks 10 print -1**2 #= -(1**2) Classes 11 print 1/2 #=0 Packages 12 print 1./2. #= 0.5 Numpy Astropy 13 print 1.//2. #= 0.0 PyRAF Pyplot Amuse 1 from __future__ import division Help 2 print 1/2 Python Introduction Math and Numbers for Programmers Edwin van der Helm 1 # Float 2 2.3 Python 3 -4. 1 # Explicit Scripts 4 .1 2 float (2) Variables Math and 5 2.99 e10 3 float (2**1000) Numbers Strings and 6 4 Lists -1e1 , -1E1 int (2.3) Indentation 7 5 int ( -2.3) Logic 8 # Long 6 int (2**1000) Functions Parameters 9 2**1000 7 long (2) Advanced tricks 10 2L, 2l 8 str (2) Classes 11 9 complex (1 ,2) Packages 12 10 Numpy # Complex Astropy 13 1J 11 # Test PyRAF Pyplot 14 2. + 3j 12 type ((2 -3j)) Amuse 15 (2.+3j).real Help 16 (2+3j).imag Python Introduction Strings and Lists for Programmers Edwin van der Helm Python Scripts 1 x ='test string' Variables Math and 2 Numbers Strings and 3 'doesn\'t' Lists Indentation 4 "doesn't" Logic 5 '"Yes," he said.' Functions 6 Parameters Advanced tricks 7 """ This isa multi Classes 8 line string with Packages 9 an entire story in it.""" Numpy Astropy PyRAF Pyplot Amuse Help Python Introduction Strings and Lists for Programmers 1 s ="I havea telescope!" Edwin van der Helm 2 len (s) 3 'tel'+"escope" # Concatination Python 4 'telescope' + 100 # won't work Scripts 5 'telescope'+ str (100) Variables Math and 6 'telescope' * 100 # Be careful Numbers Strings and 7 'telescope'*'100' Lists Indentation 8 Logic 9 s [0] # First character Functions 10 Parameters s [1] # Second character Advanced tricks 11 s [2:10] # Substring Classes 12 s [100] # Error Packages 13 s[ -1] # Last character Numpy Astropy 14 PyRAF Pyplot 15 s = s.strip() Amuse 16 s1 = s.split() Help 17 s2 = s.split('e') 18 s = s.replace("telescope","computer") 19 "Name: {0}, age: {1}". format ('John', 35) Python Introduction Strings and Lists for Programmers Edwin van der Helm 1 x = ["I","havea","telescope!", 4] Python 2 Scripts 3 # Slices Variables Math and 4 x [0] Numbers Strings and 5 x [1:] Lists Indentation 6 x [::2] Logic 7 x [2] ="computer!" Functions 8 Parameters x [1:3] ='bla' Advanced tricks 9 Classes 10 # Dictionary Packages 11 y = {1323:'Bob', 6543:'Alice'} Numpy Astropy 12 print y[1323] PyRAF Pyplot 13 y = {'Bob':1323,'Alice':6543} Amuse 14 printy['Alice'] Help Python Introduction Strings and Lists for Programmers Edwin van der Helm Python Scripts Variables Math and Numbers Strings and 1 # List comprehensions Lists Indentation 2 print range (10) Logic 3 squares = [i**2 fori in range (30)] Functions 4 Parameters dict_squares = [i:i**2 fori in range (30)] Advanced tricks Classes Packages Numpy Astropy PyRAF Pyplot Amuse Help Python Introduction Strings and Lists for Programmers Edwin van der Helm Python 1 x = ["I","havea","telescope!", 4] Scripts 2 Variables 3 "".join(x[:-1]) Math and Numbers 4 x. append ("yes") Strings and Lists 5 x. extend ( range (6)) Indentation Logic 6 x.insert(2,"good") Functions 7 x. remove ('I') Parameters Advanced tricks 8 x. pop () Classes 9 x. index ('havea') Packages 10 x. count (4) Numpy 11 x. sort () Astropy PyRAF 12 x.reverse() Pyplot Amuse Help Python Introduction Indentation for Programmers Edwin van der Helm 1 fori in range (3, 20, 2): Python 2 print"This is part of the loop", i Scripts Variables 3 print"This is also part of the loop" Math and Numbers 4 print"This is not part of the loop" Strings and Lists 5 Indentation 6 Logic # Use4 spaces or one tab as indentation. Functions 7 # Please be consistent with your indentation Parameters 8 Advanced tricks 9 fori in range (5): Classes 10 forj in range (5): Packages Numpy 11 print"Inner loop", i, j Astropy 12 PyRAF print"Outer loop", i, j Pyplot 13 print"Not in the loop" Amuse Help Python Introduction Indentation for Programmers Edwin van der Helm 1 for letter in'Python': 2 if letter =='h': Python 3 break Scripts 4 print'Current Letter:', letter Variables Math and 5 Numbers Strings and 6 for letter in'Python': Lists Indentation 7 if letter =='h': Logic 8 continue Functions Parameters 9 print'Current Letter:', letter Advanced tricks 10 Classes 11 for letter in'Python': Packages 12 if letter =='x': Numpy Astropy 13 break PyRAF Pyplot 14 print'Current Letter:', letter Amuse 15 else: Help 16 print'Nox in python' Python Introduction Logic for Programmers Edwin van der Helm 1 if'Steven' in['Bob','Steven','Fred']: 2 print'Here!' Python 3 Scripts 4 if'Carol' not in['Bob','Steven','Fred']: Variables Math and 5 print'Away!' Numbers Strings and 6 Lists Indentation 7 test = a == b Logic 8 if test: print'Equal' Functions Parameters 9 Advanced tricks 10 x = int ( raw_input ("Enter integer:")) Classes 11 if x == 0: Packages 12 print'Zero' Numpy Astropy 13 elif x == 1: PyRAF Pyplot 14 print'One' Amuse 15 else: Help 16 print'Not binary' Python Introduction Logic for Programmers Edwin van der Helm 1 "sub" in"mysubstring" Python 2 "mystring".startswith("myst") Scripts Variables 3 Math and Numbers 4 0 and"not empty" Strings and Lists 5 False or"" or [1,2,3] or 42 Indentation 6 Logic "string" is not None Functions 7 Parameters 8 2 < x <= 10 Advanced tricks 9 Classes 10 x = y = [1,2,3] Packages Numpy 11 [1 ,2 ,3] == x Astropy 12 PyRAF [1 ,2 ,3] isx Pyplot 13 y isx Amuse Help Python Introduction Functions for Programmers Edwin van der 1 def my_function(x, y): Helm 2 a = x + y Python 3 printa Scripts 4 Variables 5 z = 10 Math and Numbers 6 my_function(z, 2) Strings and Lists 7 my_function(z, z-2) Indentation Logic 8 Functions 9 # Return values Parameters Advanced tricks 10 def my_function_2(x, y): Classes 11 a = x + y Packages 12 b = x * y Numpy 13 return a,b Astropy PyRAF 14 Pyplot Amuse 15 if __name__ =="__main__": Help 16 print my_function_2(1, 2) 17 m, p = my_function_2(1, 2) Python Introduction Parameters for Programmers Edwin van der Helm 1 def my_function(x, y): Python 2 print x+y**2 3 Scripts Variables 4 if __name__ =="__main__": Math and Numbers 5 # Calling by keyword Strings and Lists 6 my_function(y=2, x=3) Indentation Logic 7 Functions 8 # default values Parameters Advanced tricks 9 def my_function_2(x=1, y=1): Classes 10 print x + y**2 Packages 11 Numpy 12 if __name__ =="__main__": Astropy PyRAF 13 my_function_2(2, 3) Pyplot Amuse 14 my_function_2(y=2) Help 15 my_function_2() Python Introduction Parameters for Programmers 1 def my_function(x, y): Edwin van der Helm 2 print x+y**2 3 Python 4 if __name__ =="__main__": Scripts 5 arguments = [2, 3] Variables Math and 6 my_function(*arguments) Numbers Strings and 7 Lists Indentation 8 params = {'y':3,'x':5} Logic 9 my_function(**params) Functions 10 Parameters Advanced tricks 11 def my_function_2(*args, **kwargs): Classes 12 for arg in args: Packages 13 print arg Numpy Astropy 14 PyRAF Pyplot 15 for name, value in kwargs.items(): Amuse 16 print name,"has value", value Help 17 18 if __name__ =="__main__": 19 my_function_2(1, 2, 3, x=4, y=5, z=6) Python Introduction Advanced tricks for Programmers 1 def todo_function(x): Edwin van der Helm 2 pass 3 Python 4 def change_and_call(x, y, Scripts 5 func=todo_function) Variables Math and 6 a = x % y Numbers Strings and 7 func (a) Lists Indentation 8 Logic 9 def other_function(x): Functions 10 Parameters def inner_function(x) Advanced tricks 11 printx Classes 12 Packages 13 change_and_call(x, x+1, inner_function) Numpy Astropy 14 PyRAF Pyplot 15 if __name__ =="__main__": Amuse 16 change_and_call(1, 2, other_function) Help 17 18 print change_and_call(1, 2, 19 lambda y: y**2) Python Introduction Classes for Programmers Edwin van der Helm 1 class Planet( object ): Python 2 def __init__(self , name, mass): Scripts 3 self .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    41 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us