One Day of IDLE Toying

This page is meant to help new users of Python who might feel a little disoriented. One question that might come to mind is: ok, we've installed Python... ummm... now what?

It might be nice to have a "visual" guide to reduce any initial anxieties. That's what this page is for. The plan is to go through a small session with IDLE: the Integrated Development Environment. IDLE is designed to provide a simple way of exploring the language. During this session, I'll make a few fumbling mistakes, just to show what to expect when things don't go exactly hunky-dory.

By the way, this is an online document If you have any suggestions, or if you want to make corrections or improvements, please feel free to email atdyoo@hkn.eecs.berkeley.edu. Small plug: also, don't forget that there's a great resource in the Python Tutor mailing list: we're a group of people who like to show each other interesting stuff in Python. We're all learning together, so feel free to subscribe and join us.

Changes

  • April 9, 2015: Barbara Jean Lavender pointed out that the link to the Beginner's topic page moved. Thanks for the correction!
  • June 19, 2011: Hoang Huy Nguyen sent me a Vietnamese translation. I sincerely apologise for taking so long to put it up: he sent it to me back in January 2011, and I had completely forgotten about it. Cảm ơn bạn! Tôi xin lỗi!
  • June 29, 2007: Linda Kann has graciously donated her translation of the tutorial into SwedishTack sa mycket!
  • May 21, 2006: Thanks to Michael Tobis for spotting a mistake in the saving_edited_file.jpeg image. The old image mistakenly included a quote sign that shouldn't have been there. I've cropped the image further to hide the mistake. *wink*
  • May 11, 2006: Alan Gauld and Evans Anyokwu have updated the images used in the guide. Thank you both for your help! The older version of this page is still available at: http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/old/.
  • February 8, 2006: Caglar Saygili has graciously donated the Turkish translation. Teshek-kur edirim!
  • January 23, 2006: Liz Young noted that the Python for Beginners topic page has moved (again). Thanks for the correction!
  • October 26, 2004: Jose Miralles-Santana sent me a Spanish translation. Gracias!
  • February 10, 2004: Pramote Khuwijitjaru sent me a translation in ThaiKob-Kkun!
  • February 2, 2004: A reader named Jim noticed that the link to the Beginner's tutorials doesn't go to the right place anymore! Fixed the link tohttp://wiki.python.org/moin/BeginnersGuide/NonProgrammers
  • January 7, 2004: Mohd. Fahmi Basar has written a wonderful translation into MalaysianKa-sih! And Maafkan saya as well; I'm so sorry for being so late about updating the page. One thing drove out another, as one might say.
  • June 4, 2003: I forgot to put this link up: One Day of MacPython IDE Toying is an adaptation of this tutorial for MacPython. Much nicer screenshots, too. *grin*
  • March 11, 2003: Jisoo Yi just sent me a translation into KoreanGam sa hap nee da!
  • February 8, 2003: Tongli Zhang sent me a new translation of the IDLE tutorial in ChineseShie-shie!
  • January 9, 2003: Leon IV Isauvros has written a Greek translation! Sas efharisto poli!
  • December 13, 2002: Roberto Distefano wrote a translation in Italian. This is awesome! Grazie!
  • August 26, 2002: I Wayan Wandira has translated the tutorial into IndonesianSelamat tinggal! Also, I've implemented Ferg_S's suggestion to add page-breaking BR tags to make a dead-wood version of the guide more pleasant to read.
  • July 8, 2002: Yannick Gobin graciously donated a French translation. Merci!
  • April 30, 2002: Mark Lambrecht donated his Dutch translation. Dankzegging!
  • January 30, 2002: Fernando Manaças Ferreira sent me a Portuguese translation. This is wonderful! Obrigado!
  • December 4 (Evening): Gregor Lingl sent me a German translation of the guide. Danke!
  • December 4, 2001: there's enough demand for this page to be done, so I've tried to patch the FIXME's throughout this page.]
  • [June 19, 2001: my computer monitor has just burnt to a brown crisp, so image updates to this page will be a little delayed until I get it replaced. My apologies!]


Ok, let's assume that we've already installed Python. (If not, we can visit: http://python.org and download the most recent Python interpreter. As of this writing, that's Python 2.4.) The first thing we'd like to do is actually start running it! We can do this by opening up IDLE, which should be in our Start Menu under the newly-created Python program group.



We'll see that a new window magnificantly opens up. 

This is the main window to IDLE, and what we see right now is called the "Interpreter" window. The Interpreter allows us to enter commands directly into Python, and as soon as we enter in a command, Python will execute it and spit out its result back to us. We'll be using this Interpreter window a lot when we're exploring Python: it's very nice because we get back our results immediately. If it helps, we can think of it as a very powerful calculator.



Let's try something now! As per tradition, let's get Python to say the immortal words, "Hello World". 

Those '>>>' signs act as a prompt for us: Python is ready to read in a new command by giving us that visual cue. Also, we notice that as we enter in commands, Python will give us its output immediately.



Ok, this seems pretty simple enough. Let's try a few more commands. If we look below:

we'll see the result of running a few more commands. Don't worry too much about knowing the exact rules for making programs yet: the idea is that we can experiment with Python by typing in commands. If things don't work, then we can correct the mistake, and try it again.

If you got to this point, you now know enough to start playing around with Python! Crack open one of the tutorials from the Python For Beginners web page, and start exploring with the interpreter. No time limit here. *grin*



Now that we've paddled long enough, we might be asking: ok, this is neat, but if we close down Python and start it up again, how do we get the computer to remember what we typed?

The solution is a little subtle: we can't directly save what's on the interpreter window, because it will include both our commands and the system's responses. What we'd like is to make a prepared file, with just our own commands, and to be able to save that file as a document. When we're in the mood, we can later open that file and "run" Python over it, saving us the time of retyping the whole thing over again.

Let's try this. First, let's start with a clean slate by opening up a new window.

Here's the result of that menu command:

We notice that there's nothing in this new window. What this means is that this file is purely for our commands: Python won't interject with its own responses as we enter the program, that is, not until we tell it to. I'll call this the "Program" window, to distinguish it from the Interpreter window.



What we wanted to do before was save some of the stuff we had tried out on the interpreter window. Let's do that by typing (or copy/pasting) those commands into our Program window.

Ok, we're done with copying and pasting. One big thing to notice is that we're careful to get rid of the ">>>" prompts because there's not really part of our program. The interpreter uses them just to tell us that we're in the interpreter, but now that we're editing in a separate file, we can remove the artifacts that the interpreter introduces.



Let's save the file now. The Save command is located under the File menu:



Now that we've saved the program, how do we run the program? If we look at the menus on our program window,

we'll see that there's a menu option to "Run Module", and that's what we'll do. What we want to see is Python running through the program, and displaying its results in the Interpreter window.

By the way, one thing to notice is that I made a typo: I didn't quite copy exactly what I had entered in the interpreter window before. Does this affect things?

Ooops. Here is an example of what Python calls a "syntax error". Python sees that we made a typo, and warns us to take a much closer look at our program. The designers of Python feel that having the system point out the error is better than trying to guess at what the programmer meant. It's the idea of explicitness versus implicitness. There are certain rules that Python follows that measure what looks good and what looks suspicious. As we speak the language, we'll get a feel for these rules. And if you're feeling suspicious, yes, this is a bit like grammar. *grin*

Python is often perceptive enough to direct us toward the problem, and in this case, it's telling us that we forgot to put something at the end of this line. In this case, we need to add an additional quotation mark. Let's add that in now.



Ok, let's say that we fixed that silly typo. Let's try to run the program again.

Another snag, but not that complicated, just silly. One other thing that might put us off guard is that IDLE wants us to save any Program windows before we run them; this is a user interface issue that makes sure that we save our work before we start running the program.

(Note: the comment above might be outdated; enough people got annoyed by IDLE's lazy behavior that they've clamored for change. We hope that the development version of IDLE doesn't give this error message anymore.)

They say that third time's the charm, so let's try running it again. Hopefully, it should run well now.



As we play with Python, we'll find ourselves "switching modes" between the Interpreter window and the Program window. The reason for this is because we can use the Interpreter as if it were a small laboratory and experimentally try small programs. After we're satisfied (or when we're tired), we can save what we've learned into a program file.

This, of course, assumes that we can actually load the file later on; it would be a silly thing to save a program, and not be able to load it up later. Let's show this, and then stop for today. I'll close everything down in IDLE, and start from a clean slate.

We'll find the Open command under the File menu:

and if all goes well, we should see a new Program window open up:

with our old program. We're in business! We can save our old work and open it up at a later time. It's not jaw-opening, but it is crucial for the person who wants to play with Python for more than a day. *grin*

This is pretty much all we need to know about IDLE to actually do interesting things. This guide has skipped a lot of stuff about IDLE: IDLE is much more than an mere editor, but it takes some time to explore all of its features, so let's stop for now. There's a IDLE Documentation page that explains advanced IDLE use, for those with an insatiable curiosity. Again, if you have any questions, please feel free to stop by the Python Tutor mailing list; there's usually someone there who'd be happy to chat about Python with you. It's been fun, and I hope this has been helpful!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值