Ruby is my favorite programming language but I find Python to be more in demand these days (maybe because Python is still the most common language taught at universities?). Luckily, when you’re learning a new language, you don’t have to start from scratch—no need to re-learn what a verb or noun is… you just have to learn some vocabulary and the particular way the foreign language puts words together to make meaningful statements. Show Similarly, the basic building blocks of programming, like data structures, scope, and flow control, are found across all programming languages. Most often, you really only need to learn new syntax and vocabulary. So if you need to make the switch from Ruby to Python, I began compiling some of their differences… Defining methods Python starts methods with a colon. Ruby does not. Ruby closes methods with an //--ruby waydef simple_method2 keyword. Python does not. //--ruby waydef method_name(args) Return Values In Ruby, methods implicitly (read: magically) returns the value of the last line of the expression unless an explicit //--ruby waydef simple_method3 statement was issued. In Python, methods must end with //--ruby waydef simple_method3 or //--ruby waydef simple_method5 or it will fall back to an implicit return statement that returns //--ruby waydef simple_method6. You gotta be more explicit. //--ruby waydef simple_method Indentation Ruby is indentation-insensitive. It only looks for line breaks and //--ruby waydef simple_method2 to distinguish one code block from another. However, indentation is useful for readability, so use it anyway! Python, on the other hand, requires indentation. Unless you write your entire code block on one line or use continuation lines, indentation levels indicate which block is nested in which. The absolute amount of indentation doesn’t matter as much as indentation a block has relative to other blocks. Python thinks it has reached the end of a function once the indentation level falls back to the level where the function is defined. //--ruby way: method A and method B are functionally the samedef methodA(args) Inheritance Ruby uses //--ruby waydef simple_method8 to signify a child inheriting from a parent class. Python places the parent class inside parentheses as if passing an argument to the child. //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal) String Interpolation Ruby sees everything as an expression that can be evaluated to a string then passed to a //--ruby waydef simple_method9 or //--ruby way: method A and method B are functionally the samedef methodA(args)0 operator to print the string onscreen. Ruby uses //--ruby way: method A and method B are functionally the samedef methodA(args)1 to indicate string substitutions within an expression. Python distinguishes between statements and expressions but only expressions can be evaluated and therefore printed to the screen. Python uses //--ruby way: method A and method B are functionally the samedef methodA(args)2 or //--ruby way: method A and method B are functionally the samedef methodA(args)3 to indicate substitutions. To output a string, “Kim! My name is Slim Shady.”: //--ruby way name1 = "Kim" Multiline Strings Ruby uses heredoc //--ruby way: method A and method B are functionally the samedef methodA(args)4, whereas Python uses triple quotes //--ruby way: method A and method B are functionally the samedef methodA(args)5 or continuation lines //--ruby way: method A and method B are functionally the samedef methodA(args)6. //--ruby waytext = <<~ HEREDOC Class Methods Ruby references the calling object using self.method whereas Python passes the class instance to itself as an argument. //--ruby waydef self.method Arguments Ruby distinguishes between optional and non-optional arguments. Python distinguishes between positional arguments and keyword arguments. As in Ruby, arguments to be passed in the same order as in the function signature UNLESS they are keyword arguments. Keywords can be passed in any order. //--ruby wayarg1 = "I" Note: Use %s to denote a string, %d to denote an integer, %f to denote float. Instantiation //--ruby way@burrito = Burrito.new//--python wayburrito = Burrito() Iteration //--ruby waybags.each do |bag|//--python wayfor item in bags: (indent) i += 1 Data types Python automatically supports tuples. Ruby requires building a custom data type to support tuples, which is basically an array with two elements. Ruby uses //--ruby way: method A and method B are functionally the samedef methodA(args)7 or //--ruby way: method A and method B are functionally the samedef methodA(args)8 where Python would use //--ruby waydef simple_method6. For boolean data types, Ruby uses //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)0 or //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)1, whereas Python uses capitalized //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)2 or //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)3. Ruby’s //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)0 and //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)1 are instances of a //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)6 and a //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)7 respectively. Dependencies //--ruby waydef simple_method0 Standard Library Length //--ruby waydef simple_method1 Note: Python2 rounds down to an integer. Python3 defaults to float In Ruby, //--ruby way: method A and method B are functionally the samedef methodA(args)0 does not add a new line. Use puts to add a new line //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)9 to the end of each argument is there isn’t one already. In Python //--ruby way: method A and method B are functionally the samedef methodA(args)0 adds a newline. Use //--ruby way name1 = "Kim"1 to prevent adding a newline. Apa perbedaan yang mendasar dari program R vs Python?Perbedaan yang sangat jelas adalah Python digunakan pada data science dan software development, sedangkan R digunakan untuk analisis statistik. Python merupakan bahasa multipurpose sama seperti Java dan C++.
Apakah Python bagus untuk pemula?Bahasa Python sangat berperan dalam bidang Machine Learning dan Deep Learning, alasannya karena sintaksnya yang sederhana dan mudah dibaca, mudah dikuasai dan cocok untuk pemula, serta Python memiliki banyak library yang dapat digunakan dalam perhitungan dan pemrosesan statistik.
Python bagus untuk apa?Python adalah bahasa pemrograman yang banyak digunakan dalam aplikasi web, pengembangan perangkat lunak, ilmu data, dan machine learning (ML). Developer menggunakan Python karena efisien dan mudah dipelajari serta dapat dijalankan di berbagai platform.
Mengapa bahasa Python lebih mudah?Mudah untuk Dipelajari
Kalau Python menjadi bahasa pemrograman yang sangat cocok untuk dipelajari oleh programmer pemula. Alasannya karena struktur kode Python mirip dengan bahasa Inggris sehari-hari serta memiliki sintaksis ringkas sehingga membuat Python mudah dipahami dibandingkan bahasa pemrograman lainnya.
|