If you are a programmer and you are looking for work right now you probably know that Python is one of the most sought after job skills for developers today. And with the rise of Machine Learning and AI, demand for Python programmers is just increasing.
Chances are that you go about learning a new language by diving straight into some project and trying to figure it out as you go. That’s a great way to learn, We’re big fans of practical application. It can also be a frustrating way to learn because you keep hitting walls when things don’t go your way. That’s why we give you 5 survival tips to make your transition to Python less frustrating.
1. No Semicolons – No Curly Brackets
Python is known for its simple syntax, in fact a central “pythonic” philosophy is to not use any unnecessary characters. If you are coming from another programming language such as Java or any C dialect you will have imprinted in your spine that every line must end with a semicolon. In Python you can still do that, it will work. But it is not very pythonic.
There are also no curly brackets. There is no need for them because you must always indent your code, which brings us to the next point.
2. Forced Indents
There are two types of programmers in this world. Those who format their code well, and those who don’t. Python enforces us all into good habits, your code simply will not work if you do not indent properly. How to conform? Simply follow that little voice in your head that says “you should have indented this part”.
3. Dynamic Types – Nothing To Declare
Python is dynamically typed, which means you generally do not need to worry about what type your variables are. In fact you can even change them to another type during runtime. Your variables are declared when you assign them and Python figures out the type all by itself. So you can do things like this:
>>> a = "Hello I'm a String" >>> b = 2 >>> type(a) <type 'str'> >>> type(b) <type 'int'> >>> a = 1 + b >>> type(a) <type 'int'> >>> a 3
In the above example a starts out as a str, and is then dynamically changed to an int when it gets assigned the value ‘1 + b‘ (which equals 3).
4. Function definition
Functions are defined like this:
def my_function(self, something): print(something)
Remember how we said Python does not like unnecessary characters and enforces indents? This makes function definitions really simple. Just a ‘def’ and a colon and any arguments in parentheses. No curly brackets, no “private static void blah blah”. Just define your function and un-indent when your function is finished.
The strangest thing about Python function definitions is that the first argument is always “self“. You can call it what you want, but it will always be a reference to the current instance of the class. If the first argument is always self, why do we have to type it?
When you call a function you start with argument number two in the functions argument list, because you would never pass self to the function. So calling my_function from above would look like this:
my_function("hello")
5. Class definitions
Class definitions are even more simple. Again, no curly brackets, no lengthy incantations, just:
class MyClass:
Notice that by convention classnames are written in CamelCase while functions and variables are all lowercase. And don’t forget to indent.
We hope this gives your Python journey a comfortable start, if so please like and share on facebook/twitter/linkedin/anywhere! If there are things you think we should add, please tell us in the comments.
Oh, and before you go, open your Python interpreter and type:
>>>import antigravity