7 Useful Python Tips and Tricks Which Will Save You Heaps Of Google Time

By lumiobyte / Published Aug. 19, 2019, 10:46 a.m. UTC

Anyone who can program also has one other super useful skill: Googling. It’s an essential coding skill, almost as useful as knowing how to write the code itself. But sometimes, always having to Google that same question that you keep forgetting the answer too really sucks. It happened to me. So that’s why I compiled this quick little guide covering the most Googled “how do I do this?” questions in Python! The things that Python tutorials don’t talk much about, but nobody can ever remember. If this guide helps you, add it to your bookmarks as an easy reference. Anyways, without further ado, let’s get right into 6 quick Python programming tips and tricks which will save you heaps of googling time!

TIP #1: QUICK ESSENTIALS:
You should always get the latest version of Python, currently 3.7 at the time of writing
Remember Python values whitespace! You must always tab in anything in a class, function, or if, for, while statements.
You can create a Python comment using # and the comment will span that line
You can create a Python block comment like this:
“““
Block comment here!
You can extend this as much as you like.
”””
Python starts counting at 0! Not 1! Refrence the first item in a list, use yourList[0] not [1]!

TIP #2: PYTHON LISTS
Some of the most common questions are “How do I append to a list?” or “Python add to list”. It’s actually really simple. To append to a list, use:
yourList.append(somevalue)
somevalue can be anything, like a string, an integer, a boolean (True/False), another object or variable, or even another list!

TIP #3: SLEEP FUNCTION (TIME DELAY)
Another common question is “Python sleep” or “How do I make a time delay in Python?”. To do this, you need to import a module called time, included with Python. It looks like this:
import time
time.sleep(1000)
1000 = 1 second, as sleep() takes milliseconds as an argument. Easy!

TIP #4: FOR LOOP (ITERATION)
For loops appear to be the source of some uncertainty too, “how to use Python for loop?”. A for loop is an efficient way to interate through a list of objects like strings, integers, booleans, or whatever you can store in a list, as I mentioned earlier in tip number 1. It works like this:
for item in yourList:
# run checks on the item or do what you want with it
In this case yourList is the list of objects you’re iterating through, and item is the current iteration. You can do whatever you could with item as you could by refrencing it in the list (e.g. yourList[3])!

TIP #5: FOR LOOP (for i in range())
Along with general for loop iteration, a common search query is “Python for loop range”. Using the range function in a for loop is great for iterating a number of times, without a list. It’s like a repeat x times. Works like this:
for x in range(10):
# use x for whatever
In this case, your loop will run ten times. x is the current iteration number. However, Python, like other programming languages, likes to start counting at 0. Therefore, a for loop in range(10) will have x equal to 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 during iterations 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. You can work around this though:
for x in range(1, 11):
# use x
Like this, the for loop will have x equal to 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 for each iteration respectively. Remember, start counting at 0 and end one before the target number!
If you want your loop to jump more than one at a time, or count in reverse, set the step:
for x in range(1, 11, 2):
# use x
It will jump two at a time: 1, 3, 5, 7, 9 for iterations 1, 2, 3, 4, 5. You can also set step to a negative number.

TIP #6: CONVERT STRING TO INT
People seem to really have an issue with this: a common query is “python string to int”. It’s easy! Like this, as long as your string contains ONLY numbers:
stringInt = int(stringNotInt)

TIP #7: DICTIONARY
Dictionaries are difficult at first. At least it appears so - “how to use python dictionary” is a common search. To create one:
myDict = {}
Created! Now to add something to it:
myDict[‘someItem’] = “something, string/int/boolean/object… anything!”
Your dictionary is now this:
{‘someItem’: “something…”}
Dictionaries use JSON format. To access this item:
print(myDict[‘someItem’]) # will print “something…”
To modify it, just do the same thing you did to set it. To add another dictionary within a dictionary:
myDict[‘dictInDict’] = {}
And then access or set or modify a value:
myDict[‘dictInDict’][‘someValue’] = “whatever you want”
print([‘dictInDict’][‘someValue’]) # will print “whatever you want”
Easy, eh?

FINAL NOTES
I hope you enjoyed my new post, and found it useful! Bookmark it, do whatever you want, share it with your coder friends… it would help me a lot! I’ve got a follow up article for this one on the way soon too! If you’ve got any suggestions for this post or something’s incorrect, send me a contact request (available on contact page through the navbar of the site) and I’ll happily edit the article! Thanks for reading!

Subscribe to the mailing list - never miss a post.

* indicates required

This is an advertisement.

Back


Copyright 2019 Lumiobyte/Cockatoo Development Studios. All Rights Reserved.