r/learnpython • • Apr 15 '22

Python syntax, any alternative to tabs for loops?

Are python code blocks really done with tabs? Is there no option to use some kind of close or end statement for loop or if blocks?

This is so weird to me! I have never seen a langauge syntax based on white space, am I being dumb or what?

The learning/tutorial resources I've used so far, just give an example with tabs and move on, with no mention of the syntax itself, as if it's no big deal or common knowledge or something.

Edit: I deserve more downvotes, big noob here

24 Upvotes

68 comments sorted by

38

u/buqr Apr 15 '22 edited Apr 03 '24

My favorite color is blue.

7

u/badcrow7713 Apr 15 '22

Alright I'm just being dumb then

5

u/outceptionator Newbie Apr 16 '22

No you're not but after a while you'll see it makes code really easy to read

16

u/nativedutch Apr 15 '22

I just did some C++ coding with curly brackets. Now doing some python devt , prefer the indents in python. But actually its the same difference

5

u/badcrow7713 Apr 15 '22

There's one particular thing that really bothers me about it, which is really long lines of code aren't easily broken up if white space is part of the syntax. Unless there's some kind of python way of doing that but I haven't seen any so far.

14

u/chzaplx Apr 15 '22

You can line break things inside parentheses and some other cases as well, like list elements in brackets. Python doesn't care about whitespace in those cases. Not uncommon to see function calls with a lot of params to have each on it's own line.

5

u/badcrow7713 Apr 16 '22

perfect thanks! sorry for being a noob, I feel like I deserve a lot more downvotes

7

u/Bjadrik Apr 16 '22

Downvotes are for content which doesn't contribute anything of relevance. You're asking genuine clarifying questions and therefore deserve the upvotes.

1

u/badcrow7713 Apr 16 '22

kind of a "read the manual" moment though, no?

5

u/ebdbbb Apr 16 '22

You can have a line continuation with a \ or it'll automatically continue a line that's within brackets as long as the indentation doesn't match the statement level.

# these are the same
something = [1, 2, 3, 4]
something_else = [
    1,
    2,
    3,
    4
]

1

u/badcrow7713 Apr 16 '22

perfect thank you!

4

u/nativedutch Apr 16 '22

Its not whitespaces, its indents.

9

u/notislant Apr 15 '22

I did some Javascript from Python and honestly both ways seem easy to get used to. Pure indents look better imo. Either way I use indents in both to try and keep it from looking super confusing.

3

u/nativedutch Apr 15 '22

Yep , indeed for your own benefit.

3

u/-Buzzy- Apr 16 '22 edited Apr 16 '22

I code 40% of the time in c++ and 60% in python and while both ways are acceptable and ok to use python's way is much easier to read and cleaner when you look at it in my opinion. C++ gets messy when you follow this standard:

int main()
{
    return 0;
}

you get two more lines of code. You could also do:

int main() { 
    return 0;}

or even fit the code in one line:

int main() {return 0;}

but In my experience most people follow the first example. VSC C++ extension also formats code that way.

Python for comparision:

def main():
    return 0

EDIT: typos

3

u/hue_Broacks Apr 16 '22

You Just forgot the : in Python.

def main():

   return 0

3

u/-Buzzy- Apr 16 '22

thanks, edited

2

u/hue_Broacks Apr 16 '22

I Just dont know How to use that code block here -.-

8

u/scithon Apr 15 '22

The whitespace indentation is addressed in the antigravity module docs. Just import it to see them

import antigravity

5

u/badcrow7713 Apr 15 '22

sounds risky :D

5

u/ebdbbb Apr 16 '22

It's one of the best standard library modules.

5

u/badcrow7713 Apr 16 '22

but I can't import magnetic boots first, I get an error

2

u/emluh Apr 16 '22
import boots from magnetic

1

u/badcrow7713 Apr 16 '22

perfect thanks

5

u/ToddBradley Apr 15 '22

Even if there was some alternative, you wouldn't want to use it, because every piece of source code ever written in Python uses indentation to indicate code blocks.

2

u/badcrow7713 Apr 15 '22

Ah, didn't even consider this, thank you

5

u/[deleted] Apr 16 '22

add colons remember

2

u/badcrow7713 Apr 16 '22

roger that

3

u/Nightcorex_ Apr 15 '22

Loops create a new scope, therefore you need an indentation.
If you're only interested in the return results you can use comprehensions, like f.e. list comprehensions:

xs = [random.randrange(0, 10) for _ in range(10)]

2

u/badcrow7713 Apr 15 '22

oh nice! now I know what to look up, thanks for this!

97

u/jddddddddddd Apr 15 '22

You don’t have to use tabs, you can use spaces..

53

u/[deleted] Apr 15 '22

Please delete this it offends me

29

u/jddddddddddd Apr 15 '22

I think PEP8 recommends spaces… Plus I think most surveys show most Python devs use spaces rather than tabs..

30

u/buqr Apr 15 '22 edited Apr 04 '24

I enjoy cooking.

6

u/[deleted] Apr 15 '22

You're probly right I just started off using tabs and now I can't stop. Why spaces over tabs though just out of curiosity?

