Python Forum
Why does Python not use parenthesis to contain loops?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does Python not use parenthesis to contain loops?
#1
I'm following along with the MIT online course. I am a very elementary programmer, but I have some really basic knowledge of c++ and c#. Then I have a little more in depth knowledge of Powershell. I've been writing scripts in Powershell for about a year now. But, with any loop, typically you do something like this...

for (i=0; i<5; i++) {
do this first
do this second
do this third
}

But, I put in curley braces to contain the loop. Curley braces or parenthesis don't seem to work at all in Python. I tried adding them to a loop and I got an error. How does it know where the loop ends and the next code not part of the loop starts? Is it done purely on indentation?
Reply
#2
Because it's not C
Reply
#3
Yes the indentation is important in python, that's how it knows where the looped code ends.
for number in range(2):
    print(f'{number}: do this first')
    print(f'{number}: do this second')
    print(f'{number}: do this third')
print('do this last')
Output:
0: do this first 0: do this second 0: do this third 1: do this first 1: do this second 1: do this third do this last
https://docs.python.org/3/faq/design.htm...statements Wrote:Why does Python use indentation for grouping of statements?

Guido van Rossum believes that using indentation for grouping is extremely elegant and contributes a lot to the clarity of the average Python program. Most people learn to love this feature after a while.

Since there are no begin/end brackets there cannot be a disagreement between grouping perceived by the parser and the human reader. Occasionally C programmers will encounter a fragment of code like this:
if (x <= y)
        x++;
        y--;
z++;
Only the x++ statement is executed if the condition is true, but the indentation leads many to believe otherwise. Even experienced C programmers will sometimes stare at it a long time wondering as to why y is being decremented even for x > y.

Because there are no begin/end brackets, Python is much less prone to coding-style conflicts. In C there are many different ways to place the braces. After becoming used to reading and writing code using a particular style, it is normal to feel somewhat uneasy when reading (or being required to write) in a different one.

Many coding styles place begin/end brackets on a line by themselves. This makes programs considerably longer and wastes valuable screen space, making it harder to get a good overview of a program. Ideally, a function should fit on one screen (say, 20–30 lines). 20 lines of Python can do a lot more work than 20 lines of C. This is not solely due to the lack of begin/end brackets – the lack of declarations and the high-level data types are also responsible – but the indentation-based syntax certainly helps.
Reply
#4
davidorlow Wrote:Why dos Python not use parenthesis to contain loops?
Python is derived from an experimental language named ABC developped in the 1980's. That's where it took the idea to indicate the body of loops or functions or classes by indentation only.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why both loops don't work in python? nau 3 1,055 Sep-21-2022, 02:17 PM
Last Post: rob101
  Python for loops Kristenl2784 3 41,891 Jun-16-2020, 06:01 PM
Last Post: Yoriz
  For loops in python Boinbo 3 68,550 Apr-18-2020, 01:23 AM
Last Post: buran
  For loops help, using python turtle SemiBeginnerPY 2 3,861 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY
  parenthesis around a tuple of literals in a for Skaperen 2 2,142 Aug-25-2019, 03:00 AM
Last Post: Skaperen
  Parenthesis in User-Defined Functions giorgitsu 2 1,940 Aug-07-2019, 12:56 PM
Last Post: ThomasL
  a Word Inside Parenthesis in Class Decleration tahasozgen 2 2,496 Dec-18-2018, 05:59 AM
Last Post: Gribouillis
  Escaping whitespace and parenthesis in filenames jehoshua 2 9,634 Mar-21-2018, 09:12 AM
Last Post: jehoshua

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020