Advanced Python Syntax


The first programming language I learnt is Python. I started learning that by reading and following "Learn Python the Hard Way" (actually a very good book for programming beginners who wants to get really started) and by finishing the Python course on Codecademy - which is the best course it offers by far in my opinion.

The Codecademy Python course introduces basic programming concepts like data types, functions and class (object oriented programming). Unlike other courses it offers - I have went through the HTML/CSS and Javascript/jQuery and PHP course provided there, but these courses are more focused on teaching you how to use a language to do something, while the Python course tries to do something really different - it aims to teach programming as a skill and mindset in general, along with some basic computer science concept, all by teaching the Python language.

I really love Python for its simplistic syntax that is so neat, and its enormous library that has almost every topic and aspect of programs covered.

However, while most of the syntax in Python are straightforward and easy to understand - most of them are really just plain English words that is used in its literal meaning, to really get a hold of the language, some advanced syntax are often seen but their meaning and usage are often not that obvious.

In the list below are syntax that I couldnt understand at the beginning, for their meaning are not that obvious and easy to understand, or are used to do something more advanced like testing and debugging. After coming back from Front End development, I tried to pick up server-side programming with Python again, and now as a more confident coder, I realized that some of the concept behind the syntax that I couldnt understand before becomes more clear now. So, in this post, I am trying to provide some simple explanation on the syntax on this list as reference for myself and other Python beginners like me.

For some of them which I left blank, they are those syntax that I am still not sure what they do, I will come back to them once I figured them out

1. del - unset, remove variable from memory.

    Usage: It is used to delete a variable or item from a script.
                a = 100
                del a    # this will unset the variable a, and it will become not accessible again
               
                a = [1,2,3] #define a new list
                del a[1] #delete item at index 1 in list a
                print a
                >>> [1,3] #2 is removed.

   It can also be used to delete items in dictionaries.

   Also, deleted variable will be removed from the current namespace

2. as = assign alias for imported module/method/variable

Usage: import django as d
            import django.someModule as dm
            print dm.__doc__ <--- this is print the imported django modules docstring

3. global - override default namespacing and force a variable to use a global one, mostly used in functions to access global variables.

Usage:

    a = 100

    def printStuff():
           global a
           print a   <---- print 100

4. assert  - for testing and debugging, it will pause an operation if the valuation returned False. Raise assertion error if asserted statement returned False

Usage:

a = 100
assert a == 100   #nothing will happen
assert(a == 100)    #same as above, different syntax
assert a == 1001

>>> AssertionError

# if the statement after assert keyword returns False, it will raise an AssertionError

5. is - Check if two object is the same.

    Note that this keyword is not used to check the value of an object, but the object itself is exactly the same between two. Useful in object oriented programming.

6. lambda - anonymous function keyword, it is used for assigning ad-hoc function.

     Explained more in another post, but that post is not yet finished.

Syntax to study:

try
except
raise
finally

yield

with
exec
continue
download
alternative link download