diff --git a/guess.py b/guess.py old mode 100644 new mode 100755 index f68f125..8fb3b11 --- a/guess.py +++ b/guess.py @@ -1,29 +1,30 @@ +#!/usr/bin/python3 + import random number = random.randint(1, 20) guesses = 0 -print 'Hello! What is your name?' -name = raw_input() +print('Hello! What is your name?') +name = input() -print "Hi, {}. I'm thinking of a number from 1 and 20.".format(name) +print(f"Hi, {name}. I'm thinking of a number from 1 and 20.") while guesses < 6: - - print 'What is your guess. You have {} more guesses.'.format(6-guesses) - guess = raw_input() + print(f'What is your guess? You have {6 - guesses} more guesses.') + guess = input() guess = int(guess) guesses = guesses + 1 if guess < number: - print 'Too low.' + print('Too low.') elif guess > number: - print 'Too high.' + print('Too high.') elif guess == number: - print 'Good job, {}! You guessed my number in {} guesses!'.format(name,guesses) + print(f'Good job, {name}! You guessed my number in {guesses} guesses!') break if guess != number: - print 'Nope. The number I was thinking of was {}.'.format(number) + print(f'Nope. The number I was thinking of was {number}.') diff --git a/guess.rb b/guess.rb old mode 100644 new mode 100755 index 18a7c52..f15c2dc --- a/guess.rb +++ b/guess.rb @@ -1,30 +1,22 @@ +#!/usr/bin/env ruby + number = rand(1..20) -guesses = 0 puts 'Hello! What is your name?' -name = gets.chomp.to_s - -puts "Hi, #{name}. I'm thinking of a number between 1 and 20." +name = gets&.chomp -while guesses < 6 +puts "Hi, #{name}. I'm thinking of a number between 1 and 20." - puts "What is your guess? You have #{6-guesses} more guesses." - guess = gets.chomp.to_i - guesses += 1 +1.upto 6 do |guesses| + puts "What is your guess? You have #{7 - guesses} more guesses." + guess = gets&.chomp.to_i - unless guess == number - message = if guess > number - "Too high" - else - "Too low" - end - puts message - else + if guess == number puts "Good job, #{name}! You guessed my number in #{guesses} guesses." exit + else + puts(guess > number ? 'Too high' : 'Too low') end - end puts "Nope. The number I was thinking of was #{number}." - diff --git a/images/codeeval2014.jpg b/images/codeeval2014.jpg new file mode 100644 index 0000000..58c89b5 Binary files /dev/null and b/images/codeeval2014.jpg differ diff --git a/images/codeeval2015.jpg b/images/codeeval2015.jpg new file mode 100644 index 0000000..8ccf9c7 Binary files /dev/null and b/images/codeeval2015.jpg differ diff --git a/python.png b/images/python.png similarity index 100% rename from python.png rename to images/python.png diff --git a/pyvsrb.png b/images/pyvsrb.png similarity index 100% rename from pyvsrb.png rename to images/pyvsrb.png diff --git a/readme.md b/readme.md index 85d53e1..f281cd9 100644 --- a/readme.md +++ b/readme.md @@ -1,29 +1,31 @@ ## Should I Learn Python or Ruby? -![pyvsrb](https://raw.github.com/mjhea0/python-ruby/master/pyvsrb.png) +![pyvsrb](https://raw.githubusercontent.com/mjhea0/python-ruby/master/images/pyvsrb.png) -At first glance, Python and Ruby appear to be very similar languages. Both are high-level, [dynamic](http://en.wikipedia.org/wiki/Dynamic_programming_language) languages used for rapid development. Both are beautiful languages that, when done well, can be intuitive and be read much like English. +At first glance, Python and Ruby appear to be very similar languages. Both are high-level, [dynamic](http://en.wikipedia.org/wiki/Dynamic_programming_language) languages used for rapid development. Both are beautiful languages that, when written well, are intuitive and can read much like English. What do I mean by dynamic? Well, with a dynamically typed language you can do this: ```sh >>> variable = 1 >>> type(variable) - + >>> variable = "Foo" >>> type(variable) - + >>> variable = ["bar",10] >>> type(variable) - + ``` -Essentially, I can change the datatype (from an integer to a string to a list, in the above example) at any point in a program. In a statically typed language, this would result in an error when compiled. +Essentially, you can change the datatype (from an integer to a string to a list, in the above example) at any point in a program. In a statically typed language, this would result in an error when compiled. Both support multiple programming paradigms as well - e.g, procedural, functional, object oriented, and so forth. However, there are a number of differences .. +> This article begins with the language structure, then moves toward detailing what you can do with the two languages. Although the differences are important, it's much more important to focus on your end goal - that is, what you want to accomplish with said language. More on this later. + ### The Python Way Open your terminal and enter the Python shell then type `import this`: @@ -61,9 +63,9 @@ This is what is known as the [Zen of Python](http://www.python.org/dev/peps/pep- 4. Complex is better than complicated. 5. Readability counts. -Essentially, Pythonists value code readability and productivity over all else. +Essentially, Pythonists value code readability and productivity over all else. -> Check out [this](http://stackoverflow.com/questions/228181/the-zen-of-python) StackOverflow question to see examples of all the guidelines in use. +> Check out [this](http://www.slideshare.net/doughellmann/an-introduction-to-the-zen-of-python) Slideshare to see examples of all the guidelines in use. Highly recommended! Finally, if shortened to one major point- @@ -77,11 +79,18 @@ While Python values one main way of solving a problem, Ruby - influenced by Perl *Ruby inherited the Perl philosophy of having more than one way to do the same thing. I inherited that philosophy from Larry Wall, who is my hero actually. I want to make Ruby users free. I want to give them the freedom to choose. People are different. People choose different criteria. But if there is a better way among many alternatives, I want to encourage that way by making it comfortable. So that’s what I’ve tried to do. Maybe Python code is a bit more readable. Everyone can write the same style of Python code, so it can be easier to read, maybe. But the difference from one person to the next is so big, providing only one way is little help even if you’re using Python, I think. I’d rather provide many ways if it’s possible, but encourage or guide users to choose a better way if it’s possible.* -—Yukihiro Matsumoto (Matz) +—Yukihiro Matsumoto (Matz) Source: [http://www.artima.com/intv/rubyP.html](http://www.artima.com/intv/rubyP.html) -With more freedom and less syntactical rules, many Rubyists believe that Ruby is a much more elegant language - and it is. But you can also often see messy code (especially from beginners) that can be difficult for other developers to read. For example, you can put multiple statements on one line. This can look good (or sexy as many Rubyists put it) - and be readable - depending on how it's coded or it can be a mess. +With more freedom and less syntactical rules, many Rubyists believe that Ruby is a much more elegant language - and it is. Also, while Python focuses on simplicity, Ruby focuses on making the programmer happy, while this tends to be true and people can make truly beautiful programs, you can also often see messy code (especially from beginners) that can be difficult for other developers to read( or even themselves in a not so distant future). For example, you can put multiple statements on one line. This can look good and be readable, depending on how it's coded - or it can be a mess. + +### The principle of least astonishment + +One principle very present in the design of Ruby is the [principle of least astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment) - the language working the way you think it should. + +In order to behave like this Ruby tends to do things like having the same functions with different names, which can be confusing for beginners. Although Python does not have this principle in mind when making the language, it also behaves quite like this by make pretty much everything work in a standard way. One could argue that this only benefits advanced Python users, though. + Let's compare some code. The following snippets of code are for solving the Fibonacci sequence: @@ -89,8 +98,9 @@ Let's compare some code. The following snippets of code are for solving the Fibo ```ruby def fib(n) - n <= 2 ? 1 : fib(n-2) + fib(n-1) -end + n < 2 ? n : fib(n-1) + fib(n-2) +end +alias :fibonacci :fib ``` *Python:* @@ -103,7 +113,11 @@ def fib(n): return fib(n-1) + fib(n-2) ``` -Although you can write this code in many ways, both of these methods are *true* to the language. In other words, the Ruby example is very Ruby-ish while the Python example is very Pythonic. Can you read the Ruby code? It may be more elegant but it's a bit harder to read. Meanwhile, it's easy to follow the Python code, right? You of course can write code anyway you want. It's advisable to write Ruby code, when beginning, in a more Pythonic way - which simply means making it more readable: +Although you can write this code in many ways, both of these methods are *true* to the language. + +> The Ruby code demonstrates the practices of using ternary operators and of having multiple names for the same function (`fib` and `fibonacci` are the same). + +In other words, the Ruby example is very Ruby-ish while the Python example is very Pythonic. Can you read the Ruby code? It may be more elegant but it's a bit harder to read. Meanwhile, it's easy to follow the Python code, right? You of course can write code anyway you want. It's advisable to write Ruby code, when beginning, in a more Pythonic way - which simply means making it more readable: ```ruby def fib(n) @@ -115,7 +129,7 @@ def fib(n) end ``` -Keep in mind that in many cases in Python, there are a number of ways to do one thing. Take copying a list for example. There's a least four different ways: +Keep in mind that in many cases with Python there are still a number of ways to do one thing. Take copying a list for example. There are at least four different ways: ```python >>> my_list = [1,2,3] @@ -138,11 +152,11 @@ The difference is that there is one right way of doing this given the situation. ### More differences -As you can imagine, there are many more differences that just the syntax and philosophies of the two languages. Let's quickly look at some examples. +As you can imagine, there are many more differences than just the syntax and philosophies of the two languages. Let's quickly look at some examples. #### Learning Curve -Without a doubt, Python is much easier to learn because of how the language is structured - and how **explicit** it is. One can literally become proficient in less than two months. Ruby takes much longer to learn due to its flexibility. Beneath the elegant surface, there's a lot of magic happening. It takes a while to grasp exactly what is happening. It can take upwards of six months to become proficient in Ruby. +Without a doubt, Python is much easier to learn because of how the language is structured - and how **explicit** it is. One can literally become proficient in two to three months. Ruby takes much longer to learn due to its flexibility. Beneath the elegant surface, there's a lot of magic happening. It takes a while to grasp exactly what is happening. It can take upwards of six months to become proficient in Ruby. You can see just how explicit Python is based on this example: @@ -158,28 +172,74 @@ require 'twitter' twit = Twitter.new ``` -In the first example (Python), we are importing the `Twitter()` class from the `twitter` library, while in the latter example (Ruby), we are simply importing the `twitter` library, giving us access to the entire library, not just the `Twitter()` class. So you can see that in Python, you import only what you need, nothing else. +In the first example (Python), we are importing the `Twitter()` class from the `twitter` library, while in the latter example (Ruby), we are simply importing the `twitter` library, giving us access to the entire library, not just the `Twitter()` class. So you can see that in Python, you import only what you need, nothing else. -![learning_curve](https://raw.github.com/mjhea0/python-ruby/master/python.png) +![learning_curve](https://raw.github.com/mjhea0/python-ruby/master/images/python.png) #### Programming Paradigms -Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards true object orientation over the last few years. This is a toss-up. Neither language has the upper hand, in other words. +Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards being a true object orientated language over the last few years. However, Ruby has one advantage: it can add methods to existing classes, while Python by default can't do this (although it's possible with the use of external libraries). #### Performance -Performance is a toss up as well. In some cases Python performs better, while in other cases, Ruby outperforms Python. It all depends on the task at hand. +Performance is a toss up as well. In some cases Python performs better, while in others Ruby outperforms Python. It all depends on the task at hand. #### Usage -Python can be used for web development, sys admin, statistics, and scientific computation. Meanwhile, while Ruby can do all those things, it's used primarily for web development (Or: For learning Rails). Python has the upper hand here, but if you are looking to *only* do web development, Ruby is the way to go. +Ruby has a bigger web presence with Rails than Python does with Django, so if you're looking to move into web development, Ruby may be the way to go. Python is a great general-purpose language and has more momentum going for it for areas outside of the web, such as sys admin/DevOps, statistics, and scientific computation. + +That said, take a look at the two code snippets below - + +```python +print("Hello, World!") +``` + +and + +```ruby +puts 'Hello, world!' +``` + +End users do not care about the syntactical differences; they just want to see "Hello, World!" outputted on their screen. Think about this site that you're reading this post on now. Do you really care of it's Ruby/Rails-based or Python/Django-based? I imagine you just want to be able to read this post. So, if you want to go into web development, worry less about the back-end language. Learn one, then get really good at. Then go learn JavaScript. #### Community -Python wins. The community is active, vibrant, and truly helpful. Although, you can say the same about the Ruby community, the community itself is very much tied into Rails. If Rails is your thing, then you are in luck. +The Python community is active, vibrant, and truly helpful. Although you can say the same about the Ruby community, the community itself is very much tied into Rails. If Rails is your thing, then you are in luck. + +#### Popularity/Jobs + +![popularity](https://static1.squarespace.com/static/51361f2fe4b0f24e710af7ae/t/56b1187d4c2f85efc5598bb1/1454446752995/?format=750w) +Source: http://blog.codeeval.com/codeevalblog/2016/2/2/most-popular-coding-languages-of-2016 -### Extended Example + +![popularity](https://raw.github.com/mjhea0/python-ruby/master/images/codeeval2015.jpg) + +For the fourth year in a row, Python is the most popular language. Also, notice how Ruby decreased in popularity: + +![popularity](https://raw.github.com/mjhea0/python-ruby/master/images/codeeval2014.jpg) + +Source: http://blog.codeeval.com/codeevalblog/2014#.Uxewd_SwL5g + +Excellent [article](http://techcrunch.com/2012/09/30/an-analysis-of-market-demand-for-web-programming-languages/) about jobs by programming language. + +Indeed: +- [Python Jobs in SF](http://www.indeed.com/q-Python-l-San-Francisco-Bay-Area,-CA-jobs.html) +- [Ruby Jobs in SF](http://www.indeed.com/jobs?q=Ruby&l=San+Francisco+Bay+Area%2C+CA) +- [Rails Jobs in SF](http://www.indeed.com/jobs?q=rails&l=San+Francisco+Bay+Area%2C+CA) + +#### Average Salary + +1. [Rails Developer - SF](http://www.indeed.com/salary?q1=rails+developer&l1=san+francisco) +2. [Django Developer - SF](http://www.indeed.com/salary?q1=django+developer&l1=san+francisco) + +#### Companies + +1. Python - Disqus, Dropbox, Yelp, Google, Reddit +2. Ruby - Hulu, Twitter, Github, Airbnb + + +### Extended Code Example (for further comparison) Guessing game ... @@ -187,65 +247,55 @@ Guessing game ... ```python import random -import os + number = random.randint(1, 20) guesses = 0 -print 'Hello! What is your name?' -name = raw_input() +print('Hello! What is your name?') +name = input() -print "Hi, {}. I'm thinking of a number from 1 and 20.".format(name) +print(f"Hi, {name}. I'm thinking of a number from 1 and 20.") while guesses < 6: - - print 'What is your guess. You have {} more guesses.'.format(6-guesses) - guess = raw_input() + print(f'What is your guess? You have {6 - guesses} more guesses.') + guess = input() guess = int(guess) guesses = guesses + 1 if guess < number: - print 'Too low.' + print('Too low.') elif guess > number: - print 'Too high.' + print('Too high.') elif guess == number: - print 'Good job, {}! You guessed my number in {} guesses!'.format(name,guesses) + print(f'Good job, {name}! You guessed my number in {guesses} guesses!') break if guess != number: - print 'Nope. The number I was thinking of was {}.'.format(number) + print(f'Nope. The number I was thinking of was {number}.') ``` #### Ruby ```ruby number = rand(1..20) -guesses = 0 puts 'Hello! What is your name?' -name = gets.chomp.to_s +name = gets&.chomp -puts "Hi, #{name}. I'm thinking of a number between 1 and 20." +puts "Hi, #{name}. I'm thinking of a number between 1 and 20." -while guesses < 6 +1.upto 6 do |guesses| + puts "What is your guess? You have #{7 - guesses} more guesses." + guess = gets&.chomp.to_i - puts "What is your guess? You have #{6-guesses} more guesses." - guess = gets.chomp.to_i - guesses += 1 - - unless guess == number - message = if guess > number - "Too high" - else - "Too low" - end - puts message - else + if guess == number puts "Good job, #{name}! You guessed my number in #{guesses} guesses." exit + else + puts(guess > number ? 'Too high' : 'Too low') end - end puts "Nope. The number I was thinking of was #{number}." @@ -255,9 +305,21 @@ What are the syntactical differences? Is the Python code Pythonic? Is the Ruby c ### Conclusion -As you can tell, there's not too many differences in the languages themselves. Yes, Python values **readability**, **one method of doing things**, and being **explicit** - but these are minor differences, in my opinion. It's more about the end goal. What are you trying to accomplish. My advice: Try both languages. See which language you like better, then learn that one. After that, learn the other. +As you can tell, there's not too many differences in the languages themselves. Yes, Python values **readability**, **one method of doing things**, and being **explicit** - but these are minor differences. It's more about the end goal. What are you trying to accomplish? My advice: Try both languages. See which language you like better, then learn that one. After that, learn the other. Always, always, ALWAYS remember that no language can do it *all*. In other words, all languages have their positives and negatives Cheers! +### Resources + +1. [Python vs Ruby](http://c2.com/cgi/wiki?PythonVsRuby) +1. [Why I push for Python](http://lorenabarba.com/blog/why-i-push-for-python/) +1. [4 Reasons Why You Should Learn Ruby As Your First Programming Language](http://www.skilledup.com/learn/programming/4-reasons-learn-ruby-first-programming-language/) +1. [How do Python and Ruby compare?](http://www.quora.com/How-do-Python-and-Ruby-compare/answer/Fabio-Akita?share=1) +1. [Why Python is a Great First Language](http://blog.trinket.io/why-python/) +1. [Python vs Ruby](http://www.scriptrock.com/articles/python-vs-ruby) +1. [Ruby on Rails vs Python and Django: Which Should a Beginner Learn?](https://www.coursereport.com/resources/ruby-on-rails-vs-python-and-django-which-should-a-beginner-learn) +1. [Python Environment Management for Rubyists – a Guide](http://spin.atomicobject.com/2015/01/02/python-environment-mgmt/) +1. [Ruby vs Python](https://www.coursereport.com/blog/ruby-vs-python-choosing-your-first-programming-language) +1. [Ruby vs Python, the Definitive FAQ](https://hackernoon.com/ruby-vs-python-the-definitive-faq-5cb0046292be) diff --git a/todo.md b/todo.md deleted file mode 100644 index a538afc..0000000 --- a/todo.md +++ /dev/null @@ -1,5 +0,0 @@ -Shift more to - The article focuses on language structure, which isn't very helpful to people when deciding what to code in. It's important to know what you can do in two frameworks/languages, rather than the differences in how you do it. - -In addition to the coding sections you may want to highlight other issues, i.e. popularity, community support, number of current job postings, average developer salary, popular companies and which one they use and that sort of stuff. - -Also you may want to highlight the different areas where each are more strongly used. For example I think it’s safe to say that ruby is much more strongly used in the DevOps community vs Python in the Scientific / AI / data modelling community. The web is kind of a toss up, but probably rails is the leading framework these days…. Security probably goes to python, although there are some really popular ruby tools aka Metasploit…