- Tuple is faster than list
- An operation like a_list = a_list + 1 is memory intensive
Search This Blog
Showing posts with label Good programming practices. Show all posts
Showing posts with label Good programming practices. Show all posts
Tuesday, May 27, 2014
Faster commands that improve performance
Sunday, March 16, 2014
import search path
Python looks are several directories when you try to import a module. It mainly searched the directories defined in sys.path. This is a list and you can see it as follows:
>>> import sys
>>> sys.path
['', 'C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\lib\\site-packages\\selenium-2.40.0-py3.3.egg', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']
If you want it to see in other directories, you can make it search it by using the sys.path.insert( 0, new_path ). This is very useful, if you want to import your own custom module.
>>> import sys
>>> sys.path
['', 'C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\lib\\site-packages\\selenium-2.40.0-py3.3.egg', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']
If you want it to see in other directories, you can make it search it by using the sys.path.insert( 0, new_path ). This is very useful, if you want to import your own custom module.
Documentation Strings
Everything between the triple quotes is the function's docstring, which documents what the function does.
A docstring must be the first thing defined in a function (on the next line after the function declaration).
The docstring is available at runtime as an attribute of the function (.__doc__)
""" This is how you declare a doc string.
This even goes to multiple lines.
The docstring ends here. It even ends with a triplequote"""
Assign default values for function arguments
def are_we_going (what_time, go_to_movie="Yes") : print(go_to_movie + ". We are going at " + what_time)are_we_going("five")
>>> Yes. We are going at five
def are_we_going (what_time, go_to_movie="Yes") :
In the above declaration, we have assigned the value of go_to_movie as Yes. This will be the default value, if no value is passed to this argument while calling this function.
Another important thing to note is that the default arguments should follow the non-default arguments. If this rule is not followed, you will get the following error:
SyntaxError: non-default argument follows default argument
Use 'if' to assign a Value
You can use 'if' effectively to assign a value to a variable. See the following example:
The value of go_to_movie is set to Yes, if movie_is_harry_potter=True or else it will be set to 'No'.
Please note that movie_is_harry_potter takes either True / False but not any other value if you are planning to use it in the 'if' statement here.
go_to_movie = Yes if movie_is_harry_potter else No
print(go_to_movie)
The value of go_to_movie is set to Yes, if movie_is_harry_potter=True or else it will be set to 'No'.
Please note that movie_is_harry_potter takes either True / False but not any other value if you are planning to use it in the 'if' statement here.
Subscribe to:
Posts (Atom)