Hacker News new | comments | show | ask | jobs | submit login
Learn Python in minutes (learnxinyminutes.com)
158 points by jurajpelikan 1545 days ago | hide | past | web | 55 comments | favorite



"""-delimited text is not actually a comment. Its a multiline string literal

    x = """aaa
    bbb"""
And you can also use single quotes if you want.

    x = '''aaa'''
The reason this might have been confused with a comment is because triple-quoted strings are often used for documentation strings, a Python feature that gives special meaning to string literals that occur as the first statement in a function or class. Those strings get associated to the function as its documentation and can even be inspected at runtime:

    def foo():
       "my documentation string"
       return 17

    print help(foo)


This is exactly what I wanted a few years ago while working on a GAE project. I had never touched Python but I knew programming and just wanted to know what the language had and how in basic terms to use it.

I remember picking up a large book from a store shelf titled something like "Python for Programmers". Thinking, "Oh, this is for people who already know how to program and just want to learn Python". I open the book randomly and start reading it's explanation on what functions are in programming and why they're useful. I sighed and placed the book back on the shelf.


An error: *args arrive as a tuple, not a list, so the first line that all_the_args(1, 2, a=3, b=4) prints is not:

    [1, 2]
but is instead

    (1, 2)
The second line of that output also happens to be wrong, because printing a dictionary uses repr() on its keys and values and str.__repr__ normally uses single quotes, so instead of

    {"a": 3, "b": 4}
It will instead print

    {'a': 3, 'b': 4}
For that matter, I would prefer it if this guide used single quotes primarily; that is the preferred standard in Python.


Nice and concise. It would be complemented nicely by a list of Python "gotchas".

E.g., off the top of my head, mutable default arguments, or trying to catch multiple exceptions with "except Exception1, Exception2:". That's the kind of thing people new to the language can spend a lot of time debugging when they first run into it.

Edit: and any introduction to tuples should probably show how to write empty and one-element tuples, since that can be non-intuitive. Writing (1) instead of (1,) is also a fairly common gotcha.


Author here.

I didn't want to overload the reader. Perhaps a more in-depth guide is required.


Hey, thanks for the guide. It's a concise read :) looking forward to the article on Python 3.


"Learn X in 30 Days" has gone to "Learn X in Minutes"

I am waiting for "Learn X While You Sleep--wake up as an expert!"


I used to sleep with a walkman plugged in and playing german vocab - everyone knew that was guaranteed to work


I put my computer on youtube's google io talk whenever I can't get to sleep. It sures put me to sleep fast. :-)

If Michael Jackson just knew that......


...he would still be alive , lol !


This looks very similar to the notes I was taking on my own when going through "learning python the hardway". Yours are much better and easier to read though, and make more sense in terms of going from very easy to more complicated in staggered steps.

Great stuff!

As a sidenote, how does everyone take note (if you even do) when learning something new like a language or some kind of skill?


I don't take notes.

I learn better when I read the same thing over and over from different sources, which might have additional details or different points of view.

Not taking notes forces me to go back when I don't remember something, often finding new sources.


What taking notes does for me -- and I assume most other people -- is that it forces me to internalize what I just learned in a way that makes sense to my brain and fits within previous constructs I've already developed through n years of living.


I make guesses about what the result of something will be, check the guesses to see if they work and then write down why I think they worked or didn't work.


What this needs is Rap Genius style annotations, so people can make comments and additions. I recently (re:yesterday) decided to pick up and try out python for a small project and this is a great quick and dirty start.


I think you mean to say "glance over and quickly forget the basics of python in minutes"


Author here.

How would you improve the retention rate?

This article is meant for programmers from other languages who want to quickly look up a language specific detail.


Wow. I've only every programmed Java and this looks amazing. I'm so sorry scripting languages, I've neglected you!


I've been forced to use C# for the last week. I'm so sorry scripting languages, I'll be back soon! I miss you!


While I see value in a cheat sheet that helps you remember a language's features, using the words 'Learn X in minutes' is a bit stretching it. It takes a good few weeks to get comfortable building something in a new language.

This is a good cheat sheet.


obligatory norvig: http://norvig.com/21-days.html

but all snark aside, good work so far! like the concept.


Having just recently learned Python the "hard" way, I really wish I would've seen this beforehand. Even having done some hands-on work and a lot of Googling, I picked up a few new things from this I hadn't seen before. Thanks!


Same here, I just went through the same course. (Learn Python the hard way).

Coming from a php background I had (and am having) problems wrapping my head around the packaging section. I started to lose interest because of it. It's as if I hit a wall.

Can any Python hackers offer some good resources for learning more about packaging?


Hey Zed here, are you talking about exercise 46?

http://learnpythonthehardway.org/book/ex46.html

If you email me with the problems you had I'll see what's wrong with the exercise and figure it out. There was a particularly stupid ordering problem with it where I had people install requirements after they needed them, so go double check it again and see if it got better.

Also, email me help@learncodethehardway.org with what you ran into. If you're in San Francisco I'll even meet you in person to figure out what you're doing live (works way faster).


Are you trying to use packages or create them?


I'm trying to create packages.

I haven't been able to find a decent tutorial on creating packages. That particular section in "Learn Python the Hard Way" was hard for ME to follow. I was able to create a package but only after finding a link on stackoverflow question that then redirected me to a github page containing a sample package from an individual who also had problems. He states that his package worked but he wasn't sure if he created the package correctly.

I've tried reading the python documentation but I'm too dumb to understand some of the language. (No CS background, just a regular dude and a computer here.)


Author here.

That would've been impossible because I wrote that 2 days ago =]


This existed as of two days ago, so don't feel too bad :)


<3 Reblogged on my personal and followed, for great justice.


The bit about exceptions, using a comma instead of as, should just be left out. I don't imagine anyone starting out today is using Python < 2.6, and having both in the document is confusing.


I added that after the fact because someone did try to run the thing on python 2.4, got an error and was confused about it


Responded in the ticket, but yeah, totally your call (obviously :D)


I added a couple of lines to fix errors I ran into with Python 2.7.3:

Add about line 89, before assignment to some_var,

    a, b = 3, 4
Added a try-except around line 186, to guard the KeyError,

    try:   
        filled_dict["four"] #=> KeyError
    except KeyError:
        print "Raises a KeyError"
Added a definition of foo around 320, so calls would work

    def foo(*seq, **dict): pass


int, boolean, float etc. are not _primitive_ data types in python. If you say that, you really have no idea, what you are talking about. They are objects like everything else. You can inherit from them and do all the good OO stuff.

Sorry for the nitpicking, but calling them primitive types, is just plain wrong.


Author here.

Sorry for that. I know they are objects but if you consider everything an object then really you have no primitives except object itself. Certainly this has a place in programming philosophy but not really a helpful point in a language-primer/cheat sheet.


I agree that "everything is an object" doesn't mean much but "primitive types" has a strong meaning when it comes to OO programming so a different term might be more appropriate.


Why not call them "built-in" then?


I'm usually a php guy, and recently I had to use some vendor's python api to do some data integration and this might have come in handy. I picked up python, or atleast the basic syntax in roughly 1 day, and was done with my implementation within the next day.


This might be even more useful as an ipython notebook so that people can modify and execute any of the examples. The easiest way to host a notebook that users can try from their browser seems to be Wakari (public viewing, free signup needed to modify/run).


I don't know the efficacy of this as an educational tool, but as a Python programmer, it's a really handy "cheat sheet" to have around whenever doing general scripting. It's really well written as well.

Kudos to whomever wrote it.


Author here.

Glad you find it useful!


I would love a version of this that is incredibly comprehensive. I mean like, go over the entire Python doc in this format. Don't get me wrong, this goes over a lot, and but there's still a lot missing.


What does he mean by "# Tuples are created by default if you leave out the parentheses".

With or without the parens I'm getting the same result for "a, b, c = (1, 2, 3)"


Yes, that is the same with or without parentheses. You're creating a tuple on the right-hand side of the equal sign, and using tuple unpacking on the left-hand side to assign the members of that tuple to variables. To make it a bit more clear, you can add parentheses on the left to match the ones on the right:

    (a, b, c) = (1, 2, 3)


You get a list if you do "object_a = [1, 2, 3]" and a tuple if you do "object_b = (1, 2, 3)". Then try "object_c = 1, 2, 3" and you'll also get the tuple.


You should also mention the 1-item case where the (optional in other cases) trailing comma is required to form a tuple: (1,)


One thing is missing: package importing and the related gotchas.


And something I wrote seven years ago, for Y = 10:

http://www.stavros.io/tutorials/python/


awesome tutorial. if you need a free Python IDE (based on VS) to try the exercises in, we just released PTVS 2.0 Beta: http://pytools.codeplex.com.

for you gurus, it's got a cool new feature: debugging Python & C++ together: http://www.youtube.com/watch?v=wvJaKQ94lBY



Looks like Java won't make it in this part of town.


I love this. Do it for ruby pretty pretty please?


I'm working on a ruby version, the WIP can be seen on this pull request: https://github.com/adambard/learnxinyminutes-docs/pull/18


Shakes body in chair

"I know kung-fu"


li[-1] #=> 3




Applications are open for YC Winter 2018

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: