tailieunhanh - Ebook Python for computational science and engineering: Part 2
(BQ) Part 2 book "Python for computational science and engineering" has contents: From matlab to python, python shells, symbolic computation, numerical computation, numerical python, visualising data, numerical methods using python, where to go from here. | Chapter 10 From Matlab to Python Important commands The for-loop Matlab Python for i = 1:10 disp ( i ) end for i in range (1 ,11): print ( i ) Matlab requires the end key-word at the end of the block belonging to the for-loop. The if-then statement Matlab Python if a ==0 disp ( ’a is elseif a <0 disp ( ’a is elseif a ==42 disp ( ’a is else disp ( ’a is end zero ’) negative ’) 42 ’) positive ’) Matlab requires the end key-word at the very end of the block belonging to the for-loop. Python requires a colon (“:”) at the of the for-line. (This is important and often forgotten when you have programmed in Matlab before.) Python requires the commands to be executed within the for-loop to be indented. if a ==0: print ( ’a elif a <0: print ( ’a elif a ==42: print ( ’a else : print ( ’a is zero ’) is negative ’) is 42 ’) is positive ’) Python requires a colon (“:”) after every condition (. at the of the lines starting with if, elif, else. Python requires the commands to be executed within each part of the if-then-else statement to be indented. Indexing Matlab’s indexing of matrices and vectors starts a 1 (similar to Fortran), whereas Python’s indexing starts at 0 (similar to C). 91 92 CHAPTER 10. FROM MATLAB TO PYTHON Matrices In Matlab, every object is a matrix. In Python, there is a specialised extension library called numpy (see Sec. 14) which provides the array object which in turns provides the corresponding functionality. Similar to Matlab, the numpy object is actually based on binary libraries and execution there very fast. There is a dedicated introduction to numpy for Matlab users available at for Matlab Users. Chapter 11 Python shells IDLE IDLE comes with every Python distribution and is a useful tool for everyday programming. Its editor provides syntax highlighting. There are two reasons why you might want to use another Python shell, for example: • While working
đang nạp các trang xem trước