Setting out to learn Python. Anyone wanna join?

Grumpicus' boss (and sole contributor to one of my side projects) gave me a spare license for PyCharm at work today. I highly recommend it. I was telling them to shut up and take my money after 3 minutes using it.

Oh!!! I meant to add that I like the Enthought Python setup just for ease of use. I'm not sure it's well-suited to robust development but should make tinkering fairly easy. It manages dependencies for you, for packages it includes, and it seems to have most of what an early tinkerer (like me) would need.

I don't know about how or if Enthought includes or works with virtualenv but it should be easy to sort out. I haven't actually used Enthought, but use Python on Linux and use pip and virtualenv.

Bonus_Eruptus wrote:

Grumpicus' boss (and sole contributor to one of my side projects) gave me a spare license for PyCharm at work today. I highly recommend it. I was telling them to shut up and take my money after 3 minutes using it.

I love anything JetBrains does. Those guys are really smart. I use WebStorm for all my Javascript/HTML/CSS stuff, it is fantastic. At work, we use TeamCity for our CI builds, I am trying to convert us to use YouTrack for our bug database, and when I do C# I have my handy copy of ReSharper which is great too. If only I could love IntelliJ, but it hates me for some reason.

I tried PyCharm when it was first released and even back then it was quite good, I can only imagine how good it is now.

Sadly, I don't do enough Python these days to justify dropping a Franklin on it.

Katy wrote:

It's mostly getting to know my way around the library and the available types. I understand why the codeacademy class introduces methods and function calls at the same time, but because it's set up to introduce things on a just-in-time basis, it's a little harder for me to build the appropriate mental model of the library.

The early lessons go through the basic data types, and introduce some methods of the string class as well as some general-purpose functions that take a string as input. (e.g. string.upper() and len(string) ). I keep mixing them up, and trying to call string.len() or upper(string). At some point I need to spend a little more time looking over the documentation and getting a bit more of it inside my head.

Gotcha. Yeah you just have to remember that stuff. Fortunately, there's really not that many to remember any more. len and open are the most nonsensical ones still around. Most everything else is type conversions or "primitive" creation (byte(), str(), int(), range(), etc.), functions operating on iterables(zip(), all(), map(), etc.) , or reflection functions (callable, isinstance, issubclass, etc.).

KingOctavious wrote:

EDIT: I'll add that I think you'll like Codeacademy. One of the things that I struggled with on Learn Python the Hard Way is how often he goes, "Now go Google this and learn about it on your own." I can understand his reason for doing so, but with all I have going on on a daily basis, I reeeaaaally struggle to go off and do my own research on things like that.

Agreed! I'm about 60% through the Python course on Codecademy now, and I really like it a lot more. I mean, I've gotten some sweet badges.

Well, starting with zero python experience and a broken and bloated project I inherited (would it have killed him to comment just a little), I managed to build a UI streaming data from a Bluetooth device, parsing (ugh json parser irritations), processing the data and updating multiple live plots and numerical displays. And it worked in our presentation!

I'm pretty impressed with Python as an analysis tool. Once I got used to it I started to tear through my work which was great. I don't find the "readability" goal is always upheld very well, but a google sorts things out quickly. Two things that really got me were a very irritating json streaming thing, where a string like 0032 would but it out due to it not being able to parse the 0s. The second was the fact that data1 = data does not copy the data, but creates a new reference to it! So confused for a while by that one, but ill chalk it down to experience.

Eva Earlong wrote:
KingOctavious wrote:

EDIT: I'll add that I think you'll like Codeacademy. One of the things that I struggled with on Learn Python the Hard Way is how often he goes, "Now go Google this and learn about it on your own." I can understand his reason for doing so, but with all I have going on on a daily basis, I reeeaaaally struggle to go off and do my own research on things like that.

Agreed! I'm about 60% through the Python course on Codecademy now, and I really like it a lot more. I mean, I've gotten some sweet badges.

