Learning Python

I recently started learning Python again, as I’ve had interest in it in the past, but stopped for some reason I don’t really remember.

I don’t follow a single source, I make use of videos, books, and random googling for answers.

I was following the MIT course I mentioned in an earlier post, Google’s Python class, and two books, “A Byte of Python“, and “Dive into Python“.

And here’s some of the things I learned:

Why this code doesn’t work in python

[sourcecode langauge=”python”]
x = 11
print(x += 5)
[/sourcecode]

While this does

[sourcecode language=”python”]
x = 11
x += 5
print(x)
[/sourcecode]

The good people over at Stack Overflow answered this question, you’ll find the answer for this question here.

I also learned slicing, which works for strings, tuples and lists as far as I know.

A cool trick which can be done with slicing is quickly reversing a string :

[sourcecode language=”python”]

s[ : :-1] #assuming that s is a string.

[/sourcecode]

Another feature in Python is the “in” operator, it checks data structures for a given element.

In Java, if you wanted to find an element, you had to use one of the binarySearch methods, or iterate over the elements of the collection and check manually.

The last thing I remember is copying and clearing a list.

[sourcecode language=”python”]

list = [ 3, 5, 9]

another_list = l[:] #This creates a copy of the list, rather than another reference to the same list.

another_list[:] = [] #This clears all elements from the current list.

del list[:] #This also deletes all elements from a list.

[/sourcecode]

I’m considering doing some exercises to familiarize myself further with the language, I’m thinking of using CodingBat or something similar.

I also read a little about Android development, and I think I’m getting the hang of it, even though I don’t really like dealing with XML files at all.

Leave a Comment

Your email address will not be published. Required fields are marked *