The Paylogix Post

Learning Python

Armed with the knowledge of Ruby and JavaScript, I figured learning Python should not be super difficult. In a sense it was not, as we have the familiarity of the kind of objects that we can create but it was hard in that it whitespace is VERY important.

Learning Python

Kevin Wang, Business Analyst at Paylogix, specializes in coding and utlizing platforms like Javascript and Ruby. He chimed into our blog to share his experience with learning Python.

Armed with the knowledge of Ruby and JavaScript, I figured learning Python should not be super difficult. In a sense it was not, as we have the familiarity of the kind of objects that we can create but it was hard in that it whitespace is VERY important.

Notation: Javascript — Ruby — Python
[item1, item2]: array — array — list
{a: 1, b: 2}: object — hash — dictionary

It is good to know that [] are “lists” in Python and {} are a “dictionary”, an easy way is to imagine the key (w0rd) and value (definition).

Defining functions

Javascript

function test(name){
 console.log(`Hi my name is ${name}`)
}test("Molly") // Hi my name is Molly

A few things:
- defining a function uses the “function” keyword
- we need to wrap the block in {}
- you are able to use string interpolation using backticks ``
- if you pass in a number Javascript will convert the integer type into a string

Ruby

def test(name)
 puts "Hello, my name is #{name}"
endtest("Bobby") // Hello, my name is Bobby

A few things:
- defining a function uses the “def” keyword and the “end” keyword
- string interpolation can be used using double quotes “”
- if you pass in an integer Ruby will convert it to a string

Python

def test(name):
   print "Hi, my name is " + nametest("Kevin") // Hi, my name is Kevin

A few things:
- defining a function requires the “def” key word
- at the end of the first line you NEED a colon (:)
- in the following line you MUST tab over, as I mentioned whitespace is important here. The line following the beginning of the block MUST be indented, like “for” loops, “if” statements, etc. Or else you will get the below error:

IndentationError: expected an indented block

- like all other languages you have to call the function to run it
- One thing to note is that when concatenating a string + integer, Python does not convert the type. You will end up just getting an error:

TypeError: cannot concatenate 'str' and 'int' objects

More from this author:

Kevin Wang
Learning Python

Featured Posts

Catagories

ArticlesAwards & AccoladesInterviewsPress ReleasesResearch

Co-op News

Subscribe

* indicates required
Choose your subscriptions
Email Format

View previous campaigns.