Haha, dem badges. There's something so stupidly satisfying about them.

I've been doing the code academy all morning. It is pretty sweet. A little annoying when one can't spell battery and it won't accept my variable name is all!

dibs wrote:

I've been doing the code academy all morning. It is pretty sweet. A little annoying when one can't spell battery and it won't accept my variable name is all!

Yeah I've had a few cases like that. I'm doing PHP on there also, and this morning it was rejecting a foreach statement because I was using variable $n instead of $i. Other than that, though, Codeacademy is still my favorite of the sites I've tried.

How's everyone's learning coming along? Mine is slow but steady. I've also started php, which is turning out to be extremely similar to Python. I imagine now that there are core attributes that most programs share, but I had never really considered that.

I've also recently started coursera's comp science 101, which I'm hoping will give me a broader understanding of programming in general. So far the class has been perfectly paced. Starts off very basic, but the instructor does a great job of explaining everything.

Can't wait for the next coursera python class to begin. March 24 can't come soon enough. It is too bad there isn't a self paced version or an option to download the lecture videos.

fangblackbone wrote:

Can't wait for the next coursera python class to begin. March 24 can't come soon enough. It is too bad there isn't a self paced version or an option to download the lecture videos.

Interesting, I may sign up for that.

On a side note, I just finished Codeacademy's PHP training. It was good, but I feel like it ultimately left a bunch of loose ends. It never explained the difference between print and echo for instance, and it never covered the process of getting information from users. I'm guessing the Python tutorials will be more complete, though, because it seems much longer. Also, some of the PHP stuff might be hard to get across with the single-screen format of Codeacademy.

Had to quit the coursera course. 70 hour work weeks intruded. Want to pick it up again soon.

If you are looking for programming things to do while you wait on coursera, try some project euler stuff.

Tagging into this thread. I started on Python with my older son, using the Invent with Python online books and decided to run through the Code Academy tutorial myself.

I made it to the 60% mark in the Python tutorial over the weekend. I came acrossCheck iO which looks to have good challenges and a supporting community. The first challenge sent me off on an hour long RegEx training session this morning. I'd only dipped my toe into RegEx before so this was a great reason to get my feet wet.

Now you have two problems.

Submitted for your commentary. This is the Codecademy "digit_sum" challenge, where you're supposed to sum the individual digits of an integer. My method feels like cheating.

Spoiler:
def digit_sum(x): total = 0 strX = str(x) for i in range(len(strX)): total += int(strX[i]) return total
LiquidMantis wrote:

Submitted for your commentary. This is the Codecademy "digit_sum" challenge, where you're supposed to sum the individual digits of an integer. My method feels like cheating.

Spoiler:
def digit_sum(x): total = 0 strX = str(x) for i in range(len(strX)): total += int(strX[i]) return total

Nothing's cheating, even using libraries.

Spoiler:
def digit_sum(s): return sum([int(x) for x in s if x.isdigit()])

Can't you pass generators to sum?

LiquidMantis wrote:

Submitted for your commentary. This is the Codecademy "digit_sum" challenge, where you're supposed to sum the individual digits of an integer. My method feels like cheating.

Bonus_Eruptus wrote:

Nothing's cheating, even using libraries.

Indeed! Cheating is good programming. Laziness can be good be programming. However, avoid "cleverness". It's tempting to be clever, but really good code is as simple as possible (but no simpler), so that others can read and understand it.

A couple of specific things to mention here:

>>> strX = str(x)

Python is a loosely-typed language, so we as Python programmers should generally avoid explicitly dealing with typing. We are more interested in what a thing can do than what a thing is, so it's perfectly fine to just say:

>>> x = str(x)

What you are really going for here though is not "turn x into a string", but "turn x into something I can iterate over, one piece at a time".

Casting the number to a string is a great idea to get closer to something that's iterable. You could go even further and cast it again to a list. If you cast a string to a list in Python you get something that's perfect for your goal:

