we already know java why learn python using python to
play

We already know Java. Why learn Python? Using Python to Implement - PowerPoint PPT Presentation

We already know Java. Why learn Python? Using Python to Implement Algorithms Python has far less overhead than Java for the programmer. Tyler Moore Python is closer to psuedo-code on the English-pseudocode-code spectrum than Java or C/C++, but


  1. We already know Java. Why learn Python? Using Python to Implement Algorithms Python has far less overhead than Java for the programmer. Tyler Moore Python is closer to psuedo-code on the English-pseudocode-code spectrum than Java or C/C++, but it actually executes! CS 2123, The University of Tulsa Python is handy for data manipulation and transformation, and anything ”quick and dirty.” Python is very powerful, thanks to all those extension modules. Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos 2 / 33 Python Resources Selection Sort (Java) 1 void s e l e c t i o n s o r t ( int s [ ] , int n) Download most recent stable 3.x.x version from python.org for all { 2 int i , j ; major platforms 3 min ; int 4 I’ve written an online tutorial: for ( int i =0; i < n − 1; i++) { 5 http://secon.utulsa.edu/cs2123/python3.html min=i ; 6 Think Python by Allen Downey: for ( int j=i +1; j < n ; j++) 7 ( s [ j ] < s [ min ] ) min=j ; i f http://www.greenteapress.com/thinkpython/ 8 int tmp = s [ min ] ; 9 Videos from Khan academy: https://www.khanacademy.org/ s [ min ] = s [ i ] ; 10 science/computer-science-subject/computer-science s [ i ] = tmp ; 11 } 12 3 / 33 4 / 33

  2. Selection Sort, now in Python Insertion Sort (C) i n s e r t i o n s o r t ( int s [ ] , int n) 1 def s e l e c t i o n s o r t ( s ) : Whitespace 1 ””” { 2 matters! 2 i , j ; Input : l i s t s to be s o r t e d int 3 3 Dynamic, implicit Output : s o r t e d l i s t int min ; 4 4 typing ( i =1; i < n ; i++) { ””” for 5 5 i range ( len ( s ) ) : j = i ; for in Iterators make 6 6 (( j > 0) && #don ’ t name min s i n c e r e s e r v e d word while 7 7 looping easy minidx=i ( s [ j ] < s [ j − 1])) { 8 8 Has built-in lists, swap(&s [ j ] ,& s [ j − 1]); for j in range ( i +1, len ( s ) ) : 9 9 dictionaries s [ j ] < s [ minidx ] : j = j − 1; i f 10 10 } minidx=j 11 Multiple variable 11 } s [ i ] , s [ minidx ]= s [ minidx ] , s [ i ] 12 12 assignment per s } return 13 13 line 5 / 33 6 / 33 Insertion Sort (Python) Python Built-in Data Types 1 def i n s e r t i o n s o r t ( s ) : ””” 2 Input : l i s t s to be s o r t e d Python uses implicit, dynamic typing 3 Output : s o r t e d l i s t 4 You can use casting functions float(), int() and str() to explicitly ””” 5 convert. for i in range ( len ( s ) ) : 6 j=i In addition to numbers and strings, Python offers built-in support for #don ’ t c a l l v a r i a b l e min s i n c e i t ’ s r e s e r v e d word 7 while j > 0 and s [ j ] < s [ j − 1]: lists, tuples, sets and dictionaries. 8 s [ j ] , s [ j − 1]=s [ j − 1] , s [ j ] 9 j=j − 1 10 s return 11 7 / 33 8 / 33

  3. Working with lists List indexing Python offers several ways to extract elements and sublists from a list. Syntactically you use square braces at the end of the list variable name, with the indices you want to access. 1 > > s p o r t s =[ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , > ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] Indices start at 0. You can get a range of values by including two 2 > > i n o r o u t =[ ’ out ’ , ’ both ’ , ’ i n ’ , ’ out ’ , ’ out ’ , ’ i n ’ , ’ out ’ , ’ i n ’ ] > numbers separated by a colon. If you use one number and a colon, it 3 > > len ( s p o r t s ) #b u i l t − i n f u n c t i o n computes the l e n g t h of l i s t s > will give you the rest of the list. 4 8 5 > > s p o r t s l o c= z i p ( sports , i n o r o u t ) #b u i l t − i n f u n c t i o n combines l i s t element − wise > You can access the end of the list by using negative numbers 6 > > s p o r t s l o c > 7 [ ( ’ f o o t b a l l ’ , ’ out ’ ) , ( ’ t e n n i s ’ , ’ both ’ ) , ( ’ i c e hockey ’ , ’ i n ’ ) , ( ’ l a c r o s s e ’ , ’ out ’ ) , ( ’ f i e l d hockey ’ , ’ out ’ ) , ( ’ b a s k e t b a l l ’ , ’ i n ’ ) , 1 > > s p o r t s [ 2 ] > 2 ’ i c e hockey ’ ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 3 > > s p o r t s [ 1 : 3 ] > 8 > > r a n d o m l i s t =[ ’ f o o t b a l l ’ , 3 , 6 . 7 , ’ham ’ ] #l i s t elements don ’ t have to be the same type > 4 [ ’ t e n n i s ’ , ’ i c e hockey ’ ] 5 > > s p o r t s [ 2 : ] > 6 [ ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 7 > > s p o r t s [ : 2 ] > 8 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ ] 9 > > s p o r t s [ − 2] > 10 ’ b a s e b a l l ’ 11 > > s p o r t s [ − 2:] > 12 [ ’ b a s e b a l l ’ , ’ swimming ’ ] 9 / 33 10 / 33 Modifying a list Tuples and sets > bar = [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > > > bar . append ( ’ o ’ ) > > > bar Tuples are immutable lists > > [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ o ’ ] Sets are unordered lists > bar . pop ( ) > > ’ o ’ > bar > > > t = (4 ,6 ,2 ,3) > > [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > t [0]=5 > > > bar . extend ( [ ’ y ’ , ’ x ’ ] ) > > Traceback ( most r e c e n t c a l l l a s t ) : > bar > > F i l e ” < st di n > ” , l i n e 1 , i n < module > [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] TypeError : ’ t u p l e ’ object does not support item assignment > bar . i n s e r t ( ’w ’ ,3) > > > s = { 3 ,9 ,6 ,2 } > > Traceback ( most r e c e n t c a l l l a s t ) : > s [ 2 ] > > F i l e ” < st di n > ” , l i n e 1 , i n < module > Traceback ( most r e c e n t c a l l l a s t ) : TypeError : an i n t e g e r i s r e q u i r e d F i l e ” < st di n > ” , l i n e 1 , i n < module > > bar . i n s e r t (3 , ’w ’ ) > > TypeError : ’ s e t ’ does support i n d e x i n g object not > bar > > > 6 s i n > > [ ’ b ’ , ’ a ’ , ’ j ’ , ’w ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] True > bar . s o r t () > > > 7 i n s > > > bar > > F a l s e [ ’ a ’ , ’ b ’ , ’ h ’ , ’ j ’ , ’ l ’ , ’w ’ , ’ x ’ , ’ y ’ ] > bar [ : : − 1 ] > > [ ’ y ’ , ’ x ’ , ’w ’ , ’ l ’ , ’ j ’ , ’ h ’ , ’ b ’ , ’ a ’ ] 11 / 33 12 / 33

Recommend


More recommend