Cara menggunakan ruby vs python

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.

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_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
2 keyword. Python does not.

//--ruby waydef method_name(args)
task1
task2
end
//--python waydef methodName(args):
task1
task2

Return Values

In Ruby, methods implicitly (read: magically) returns the value of the last line of the expression unless an explicit

//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
3 statement was issued.

In Python, methods must end with

//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
3 or
//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
5 or it will fall back to an implicit return statement that returns
//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
6. You gotta be more explicit.

//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7

Indentation

Ruby is indentation-insensitive. It only looks for line breaks and

//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
2 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)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2

Inheritance

Ruby uses

//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
8 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_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
9 or
//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
0 operator to print the string onscreen. Ruby uses
//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
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)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
2 or
//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
3 to indicate substitutions. To output a string, “Kim! My name is Slim Shady.”:

//--ruby way name1 = "Kim"
name2 = "Slim Shady"
puts "#{name1}! My name is #{name2}."
//--python way 1print "{A}! My name is {B}.format("Kim", “Slim Shady”)//--python way 2name1 = "Slim Shady"
print "{}! My name is {}.format("Kim", name1)
//--python way 3print "%s! My name is %s." % ("Kim", "Slim Shady")

Multiline Strings

Ruby uses heredoc

//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
4, whereas Python uses triple quotes
//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
5 or continuation lines
//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
6.

//--ruby waytext = <<~ HEREDOC 
multiline string
multiline string
multiline string
HEREDOC
//--python way 1text = """ multiline string
multiline string
multiline string """
//--python way 2text = "multiline string"\
"multiline string"\
"multiline string"

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
end
//--python waydef method(self):

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"
arg2 = "love"
def method(arg1, arg2, arg3="baloney")
puts "#{arg1} #{arg2} #{arg3}"
end
output>> "I love baloney"//--python wayarg1 = "I"
arg2 = "love"
def method(arg1, arg2, arg3="baloney"):
print "%s %s %s" % arg1, arg2, arg3
output>> "I love baloney"

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)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
7 or
//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
8 where Python would use
//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
6.

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_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
0

Standard Library

Length

//--ruby waydef simple_method
number = 5
apple = 7
end
output>> 7def simple_method
number = 5
apple = 7
return number
end
output>> 5//--python waydef simpleMethod:
number = 5
apple = 7
output>> Nonedef simpleMethod:
number = 5
apple = 7
pass
output>> Nonedef simpleMethod:
number = 5
apple = 7
return 7
output>> 7
1

Note: Python2 rounds down to an integer. Python3 defaults to float

Print

In Ruby,

//--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
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)
task1
if conditional
task2
end
end
def methodB(args)
task1
if conditional
task2
end
end
//--python waydef methodB(args):
task1
if conditional
task2
0 adds a newline. Use
//--ruby way name1 = "Kim"
name2 = "Slim Shady"
puts "#{name1}! My name is #{name2}."
//--python way 1print "{A}! My name is {B}.format("Kim", “Slim Shady”)//--python way 2name1 = "Slim Shady"
print "{}! My name is {}.format("Kim", name1)
//--python way 3print "%s! My name is %s." % ("Kim", "Slim Shady")
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.