How great is Python? Can you give me some tricks?
Python is pretty great. It starts off easy, as does any language, with the hello world program.
>>> print("Hello, world!") Hello, world!
Then, you dive deeper. You learn about the data structures built into Python, lists, strings, integers, floats, dictionaries (Python's cute name for maps), tuples, etc. You discover some tricks, like the fact that strings and lists are really similar in some ways - you can slice strings, for example:
>>> x = 'i want to select this' >>> x[:6] + x[-5:] 'i want this'
Oh yeah, Python also supports negative indexing for strings and lists. But you can't assign a certain index in a string like you can a list!
Then, you come across a string like ‘1122334455', and you only want ‘12345'. Lucky for you, Python slicing also allows you to skip stuff too!
>>> x = '1122334455' >>> x[::2] '12345'
What if you had a list of numbers and you only wanted the even ones? Use a list comprehension:
>>> x = [1,2,3,4,5,6,7,8] >>> [i for i in x if i%2==0] [2,4,6,8]
And then there's set comprehensions and dictionary comprehensions. And nested comprehensions, which can get confusing fast.
You learn about generators, a memory efficient function that calculates results lazily.
def up_to_million(): i = 0 while i < 1000000: yield i i += 1
Each time you call
next on this generator, it will return the next number. But you don't have to store a million numbers in memory anywhere - it stores only one number at a time.
Then you learn all the libraries out there, including
Django, which makes it really really easy to make a web server, or
pandas, to do some tough number crunching and data analysis, or
networkx, to analyze graphs and connections, or
scikit-learn, for machine learning, or
pygame, to make a game, and using this myriad of libraries, you can create anything you really want to with Python.
And then, someone says "well, Python is great, but it's slow, and you have the GIL." But you're a Python expert. Python is slow (so it's not used for low-latency systems like trading platforms), but really, Python isn't thaaaat slow. As for the GIL, which limits your programs from using all of the processing power of your computer, stunting the utility of threads/threading, you can now use asynchronous programming as of Python3.5 (