>>> str(2468)
'2468'

>>> list('2468')
['2', '4', '6', '8']

So maybe you could use that combination to make your code more concise and more readable at the same time?

I see that I have added an extra step in the interests of pedagogic completeness.

It seems that a string is already iterable in this way.

>>> for char in "abc": print char
...
a
b
c
>>> for char in list("abc"): print char
...
a
b
c

Bonus_Eruptus wrote:

return sum([int(x) for x in s if x.isdigit()])

Ints aren't iterable.... I thought something looked funny about that, but didn't check until now, prompted by Jarpy's comment about wanting to get X into an iterable form. That function will add the digits of a string, but it breaks if you pass it an int.

This form works with an int:

return sum([int(x) for x in str(s) if x.isdigit()])

but I have no idea what would happen with most other variable types.

Malor wrote:
Bonus_Eruptus wrote:

return sum([int(x) for x in s if x.isdigit()])

Ints aren't iterable.... I thought something looked funny about that, but didn't check until now, prompted by Jarpy's comment about wanting to get X into an iterable form. That function will add the digits of a string, but it breaks if you pass it an int.

This form works with an int:

return sum([int(x) for x in str(s) if x.isdigit()])

but I have no idea what would happen with most other variable types.

Yeah, that's what I get for posting at stoplights. Wrap that 's' in a str(), or wrap the argument in str() when calling it.

Yeah. Just a heads up to self-appointed tutors like Bonus, Malor and Me:

We can do the most good if we keep our comments aligned with the material. For example, list comprehensions are cool but they're really out of scope for what we're trying to teach/learn here.

Okay, the Coursera Codecademy* course is just now getting to generators and that syntax. This is part 5 of the "Advanced Topics in Python" section.

[Edit] *This is the Codecademy program. I'm waiting on the Coursera one to start back up.

It's difficult to review and comment without knowing the course and where the person is in the material though. In this particular example it would've been the perfect answer at this point in my study as I'm at list comprehensions, slicing, and lambdas. At the time I asked I didn't have that syntax though, and some of the earlier exercises they specifically want you to take longcuts like iterating through a list to sum it rather than just using sum(). The Codecademy course really beats in loop structures.

It's also good to see the "right" way to do it for those of us that might want to take Python, or programming in general, further and aren't familiar with good/idiomatic programming techniques and structures. My limited Pascal, C, C# background has me wanting to do things a bit more verbose than what is apparently considered "Pythonic".

Jarpy wrote:

Yeah. Just a heads up to self-appointed tutors like Bonus, Malor and Me:

We can do the most good if we keep our comments aligned with the material. For example, list comprehensions are cool but they're really out of scope for what we're trying to teach/learn here.

It's never to early to learn the iterator/iterable divide.

Well, that's the end of the Codecademy Python course for me. Now time to see if I'm ready for CheckiO or not.

LiquidMantis wrote:

It's also good to see the "right" way to do it for those of us that might want to take Python, or programming in general, further and aren't familiar with good/idiomatic programming techniques and structures. My limited Pascal, C, C# background has me wanting to do things a bit more verbose than what is apparently considered "Pythonic".

Case and point (CheckiO Challenge #2 spoilers):
The challenge is to remove the unique integers from a list (e.g. [1,3,4,1,5,4] -> [1,4])

My solution:

Spoiler:
def checkio(data): newData = [] for datum in data: if data.count(datum) > 1: newData.append(datum) return newData

And the popular published solution:

Spoiler:
checkio=lambda d:[x for x in d if d.count(x)>1]

I'm now at the point where I understand that solution, I just don't immediately think of it and it seems like Perl levels of terseness.

Well, your solution works, and it's very readable. It would be easy for someone else to maintain.

Python is a slow language no matter what, so trying to be terse in order to save execution time is frequently silly. If you really want it to run fast, you should probably be in a different language to begin with.