by Sam » 19 May 2017, 22:17
If you mastered the basics, you might (like me) have assumed you are a Python god who really doesn't need any more Python knowledge to complete most tasks. You know all about OOP polymorphism... that's it isn't there? Well no. Here are 7 tricks to prove there's so much more out there.
(note: random order... as each "trick" came to mind)Feel free to ask questions below! There are no stupid questions.
1.
String formatting is incredible and very useful. The basic formatting allows you to create more readable code. You can also reorder variables, use them more than once, align items, prefix with a sign and more!
Check out this website for all the information.- [+] Spoiler

printing the String produces:

2.
Decorators. You may have come across the @ symbol if you've built web applications in Django or Flask. Effectively a decorator allows a function to be wrapped inside another function declared elsewhere. The other function runs additional, necessary code e.g. in creating a database model class in Django you can use @python_2_unicode_compatible - if you need to support Python 2. These decorators can help keep code concise.
- [+] Spoiler

printing the String produces:

3.
Variable arguments. In Java, R and other languages the convention is to use three dots
... to denote a method accepts a any number of arguments. In Python, the equivalent is the
*. You can always accept a list as a parameter but this makes it clear that the function call is accepting multiple values for the argument. You can use ** to accept a dictionary of arguments e.g. **kwargs.
- [+] Spoiler

produces:

4.
List comprehension. Sometimes you have a list of data and need to alter it in some way e.g. converting everything to upper or lower case. You could loop through but a list comprehension is more concise and slightly more efficient. The basic format is [x for x in some_list if ..], with the test condition being optional. You can also do dictionary comprehension!!
- [+] Spoiler

prints the new list:

5.
Help! If you're like me, you've been coding without using the
helpfunction for too long. I'm sure you learn this as part of any introductory course but it was only when using the R equivalent
?functionName (which is admittedly much better) that I remembered the same thing exists in Python. Simply use help().
- [+] Spoiler

prints:

6.
With. The
with keyword helps manage object resources by destroying them after they
have been used. You could do file_variable = open("filename", "w") and then file_variable.close() but you can easily forget to close the file. The with keyword creates concise code. There are other uses for with other than opening and closing a file. Any time you keep a resource open (loaded in memory) and then close it is a case for a with statement. See here:
http://stackoverflow.com/questions/1984 ... r-and-exit which has a nice example of using the necessary special methods to create a database connection class which can be used with
with.
- [+] Spoiler

outputs:

7.
Dictionary Key Lookup with .get(). When you lookup a key that does not exist, you receive a KeyError and your program terminates. You could place the lookup inside an IF statement (If key in dictionary: ) but we can also use the .get() method to nicely handle this.
- [+] Spoiler

prints:

[center][img]http://i.imgur.com/dGHZeIw.png[/img][/center]
If you mastered the basics, you might (like me) have assumed you are a Python god who really doesn't need any more Python knowledge to complete most tasks. You know all about OOP polymorphism... that's it isn't there? Well no. Here are 7 tricks to prove there's so much more out there.
[size=75](note: random order... as each "trick" came to mind)[/size]
Feel free to ask questions below! There are no stupid questions.
[size=150][b]1[/b][/size]. [size=150]String formatting[/size] is incredible and very useful. The basic formatting allows you to create more readable code. You can also reorder variables, use them more than once, align items, prefix with a sign and more![size=150] Check out this [url=https://pyformat.info/]website[/url] for all the information.[/size]
[spoiler][img]http://i.imgur.com/JCvfmXn.png[/img]
printing the String produces:
[img]http://i.imgur.com/VTDvWCS.png[/img][/spoiler]
[size=150][b]2[/b][/size]. [size=150]Decorators[/size]. You may have come across the @ symbol if you've built web applications in Django or Flask. Effectively a decorator allows a function to be wrapped inside another function declared elsewhere. The other function runs additional, necessary code e.g. in creating a database model class in Django you can use @python_2_unicode_compatible - if you need to support Python 2. These decorators can help keep code concise.
[spoiler][img]http://i.imgur.com/GsHoIRa.png[/img]
printing the String produces:
[img]http://i.imgur.com/nYdy2n9.png[/img][/spoiler]
[size=150][b]3[/b][/size]. [size=150]Variable arguments[/size]. In Java, R and other languages the convention is to use three dots [b]...[/b] to denote a method accepts a any number of arguments. In Python, the equivalent is the [b]*[/b]. You can always accept a list as a parameter but this makes it clear that the function call is accepting multiple values for the argument. You can use ** to accept a dictionary of arguments e.g. **kwargs.
[spoiler][img]http://i.imgur.com/Zjl2FyB.png[/img]
produces:
[img]http://i.imgur.com/8i6MzEy.png[/img][/spoiler]
[size=150][b]4[/b][/size]. [size=150]List comprehension[/size]. Sometimes you have a list of data and need to alter it in some way e.g. converting everything to upper or lower case. You could loop through but a list comprehension is more concise and slightly more efficient. The basic format is [x for x in some_list if ..], with the test condition being optional. You can also do dictionary comprehension!! :o
[spoiler][img]http://i.imgur.com/nObVjzZ.png[/img]
prints the new list:
[img]http://i.imgur.com/U3WC7pO.png[/img][/spoiler]
[size=150][b]5[/b][/size]. [size=150]Help![/size] If you're like me, you've been coding without using the [b]help[/b]function for too long. I'm sure you learn this as part of any introductory course but it was only when using the R equivalent [b]?functionName[/b] (which is admittedly much better) that I remembered the same thing exists in Python. Simply use help().
[spoiler][img]http://i.imgur.com/MN6WWSw.png[/img]
prints:
[img]http://i.imgur.com/vP8HR1B.png[/img][/spoiler]
[size=150][b]6[/b][/size]. [size=150]With[/size]. The [b]with [/b] keyword helps manage object resources by destroying them after they
have been used. You could do file_variable = open("filename", "w") and then file_variable.close() but you can easily forget to close the file. The with keyword creates concise code. There are other uses for with other than opening and closing a file. Any time you keep a resource open (loaded in memory) and then close it is a case for a with statement. See here: http://stackoverflow.com/questions/1984325/explaining-pythons-enter-and-exit which has a nice example of using the necessary special methods to create a database connection class which can be used with [b]with[/b].
[spoiler][img]http://i.imgur.com/MRRjEPl.png[/img]
outputs:
[img]http://i.imgur.com/bROBid6.png[/img][/spoiler]
[size=150][b]7[/b][/size]. [size=150]Dictionary Key Lookup with .get()[/size]. When you lookup a key that does not exist, you receive a KeyError and your program terminates. You could place the lookup inside an IF statement (If key in dictionary: ) but we can also use the .get() method to nicely handle this.
[spoiler][img]http://i.imgur.com/5AnhKs9.png[/img]
prints:
[img]http://i.imgur.com/XaysG2u.png[/img][/spoiler]