• Advertisement

# Howd You Learn Python?

This topic is 3990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi this is a question for anyone who learned Python as their first language. Im curious as to what books you used to learn. Ive found plenty of books that teach the language to people who already know how to program and just want to pick up a new language. I need something that teaches Python to a beginner at programming. Ive found some online tutorials but most seem incomplete. I want something thats all inclusive and has some good exercises so I can actually make programs and get better and not just read.

#### Share this post

##### Share on other sites
Advertisement
Quote:
 Original post by biggjoee5790Hi this is a question for anyone who learned Python as their first language. Im curious as to what books you used to learn. Ive found plenty of books that teach the language to people who already know how to program and just want to pick up a new language. I need something that teaches Python to a beginner at programming. Ive found some online tutorials but most seem incomplete. I want something thats all inclusive and has some good exercises so I can actually make programs and get better and not just read.

I learned by reading Wesley Chun's Core Python Programming. While it assumes you have some programming experience, it starts out pretty simple, really. You might try browsing through it at the bookstore to see if it suits your needs. It covers a lot of territory.

#### Share this post

##### Share on other sites
I used the introductory tutorial on python.org -- there's a bunch of them there.

It has some exercises, and practicing will help out a whole bunch.

#### Share this post

##### Share on other sites
I started out with How To Think Like A Computer Scientist: Learning With Python. Python was not my first language however, although I do think the book would be at least reasonable for a beginner. I also second the recommendation of looking at the tutorials on python.org, they're a pretty good resource.

#### Share this post

##### Share on other sites
Ok Ill check the Pytyhon website as well as the Core Python Programming book. And by the way I have the book "How to think like a computer scientist, learning with python" on my pc. I downloaded it a while ago I just wasnt sure If it was suited for a beginner

#### Share this post

##### Share on other sites
It's cool that you like to learn python. Good luck!
I propose the book "Dive Into Python"...I believe it's a nice book, but if it costs money I won't suggest it, you can learn for free.

Mikle

#### Share this post

##### Share on other sites
Quote:
 Original post by KazgorothI started out with How To Think Like A Computer Scientist: Learning With Python.

I second that document. Its fantastic :-) After this I worked (and bought ;-) ) with "Dive Into Python", which is just a great book :-)

#### Share this post

##### Share on other sites
Yea Im going to work through the book "How to think like a computer scientist.."
Ive been using it for a while now and its pretty good. Can you guys recommend any good exercises for me to do? Maybe some ideas for programs I can create, using what Ive learned so far? I know alot of the Python basics, (variables, operations, functions, booleans, If, Elif, Else, While) I just need some ideas of what I could do to really get sharp at making simple programs. I made programs that were a guessing game where you guess the number the game is thinking, Ive made a 1-10 multiplication table that lines up like a grid, Ive also made a Login type program where you can designate a user and password and the program saves it, then you can "login" with your info. I just want to get better and quicker at this simple stuff. Sometimes, when I want to make something like the programs Ive explained, it drives me insane just to figure out how to begin coding them. Obviously thats normal since im a beginner, but some good exercises should help me alot

#### Share this post

##### Share on other sites
Try a guess the number program where the user guesses a number and the program has to guess it. The computer can play this 'perfectly' with a worst-case of log2 N guesses where N is the range for the number.

#### Share this post

##### Share on other sites
Quote:
 Original post by TheUnbelieverTry a guess the number program where the user guesses a number and the program has to guess it. The computer can play this 'perfectly' with a worst-case of log2 N guesses where N is the range for the number.

This might be because I havent learned enough yet, but Im really not sure of what you mean :)

#### Share this post

##### Share on other sites
Sorry, perhaps I wasn't awfully clear.

You say you did a guess-the-number type program - by this I presume you mean that the computer 'picked' a number at random, and the user then had to guess it - with the computer player giving feedback as to whether the user's guess was too high or low.

My suggestion is to essentially reverse this. Have the user pick a number at random, and then have the computer guess it. Have the computer make attempts at guessing the number, and have the user tell the computer whether or not its guess was too high or too low. If the user cheats by giving conflicting advice (e.g. "Higher than 12" and "Lower than 10"), then admonish them and award the game to the computer. There are algorithms that mean that the computer can actually play this game optimally, whereby it will take O(log2 N) time (assuming of course you restrict the guessed number to the integers ('whole numbers')). I'll show what I mean by this using an example.

Say the user is to guess a number between 1 and 20, inclusive. This gives us a range of 20. Now, the computer should ideally take no more than log2 n (multiplied by a constant - the time it takes to make a guess) time, where n is the range.

So what is log2 n for n = 20, then? Well, most calculators don't have a base-2 logarithm button - but they do have a base-10 log button, and we can use this to work out the base-2 log by dividing the base-10 log of n (n = 20, remember!) by the base-10 log of the base that we want - which, as we've just said, is 2.

This gives us the calculation log10(20) / log10(2). A Google calculator query tells us that this is about 4.3. Now, we know that we can only make complete guesses, so this tells us that if the computer is trying the guess the user's number between 1 and 20, then the computer should hopefully take no more than 5 guesses!

I hope this cleared up my previous post. :-)

#### Share this post

##### Share on other sites
Ok yes that was very clear. The problem is, with my first program, the computer doesnt create a random number, I actually put whatever number it will be in the code. So the actual code has to be changed to play with a different number. Hehe I know thats pretty lame but at the time it was all I knew how to do. I still really dont know how to have the computer generate a random number. Also I think I need to learn more about logarithms, lets just say that when my math teachers taught that stuff, I never actually thought I would use it :)

#### Share this post

##### Share on other sites
import randomimport timerandom.seed(time.time())rnd = random.randint(0, 100) # random number between 0 and 100

Read more on random here.

#### Share this post

##### Share on other sites
thanks alot, I figured Python had something concerning random numbers in its library

#### Share this post

##### Share on other sites
Quote:
 Original post by Mikle3I propose the book "Dive Into Python"
It's freely available online (although it'd certainly be nice if you wanted to support the author by purchasing a hardcopy), but is aimed more at programmers who have experience with another language. It's an excellent book, but would probably be better as a second book or as supplemental material to someone learning Python as a first programming language.

biggjoee5790:
How about taking what Oluseyi has shown you (and looking up the Python documentation if you want a more detailed explanation of it) and modifying your original 'guess the number' program so that the computer generates a random number rather than you hard-coding it in. You could then try TheUnbeliever's suggestion of a program where the computer tries to guess your number instead.

#### Share this post

##### Share on other sites
Quote:
Original post by Kazgoroth
Quote:
 Original post by Mikle3I propose the book "Dive Into Python"
It's freely available online (although it'd certainly be nice if you wanted to support the author by purchasing a hardcopy), but is aimed more at programmers who have experience with another language. It's an excellent book, but would probably be better as a second book or as supplemental material to someone learning Python as a first programming language.

biggjoee5790:
How about taking what Oluseyi has shown you (and looking up the Python documentation if you want a more detailed explanation of it) and modifying your original 'guess the number' program so that the computer generates a random number rather than you hard-coding it in. You could then try TheUnbeliever's suggestion of a program where the computer tries to guess your number instead.

Yes I was actually thinking about modifying mine first. Now even though Im learning from a certain book, even though Im not up to anything about random number generators, its good for me to search for how to do it and break away from the book? I never thought of taking that approach of deciding to make a program, and then finding the necessary skills I need to do it.

#### Share this post

##### Share on other sites
Quote:
 Original post by biggjoee5790Yes I was actually thinking about modifying mine first. Now even though Im learning from a certain book, even though Im not up to anything about random number generators, its good for me to search for how to do it and break away from the book? I never thought of taking that approach of deciding to make a program, and then finding the necessary skills I need to do it.
Well, researching a function (or some piece of syntax, or a particular design pattern when you're a bit more advanced) is an important skill to learn. Even the best programmers can't learn everything about the languages they're using and won't be experienced with every library or API they'll have to use, so it's a good thing to learn how to find the information on a specific thing you want to do.

Now, both approaches are very valid. I know some very good programmers who taught themselves almost entirely by trying to do things and looking up what they needed as they went. The advantage of sticking with the book for now is that it provides structure; when you pick something for yourself to do you might get started and then suddenly find that you need to learn 5 or 6 (or more) concepts all at once, which can be a bit overwhelming as a beginner. Following along with the book means that the excercises you do will be in a logical order that builds on what you already know, and you'll only have to deal with one or two new things to learn at a time.

In this case, you've got a working program that you already understand (your 'guess the number'), and one extra thing you'd like to add into it (using a random rather than hardcoded number), so you'll probably be ok to at least try it out. If you have too much trouble with it or don't understand you can always leave it and continue with the book again.

My recommendation is to stick with your book, but feel free to 'wander around' a bit if there's something a bit different you want to try that you don't think will be too complicated. It's also excellent practice to take the code you write for exercises and experiment with it a bit more, making it do something slightly different. Just make sure that if you do something you havn't covered in your book yet and you don't understand what happens that you either find out (look up other sources of information for a different explanation, or ask for some help if you need to) or remember to come back to it later on when you've learnt a bit more.

Quote:
 Sometimes, when I want to make something like the programs Ive explained, it drives me insane just to figure out how to begin coding them.
You will get better at that. Each concept you learn is like another tool in your toolbox, and at the moment you havn't got all the tools yet and are still getting familiar with the ones you do have. When all you have is a hammer every problem starts to look a bit like a nail, and this can be a bit frustrating - especially if you're still a bit awkward at using the hammer. As you learn a bit more and get more practice at using your existing tools and some new tools to try out you'll find you start to get more of a feel for how to approach things. Part of the fun though is also that there's always still something else you can learn if you want/need to. [wink]

Hope that helps. [smile]

#### Share this post

##### Share on other sites
And just is case you are getting tired of number guessing, you can try the python challenge!
One of the funnest ways to learn python, and enjoyable even to the python code monkeys. Try it, I think last time I got stuck at 16 :( and that's only because of the puzzles...

Mikle

#### Share this post

##### Share on other sites
Thanks for the advice Kazgoroth, its really helpful. Im going to work through the rest of this "How to think Like a Computer Scientist" book. It seems to be helping be a lot and it also has alot of examples and exercises to try out. It covers a lot and has a big section on classes and objects so I can learn the object oriented side of Python. Im hoping that by the time I work through this book Ill be alot more comfortable with programming. Im sure I will use many more books before I really get good or at least be able to work on a game.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 10
• 12
• 10
• 10
• 11
• Advertisement