Thursday, November 07, 2013

Python Basic Tips


All of the following contents are from http://www.codecademy.com/tracks/python?jump_to=4fd50d26d7fc680003030bd9




---------------------------------------------------------------------------------
You can use

string.split()
# and
" ".join(list)
to help you here.

Remember:
"*" * 4 equals "****" //<- We can use * operator dealing with strings.

After splitting the string with string.split(), you can loop through the indices in the list and replace the words you are looking for with their asterisk equivalent. Join the list at the end to get your sentence!

---------------------------------------------------------------------------------


print text,1 text2, text3 for consecutive strings in the same line

---------------------------------------------------------------------------------

The easiest way to approach this problem is to create a new list in your function, loop through your input list, and add items from your input list to your new list if the current item is not already contained in your new list. Using the a not in b syntax might help you here.

Also, note that destructively modifying a list while you are looping through it is bad practice and will likely lead to bugs somewhere down the line! That's why we always make a fresh copy to work on.
---------------------------------------------------------------------------------
List Slicing
If you only want a small part of a list, that portion can be accessed using a special notation in the index brackets. list_name[a:b] will return a portion of list_name starting with the index a and ending before the index b.

If you tell Python my_list = [0, 1, 2, 3], then my_list[1:3] will return the list [1, 2], leaving the original list unchanged! Check it out:
---------------------------------------------------------------------------------
You can search through a list with the index() function. my_list.index("dog") will return the first index that contains the string "dog". An error will occur if there is no such item.

Items can be added to the middle of a list (instead of to the end) with the insert() function. my_list.insert(4,"cat") adds the item "cat" at index 4 of my_list, and moves the item previously at index 4 and all items following it to the next index (that is, they all get bumped towards the end by one).
---------------------------------------------------------------------------------
If your list is a jumbled mess, you may need to sort() it. my_list.sort() will sort the items in my_list in increasing numerical/alphabetical order.

It's worth noting that sort() does not return a new list; instead, your existing my_list is sorted in place (the sorted version replaces the unsorted version).
---------------------------------------------------------------------------------
Let's start with iterating over a dictionary. Recall that a dictionary is just a collection of keys and values; the Python function items() will iterate over a dictionary and return those key/value pairs. For instance, for the dictionary

d = {
    "Name": "Guido",
    "Age": 56,
    "BDFL": True
}
Calling

d.items()
Will result in

# => [('BDFL', True), ('Age', 56), ('Name', 'Guido')]
Note that the items() function doesn't return key/value pairs in any specific order. (For more on this, see the Hint.)

---------------------------------------------------------------------------------
For example, the median of the sequence [7,3,1,4] is 3.5, since the middle elements after sorting the list are 3 and 4 and (3 + 4) / (2.0) is 3.5.

You can sort the sequence using the sorted() function, like so:
-------------------------------------------------------------------------------
evens_to_50 = [i for i in range(51) if i % 2 == 0]
print evens_to_50
-------------------------------------------------------------------------------
<lambda function>
-------------------------------------------------------------------------------
my_string = "Monty Python"
# => Monty Python
my_string = my_string[:-7]
# => Monty   
-7 means that 7 left from the last

-------------------------------------------------------------------------------
The 'with' and 'as' Keywords
Programming is all about getting the computer to do the work. Is there a way to get Python to automatically close our files for us?

Of course there is. This is Python.

You may not know this, but file objects contain a special pair of built-in methods: __enter__() and __exit__(). The details aren't important, but what is important is that when a file object's __exit__() method is invoked, it automatically closes the file. How do we invoke this method? With with and as.

The syntax looks like this:

with open("file", "mode") as variable:
    # Read or write to the file
------------------------------------------------------------------------------
Python log system
http://stackoverflow.com/questions/8573023/python-is-there-no-better-way-to-get-the-expression-in-a-debug-function




No comments: