Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

About learning Python

Options
  • 25-07-2011 11:10pm
    #1
    Registered Users Posts: 914 ✭✭✭


    Hi all,
    I'm currently learning Python by using the video tutorials uploaded by "thenewboston" on youtube. The link to the playlist is here. I'm just wondering after I've completed these tutorials is there much more to learn in Python, or is this very basic stuff and I should learn more. If so, could so point me to a suitable website/book please? I'm on the 20th tutorial at the moment and finding it pretty easy so far. I've tried C++ before and kind of got fed up of it so I decided to start off fresh with a easiest easier language. BTW I'm 15 so this hasn't anything to do with career (or in the future maybe) but merely just for a hobby.

    Thanks,
    James.


Comments

  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Have you tried Learn Python the Hard Way?


  • Registered Users Posts: 1,419 ✭✭✭Cool Mo D


    How to think like a computer scientist is a well written, and free introduction to programming in python. It's fairly comprehensive, and has exercises at the end of each chapter. It was originally written as a college-level textbook for new programmers.


  • Registered Users Posts: 3,721 ✭✭✭E39MSport


    +1


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Cool Mo D wrote: »
    How to think like a computer scientist is a well written, and free introduction to programming in python. It's fairly comprehensive, and has exercises at the end of each chapter. It was originally written as a college-level textbook for new programmers.

    +1 on this book, my girlfriend used this to learn how to program. She had no prior coding experience and apart from the odd question or two, didn't need any real help from me to work through it.


  • Registered Users Posts: 914 ✭✭✭DarkDusk


    Thanks for the resources guys! Just one more question. I'm making a simple program which takes words from the user and inputs them into a story.
    Here's the code so far:
    one = raw_input ("The boy fell from the tree and broke his ")
    two = raw_input ("The boy began to ")
    three = raw_input ("A woman found the boy and brought him to the ")
    four = raw_input ("After a few weeks the boy was safe and ")
    

    What I want to do now is merge all four sentences together into a paragraph, with the user's input as well. So the final program should look like this (with the user input):
    "The boy fell from the tree and broke his arm. The boy began to cry. A woman found the boy and brought him to the hospital. After a few weeks the boy was safe and sound."

    I keep trying to use print commands for this but I keep getting syntax errors when using the following line (for just the first line in program):
    print "The boy fell from the tree and broke his " one
    

    What am I doing wrong? I would really appreciate it if someone could help me solve this.

    Thanks,
    James.


  • Advertisement
  • Closed Accounts Posts: 9,139 ✭✭✭-Trek-


    here is a page that deals with concatenating strings, hope it helps. at work at the moment so can't really type an explanation, sorry :o

    http://diveintopython.org/native_data_types/formatting_strings.html


  • Registered Users Posts: 914 ✭✭✭DarkDusk


    Thanks a trillion!! This helped a lot!!:)


  • Registered Users Posts: 914 ✭✭✭DarkDusk


    Ok here's the source code for my finished program:
    print
    print "Hello. This is James' first program. Simply type a word that suits the end of   the sentence to finish the story and then press enter! Good luck!"
    print
    print
    one = raw_input ("The boy fell from the tree and broke his ")
    print
    two = raw_input ("The boy began to ")
    print
    three = raw_input ("A woman found the boy and brought him to the ")
    print
    four = raw_input ("After a few weeks the boy was ")
    print
    print 
    print "The boy fell from the tree and broke his " + one + ". The boy began to " + two + ". A woman found the boy and brought him to the " + three + ". After a few weeks the boy was " + four + "."
    print
    print
    print
    raw_input ("Press enter to finish reading your story.")
    
    

    It works perfectly and all, but there's one little problem. When a word reaches the end of the line it will split and the rest of the splitted word will go onto the next line. I've looked this up and come across textwrapper, but I don't completely understand it. Can someone tell me what is the easy way to make words go onto the next line for all text in the program, not just for certain paras or sentences. I hope my query isn't too complicated!

    Thanks in advance,
    James.

    P.S. The random "print"s are for making extra spaces between lines, for neatness(in the program not the code lol:p). I'm sure there is an easier way to do it but it's the only way I've found out myself anyway.


  • Closed Accounts Posts: 9,139 ✭✭✭-Trek-


    I think your looking for the new line character '\n'
    print 'This is one line\nThis is another line'
    

    should give
    This is one line
    This is another line
    


  • Registered Users Posts: 914 ✭✭✭DarkDusk


    Thanks, I've used that in my program. My only criticism is that I need to search through the program and predict where I need to put "\n" every time. Is there any way I can make the words automatically go onto a new line?

    James.


  • Advertisement
  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    As was said, the newline character does this if you want to do it manually.
    If it's automatic textwrapping you want here are the docs for two simple functions to do it (They use the TextWrapper class you mentioned but are just more convenient/straight-forward for simple things).

    An example of use would be
    import textwrap #Just throw this at the top of your file
    
    ... #Your code here
    
    print textwrap.fill("The boy fell from the tree and broke his " + one + ". The boy began to " + two + ". A woman found the boy and brought him to the " + three + ". After a few weeks the boy was " + four + ".")
    

    This should automatically insert the newlines at appropriate places.
    You can also give it a width parameter, the default width is 70 characters.

    Finding the width of your terminal at runtime and dealing with it is probably far more complicated than you should be looking at this early, but for future reference curses is probably what you'd want.
    A terminal width of 80 is generally considered "standard" though in most cases, so just keeping everything below this would generally be fine.


  • Registered Users Posts: 914 ✭✭✭DarkDusk


    Pygmalion wrote: »
    As was said, the newline character does this if you want to do it manually.
    If it's automatic textwrapping you want here are the docs for two simple functions to do it (They use the TextWrapper class you mentioned but are just more convenient/straight-forward for simple things).

    An example of use would be
    import textwrap #Just throw this at the top of your file
    
    ... #Your code here
    
    print textwrap.fill("The boy fell from the tree and broke his " + one + ". The boy began to " + two + ". A woman found the boy and brought him to the " + three + ". After a few weeks the boy was " + four + ".")
    

    This should automatically insert the newlines at appropriate places.
    You can also give it a width parameter, the default width is 70 characters.

    Finding the width of your terminal at runtime and dealing with it is probably far more complicated than you should be looking at this early, but for future reference curses is probably what you'd want.
    A terminal width of 80 is generally considered "standard" though in most cases, so just keeping everything below this would generally be fine.

    So, in other words, I just have to put that thingy at the top and "textwrap.fill" infront of print? Correct?:)

    Thanks a mil!


Advertisement