3

u/buqr Apr 15 '22 edited Apr 03 '24

I'm learning to play the guitar.

5

u/Nightcorex_ Apr 15 '22

It's actually better for copying to Reddit. If you have tabs instead of 4 spaces and you copy it, it won't correctly paste the indentations for Reddit's code blocks (obviously only in Markdown mode).

2

u/badcrow7713 Apr 15 '22

oh my god so that's what i've been doing wrong

could never get code blocks to paste properly, always been a tabber

4

u/buqr Apr 15 '22 edited Apr 03 '24

I enjoy spending time with my friends.

4

u/AceAntares15 Apr 16 '22

not just on reddit. if i remember correctly, the book i read said that applies to most online platforms. anywhere you can paste text. tabs aren't exactly translated the same everywhere, but spaces usually are

4

u/baubleglue Apr 15 '22

Spaces better because "you see what you have", tabs may be configured in editor differently. Tabs better because they use less bytes.

0

u/[deleted] Apr 16 '22

Tabs better because RSI is real and 1 keystroke < 4 keystrokes

7

u/baubleglue Apr 16 '22

I type tab and it converted to spaces by editor - keystroke.

19

u/n3buchadnezzar Apr 15 '22

Pssst, just set your editor to automatically convert tab into 4 spaces.

12

u/ES-Alexander Apr 15 '22

It’s generally a matter of personal preference or style guides, but someone posted this previously which is a fun read

https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/

4

u/bugamn Apr 15 '22

That is interesting, but the one thing that I wanted to see them controlling is for company

3

u/Zeroflops Apr 15 '22

As this is an old debate Most editors will can be set to automatically convert tabs to spaces as you type. So your not hitting space all the time. You just hit tab

3

u/POGtastic itertools evangelist Apr 16 '22

It's just convention, and different codebases use different style guides. It doesn't matter as long as it's consistent. And if you disagree, you can always set up Git hooks to format the codebase's weird spacing to your preferred format on checkout, and format it back to the codebase's spacing upon commit. Tools like black and yapf can be helpful here.

2

u/Jealous-Necessary-47 Apr 16 '22

I think tabs are not consistent across all os but spaces are.

2

u/tcfcfc Apr 16 '22

Question... If the most important stylistic principle is to be more DRY why would you use 4 spaces when you can use one tab?

2

u/[deleted] Apr 16 '22

Excuse me while I try to take the steps 4 at a time.

2

u/tcfcfc Apr 16 '22

I mean you get to your destination faster and fitter!

6

u/[deleted] Apr 16 '22

I have never seen a langauge syntax based on white space

Well, Python's the language with semantic whitespace. This is considered somewhat controversial but most everyone's using the same whitespace in C-style languages anyway, so...

3

u/zurtex Apr 16 '22

Run:

from __future__ import braces

3

u/mostactiveacct Apr 16 '22

There’s an Easter egg related to this. Try:

from __future__ import braces

3

u/DavidRoyman Apr 16 '22

Something which comes up often in code reviews, is that people new to the language will use loops in situations where python uses other idioms, because that's what they used previously for other languages.

This is a dumb example, but might give an idea.

oldlist = [1, 2, 3]
newlist = []

` typical for loop
for x in oldlist:
    newlist.append(x+1)

` with list comprehension
newlist = [x+1 for x in oldlist]

2

u/MezzoScettico Apr 15 '22

Are python code blocks really done with tabs?

They're really done with indents. Indents are significant.

This is so weird to me!

It takes some getting used to if you're coming from other languages where you have a lot more freedom on the formatting.

And the indents have to agree for everything in the same code block. I'm not sure what it takes to tell Python that two lines go together if one uses spaces and the other uses tabs.

Is there no option to use some kind of close or end statement for loop or if blocks?

Nope. The end of the block is a line that has less indent level.

2

u/Shot_Calendar_7373 Apr 16 '22

Python actually throws an inconsistent indentation error if you mix tabs and spaces! Before anything even runs

3

u/chasrmartin Apr 16 '22

You can always use spaces but yes the use of indentation is very uncommon as a way to organize a block structured language.

2

u/timPerfect Apr 16 '22

4 spaces or tabs

2

u/[deleted] Apr 16 '22

Use tab or 4 spaces

2

u/Konke_yDong Apr 16 '22

white space 💀💀 this comment was brought to u by the dark mode gang

2

u/Kerbart Apr 16 '22

While you still have to indent the code inside a loop, you can close it with a scope closure statement, which is always end <scope type preceded by a pound sign. For instance:

for i in range(10):
    if i  2 == 0:
        print(i. 'is even')
    else:
        print(i, 'is odd')
    # end if
# end for
print('end of program')

2

u/RhinoRhys Apr 16 '22

I literally indent everything whether it needs it or not. It's all about that readability.

2

u/S1deWalk3r Apr 16 '22

idk anything apart from list comprehensions

2

u/zaRM0s Apr 16 '22

It’s strange to begin with but generally once you’re used to it it’s such a good idea! I hated it at first as it just wasn’t normal to me but now I’m used to it I prefer it lol