Re: [Tutor] Noob: nested if-clauses
On 30/01/16 01:06, Alan Gauld wrote: > or CD to the folder: > > C:\SOME\PATH> CD D:\mycode\abc > D: > D:\mycode\abc> Oops, that sequence should be: C:\SOME\PATH> CD D:\mycode\abc C:\SOME\PATH> D: D:\mycode\abc> sorry about that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On 29/01/16 17:41, STF wrote: > what that gives. IDLE is indeed easier to use than the "DOS-style" Python > command-line window. One thing I'd say is that you can tweak the DOS CMD window quite a lot to make it better for IDLE(and other interpreters). But IDLE is probably still better for most folks. > How do you call this thing, BTW? I'm unable to > understand how to navigate inside this thing. I'm not totally clear what you mean here. You start IDLE from the start menu or desktop shortcut. That gets you to the interactive shell which is a version of the python interpreter running inside idle > I mean, when I open it, in which folder am I in? You can find out with >>> import os >>> print ( os.getcwd() ) But its rarely important because you normally use File->Open to open a file, or File->New to create a new one and save it wherever you want). Once you do that IDLE opens a new editor (as opposed to shell) window. From there you can use the Run menu to execute your code with the output appearing in the original shell window. > Suppose I have a Python file in > D:\mycode\abc\myfile.py. How to run it? Within IDLE use File->Open to locate and load it. Then use the Run->Riun Module menu command (shortcut F5) Outside idle use C:\SOME\PATH> python D:\mycode\abc\myfile.py You can of course create a shortcut to do that. or CD to the folder: C:\SOME\PATH> CD D:\mycode\abc D: D:\mycode\abc> then type D:\mycode\abc> python myfile.py hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On Fri, Jan 29, 2016 at 3:13 PM, Alan Gauldwrote: > > When I first heard that python was white-space sensitive I > thought, oh no! But now I see it as a huge strength of the > language. Once you get used to it you will find it helps > far more than it hinders - just avoid tabs and/or map your > tab key to spaces. > > Just want to emphasize: use a tool that actually works for you and for the programming tool. In general, any modern IDE is capable of doing the right thing. NotePad++ is also capable, but avoid anything like WordPad or NotePad, if they are still around... Lastly, most projects I encounter do use 4-space, but some projects (and notably Google projects internally) would use 2-space, so you almost never have to worry about mixing 4-space and 2-space in your code. It just happen that sometimes some people publish code under tab or 2 space and if you just copy-paste you can run into issues, just FYI for the beginners. Thanks. John ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On 25 January 2016 at 21:46, Alan Gauldwrote: > On 25/01/16 15:52, STF wrote: > > > It's a total fluke. I put the indentation like this to *visually* help > > myself understand what I was going to write. > > That's one of the good things about Python, if it looks right > it very often is right. > Actually, in the original example code I type on notepad, I was using tabs. But since I can't press Tab inside Gmail interface, I pressed spaces instead. My incomprehension is partially due to this YouTube video: https://youtu.be/W1zOj2CI-KQ (@ 7:00) in which the author didn't insist on "consistency". Another reason is that, while tab is interpreted as 4 white spaces in convention, it's shown as 8 white spaces in Notepad. So when I opened some source code, I have different numbers of leading white spaces, which lead to my confusion. Personally, I don't find this as a "good thing". It rather recalls the horrible dreams I have had when I was using Fortran! In Fortran, we have to deal with position of first characters to make things work. IMO, making a visual format an essential thing in programming is a very bad idea, if it's not superficial. > > In the Python tutorial that I was using, the author only told us to use > > indentation, without emphasizing on the size of it. > > Quite right the amount is not important(syntactically at least) provided > you are consistent. > > > As I'm a newbie, I'm mostly using Python IDLE but sometimes I would use > > Programmer's Notepad. > > I don't know PN but IDLE will keep you right most of the time. > > > Let me ask an alternative question. Suppose I have something like this: > > > > > > if condition_C: > > instruction_10 > >instruction_11 > > instruction_12 > > > > There are 4 spaces in front of instruction_10, 3 spaces in front of > > instruction_11 and 5 spaces in front of instruction_12. > > > > What would happen to instruction_11 and instruction_12? > > One of the best things about Python is the interpreter. > Just try it and see. It's much faster than posting a question > here and you can be sure it's the correct answer! If you > don't understand what you see, then come here. > > Just use some print statements or simple assignments > for example: > > >>> if True: > ...print 'in the if' > ... print 'still here' > ... y = 5 * 6 > ... > > what happens? > OK, I have just tried it (instead of just reading source codes) and I see what that gives. IDLE is indeed easier to use than the "DOS-style" Python command-line window. How do you call this thing, BTW? I'm unable to understand how to navigate inside this thing. I mean, when I open it, in which folder am I in? Suppose I have a Python file in D:\mycode\abc\myfile.py. How to run it? Thx ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On Fri, Jan 29, 2016 at 11:41 AM, STFwrote: > ...How do you call this thing, BTW? I'm unable to > understand how to navigate inside this thing. I mean, when I open it, in > which folder am I in? Suppose I have a Python file in > D:\mycode\abc\myfile.py. How to run it? IDLE presents itself in two ways. Normally it defaults to opening up as the Python interpreter where you see the ">>>" and can type in code interactively. Its other view can be accessed by going to the "File" menu and either choosing "Open..." and browsing to the file you wish to open, such as "D:\mycode\abc\myfile.py" you mention above, or by creating a new file with the menu option "New File". This will open a second window which is an editor window that you can type in and edit Python code, such as you were doing in Programmer's Notepad. To run the code you have either opened using "Open..." or that you have typed in, use the menu "Run" and select "Run Module". Having both of these windows open at the same time is quite handy! As you type code into the Editor, you may get stuck and not be sure as to what you want to do next. You can then go to the Interpreter window and type in code snippets, exploring Python's behavior for the code you are contemplating. Once you are satisfied, back to the Editor and enter your code. Of course you can copy and paste between the two as well. Notice that the menu bar has differences between the two windows. You should explore the various menu options to see what you can do with both forms of IDLE. Don't forget to check out "Help > IDLE Help" for more information on IDLE's options. Also note that you can get to the local Python language docs through "Help > Python Docs". One more thing to note: IDLE has shortcut keys. As you explore the menus you will see some of them listed next to the menu entries. For more go to "Options > Configure IDLE" and you will see a small tabbed window. If you go to the "Keys" tab, you can see (and edit) the key bindings IDLE uses as well as available pre-configured key binding styles. Hopefully I'm answering the question(s) you are really asking and have not gone off on a tangent! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On 29/01/16 17:41, STF wrote: > Personally, I don't find this as a "good thing". It rather recalls the > horrible dreams I have had when I was using Fortran! In Fortran, we have > to deal with position of first characters to make things work. IMO, making > a visual format an essential thing in programming is a very bad idea, if > it's not superficial. I've never done Fortran but I did do COBOL which similarly is fussy about spacing. But trust me, Python is much more flexible and friendly in its use of whitespace. It essentially just asks you to do what you should be doing anyways. And the way it works avoids most of the dangling else errors you get in languages like C/Java etc (And I had >10 years experience with those before discovering python.) When I first heard that python was white-space sensitive I thought, oh no! But now I see it as a huge strength of the language. Once you get used to it you will find it helps far more than it hinders - just avoid tabs and/or map your tab key to spaces. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
Thanks to Joel and Alan for replying. On 24 January 2016 at 22:08, Alan Gauldwrote: > On 24/01/16 19:42, STF wrote: > > > Let's see the following instructions: > > > > if condition_A: > > instruction_1 > > instruction_2 > > if condition_B: > > instruction_3 > > instruction_4 > > instruction_5 > > else: > > instruction_6 > > > > > > * How to make Pythom understand that instruction_4 is a part of > condition_B > > if-clause but not a direct instruction of condition_A if-clause? > > You've done it above by the indentation. > It's a total fluke. I put the indentation like this to *visually* help myself understand what I was going to write. In the Python tutorial that I was using, the author only told us to use indentation, without emphasizing on the size of it. > > to make Python understand that instruction_5 is outside of condition_B > > if-clause? Just by the number of white spaces in front of every > > instruction?? > > Yes, the indent level tells Python where the instruction should be. > > > * How to make Python understand that "else" belongs to the first > > condition_A if-clause, not to the immediate condition_B if-clause? > > Again you've done it already, just use the indent level. > > > * Suppose I put four white spaces in front of instruction_1, and then > "tab > > key" in front of instruction_2, would this break things? > > In Python 2 things are a wee bit flexible but in Python 3 less so. > But in general avoid mixing them, stick to spaces. Most Python > programmers set their text editor/IDE to convert tabs to > spaces(usually 4) > > > most intelligent text editors would insert automatically a tab in place > of > > 4 white spaces after we press Enter on a line with 4 leading white > spaces. > > Most can also be configured not to use tabs at all and > for Python that's better. Tell us your editor and somebody > can probably advise on optimum settings. > As I'm a newbie, I'm mostly using Python IDLE but sometimes I would use Programmer's Notepad. > > > * Do I really need to keep the consistency of 4 white spaces? Not one > more > > or one less? > > No you can have as many or as few as you like in your own code, > just be consistent. 4 just happens to be esy to read. And its > the standard for library code so if you want to write some code > for the standard library you will need to use 4 spaces. In > the interpreter (>>>) I often only use 2 just to save typing. > But for production code I stick with 4 - not a problem since > the editor(vim) does most of the work for me. > Let me ask an alternative question. Suppose I have something like this: if condition_C: instruction_10 instruction_11 instruction_12 There are 4 spaces in front of instruction_10, 3 spaces in front of instruction_11 and 5 spaces in front of instruction_12. What would happen to instruction_11 and instruction_12? Would Python ignore them? Or would they be considered instructions outside the if-clause? Thanks again. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
> if condition_C: > instruction_10 >instruction_11 > instruction_12 > > There are 4 spaces in front of instruction_10, 3 spaces in front of > instruction_11 and 5 spaces in front of instruction_12. > > What would happen to instruction_11 and instruction_12? Would Python > ignore them? Or would they be considered instructions outside the > if-clause? > You should get an error at program parse time, as the system keeps track of the indentation used to begin new blocks. If it sees an indent level that is inconsistent with those beginnings, it should know to signal a parse error. Please feel free to ask questions. Good luck! > Thanks again. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On 25/01/16 15:52, STF wrote: > It's a total fluke. I put the indentation like this to *visually* help > myself understand what I was going to write. That's one of the good things about Python, if it looks right it very often is right. > In the Python tutorial that I was using, the author only told us to use > indentation, without emphasizing on the size of it. Quite right the amount is not important(syntactically at least) provided you are consistent. > As I'm a newbie, I'm mostly using Python IDLE but sometimes I would use > Programmer's Notepad. I don't know PN but IDLE will keep you right most of the time. > Let me ask an alternative question. Suppose I have something like this: > > > if condition_C: > instruction_10 >instruction_11 > instruction_12 > > There are 4 spaces in front of instruction_10, 3 spaces in front of > instruction_11 and 5 spaces in front of instruction_12. > > What would happen to instruction_11 and instruction_12? One of the best things about Python is the interpreter. Just try it and see. It's much faster than posting a question here and you can be sure it's the correct answer! If you don't understand what you see, then come here. Just use some print statements or simple assignments for example: >>> if True: ...print 'in the if' ... print 'still here' ... y = 5 * 6 ... what happens? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On 24/01/16 19:42, STF wrote: > Let's see the following instructions: > > if condition_A: > instruction_1 > instruction_2 > if condition_B: > instruction_3 > instruction_4 > instruction_5 > else: > instruction_6 > > > * How to make Pythom understand that instruction_4 is a part of condition_B > if-clause but not a direct instruction of condition_A if-clause? You've done it above by the indentation. > to make Python understand that instruction_5 is outside of condition_B > if-clause? Just by the number of white spaces in front of every > instruction?? Yes, the indent level tells Python where the instruction should be. > * How to make Python understand that "else" belongs to the first > condition_A if-clause, not to the immediate condition_B if-clause? Again you've done it already, just use the indent level. > * Suppose I put four white spaces in front of instruction_1, and then "tab > key" in front of instruction_2, would this break things? In Python 2 things are a wee bit flexible but in Python 3 less so. But in general avoid mixing them, stick to spaces. Most Python programmers set their text editor/IDE to convert tabs to spaces(usually 4) > most intelligent text editors would insert automatically a tab in place of > 4 white spaces after we press Enter on a line with 4 leading white spaces. Most can also be configured not to use tabs at all and for Python that's better. Tell us your editor and somebody can probably advise on optimum settings. > * Do I really need to keep the consistency of 4 white spaces? Not one more > or one less? No you can have as many or as few as you like in your own code, just be consistent. 4 just happens to be esy to read. And its the standard for library code so if you want to write some code for the standard library you will need to use 4 spaces. In the interpreter (>>>) I often only use 2 just to save typing. But for production code I stick with 4 - not a problem since the editor(vim) does most of the work for me. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Noob: nested if-clauses
Hi, I've just started to learn Python thru some online courses and websites. They just teach very basic things. I've got some questions about "if" that I'm unable to find the answers. So let me ask the newbie questions here. Let's see the following instructions: if condition_A: instruction_1 instruction_2 if condition_B: instruction_3 instruction_4 instruction_5 else: instruction_6 * How to make Pythom understand that instruction_4 is a part of condition_B if-clause but not a direct instruction of condition_A if-clause? And how to make Python understand that instruction_5 is outside of condition_B if-clause? Just by the number of white spaces in front of every instruction?? * How to make Python understand that "else" belongs to the first condition_A if-clause, not to the immediate condition_B if-clause? * Suppose I put four white spaces in front of instruction_1, and then "tab key" in front of instruction_2, would this break things? I ask so because most intelligent text editors would insert automatically a tab in place of 4 white spaces after we press Enter on a line with 4 leading white spaces. * Do I really need to keep the consistency of 4 white spaces? Not one more or one less? Thanks in advance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Noob: nested if-clauses
On Sun, Jan 24, 2016 at 2:42 PM, STFwrote: > Hi, > > I've just started to learn Python thru some online courses and websites. > They just teach very basic things. I've got some questions about "if" that > I'm unable to find the answers. So let me ask the newbie questions here. > > Let's see the following instructions: > > if condition_A: > instruction_1 > instruction_2 > if condition_B: > instruction_3 > instruction_4 > instruction_5 > else: > instruction_6 > > > * How to make Pythom understand that instruction_4 is a part of condition_B > if-clause but not a direct instruction of condition_A if-clause? And how > to make Python understand that instruction_5 is outside of condition_B > if-clause? Just by the number of white spaces in front of every > instruction?? > > * How to make Python understand that "else" belongs to the first > condition_A if-clause, not to the immediate condition_B if-clause? > > * Suppose I put four white spaces in front of instruction_1, and then "tab > key" in front of instruction_2, would this break things? I ask so because > most intelligent text editors would insert automatically a tab in place of > 4 white spaces after we press Enter on a line with 4 leading white spaces. > > * Do I really need to keep the consistency of 4 white spaces? Not one more > or one less? > yes. you can use 1 or 2 or any number of spaces, but do spacing consistently. 4 is recommended. Don't mix tabs and spaces > > Thanks in advance. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor