alist = list(map(lambda x: x*x, [1, 5])) # [1, 25] alist = [2*i for i in range(4) if i%3 == 0] # [0, 6]
"""Unlike in Scala, you cannot just declare two methods with a same name.""" def some(arg="some_default_value"): if arg is None: print("Argument is empty") else: print(arg)
To read JSON file from Python
def open_file(self): try: config = json.loads(open(self.path).read()) self.title = config["title"] self.artist = config["artist"] self.timestamp = config["timestamp"] self.track_id = config["track_id"] self.tags = config["tags"] self.similars = config["similars"] except (OSError, IOError): # blah blah # In Python3, the data of self.title and others will be clean # but in Python2 they will come like this u'value'
except (one, two, three, ....) as e:
a = [1,2,3,4]</pre> <pre>b = [3,4,5,6] list( set(a) & set(b) ) # [3,4] list( set(a) ^ set(b) ) # [1,2,5,6]
# IOError became an alias for OSError so having both of them # will cover pretty much when your file name is not found. # FileNotFoundError is not valid in Python3 except (OSError, IOError) as e:
Actually you’d better not change it. Instead there is a way around for it. Make an alias!
nano ~/.bash_profile // type in the following alias py=python3 //Ctrl-O, Enter, Ctrl-x