Some quick Python notes

These are notes as I do morning warm-up with Python on CodeWars.

Python != Ruby

(duh)

Some major differences off the bat:

Return values in Ruby are implicitly tied to the last line of code run. Return values in Python need to be explicit. If you don’t tell Python to return something, it..won’t.

Python methods always take an argument, even if its an empty parenthesis. There is no ‘end’ block. Also, put a colon on the first line. Keep spacing consistent. If you use one tab, always use one tab. If you use two spaces, always use two spaces. Otherwise Python can’t parse your work properly.

def string_to_array(s):
return s.split(" ")

Ruby is different. Whitespace doesn’t blow up your program, and methods start with ‘def’ and complete with ‘end’. Also see the difference in the lack of explicit return, because Ruby will implicitly return the last line of code run. Looks like this:

def string_to_array(s)
s.split(" ")
end

I’m not kidding, Python does not fuck around when it comes to whitespace. If you turn a file into a class, you have to format it as such using tabs OR spaces. Otherwise you get the “expected an indented block” error and your code won’t run.

This is wrong:

class Connector:
import mysql.connector
import json

This is right:

class Connector:
import mysql.connector
import json

Codewars is really great for all sorts of training, I’m using it to practice syntax with this new language, I highly recommend it to everyone!

www.codewars.com

--

--

I like to write :)

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store