[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
Thanks to Joel and Alan for replying. On 24 January 2016 at 22:08, Alan Gauld wrote: > 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
On 25 January 2016 at 21:46, Alan Gauld wrote: > 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
[Tutor] Program to check Python 2 syntaxes incompatible to Python 3?
Hi, After reading some articles about Python 2 vs Python 3 issues and web pages like: https://docs.python.org/3/whatsnew/3.0.html https://wiki.python.org/moin/Python2orPython3 I'm wondering if there's any program/tool to list out incompatible syntaxes in a Python 2 source file. I know the 2to3 tool exists, but I'm more interested to do some manual change to source code if possible. Thanks in advance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor