ForumCoin Special Offer: Free ForumCoin Exchanges Close
Forum Coin is an online community where you earn Forum Coin for making posts and referring other members. You can use that Forum Coin as a virtual currency to buy and sell goods and services and to exchange for traditional, real world currencies. Find out more!
Get paid up to 150 ForumCoin to submit your article.

7 Python Tricks for Intermediate Python Programmers

Postby Sam » 19 May 2017, 22:17

Image


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
Image

printing the String produces:
Image



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
Image

printing the String produces:
Image


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
Image

produces:
Image



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!! :o

[+] Spoiler
Image

prints the new list:
Image


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
Image

prints:

Image


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
Image

outputs:

Image


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
Image

prints:

Image
  • 1

User avatar
Sam
Community Moderator
 
Posts: 1,541
Location: New Zealand
Referrals: 12
Reputation: 511
ForumCoin: 1,414

Re: 7 Python Tricks for Intermediate Python Programmers

Postby Fergal » 20 May 2017, 09:50

That's a great article Sam, thanks for sharing it with us. I'm planning on going through the Python 3 tutorials on the New Boston soon. Once I have those done, I might be ready to benefit from your tips :)
  • 0

User avatar
Fergal
Site Admin
 
Posts: 5,828
Location: Ireland
Reputation: 1007
ForumCoin: 16,655

Re: 7 Python Tricks for Intermediate Python Programmers

Postby Sam » 21 May 2017, 04:04

Fergal wrote:That's a great article Sam, thanks for sharing it with us. I'm planning on going through the Python 3 tutorials on the New Boston soon. Once I have those done, I might be ready to benefit from your tips :)


I would not personally recommend thenewboston; mostly because it seems like his level of understanding isn't really that of an "expert" (although that could just be how he teaches). (see here and here for others views)

I would highly recommend:
https://pythonprogramming.net/ - so many things to learn here depending on what you're interested in, after you've learned the basics. Harrison is undoubtedly an exceptional programmer and you get to see his train of thought while he works through problems (in some of his more experimental videos). This is one of his cool projects: http://sentdex.com/.
Derek Banas - Learn Python in One Video. This is great for revision and understanding the fundamentals. I'm really hoping he'll put out a video on R before my exam. :P
  • 1

User avatar
Sam
Community Moderator
 
Posts: 1,541
Location: New Zealand
Referrals: 12
Reputation: 511
ForumCoin: 1,414

Re: 7 Python Tricks for Intermediate Python Programmers

Postby Fergal » 24 May 2017, 17:40

Thanks for those great resources Sam, they will be very helpful.

Those are interesting opinions on TheNewBoston. I find his videos really simple and they make it easy (for me) to understand programming concepts that are new to me. When going through other tutorials that may be more technically correct, I find it much more difficult to comprehend new programming concepts.

For example, I recently went through Bucky's video tutorials on C programming. They just cover the basics, but are really easy to follow. The way he covers pointers is really straightforward and easy to understand. Now that I have the basics I am going through the more difficult C tutorials on cprogramming.com. The fourth cProgramming tutorial covers functions and honestly I found it quite difficult. However, once I watched Bucky's videos on Functions, then went back to the cProgramming functions tutorial, it was much easier to get through it.

I will most probably go through his Python videos to get a quick basic understanding, then the resources you recommend Sam, to get a more complete understanding, with better coding habits.

Thanks again Sam.
  • 0

User avatar
Fergal
Site Admin
 
Posts: 5,828
Location: Ireland
Reputation: 1007
ForumCoin: 16,655

Re: 7 Python Tricks for Intermediate Python Programmers

Postby skysnap » 24 May 2017, 17:53

I think for R programming there is Datacamp. Their guided tutorials are really good. You can check out the following R Programming Video Tutorials.
  • 1

User avatar
skysnap
 
Posts: 4,586
Reputation: 358
ForumCoin: 1,335


Return to Articles & Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

Reputation System ©'