r/learnpython • u/badcrow7713 • 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
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
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
4
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
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 0EDIT: typos
3
8
u/scithon Apr 15 '22
The whitespace indentation is addressed in the antigravity module docs. Just import it to see them
import antigravity5
u/badcrow7713 Apr 15 '22
sounds risky :D
5
u/ebdbbb Apr 16 '22
It's one of the best standard library modules.
5
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
5
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
97
u/jddddddddddd Apr 15 '22
You don’t have to use tabs, you can use spaces..
53
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
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
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
19
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
blackandyapfcan be helpful here.2
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
6
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
3
u/mostactiveacct Apr 16 '22
There’s an Easter egg related to this. Try:
from __future__ import braces3
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
2
2
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
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
38
u/buqr Apr 15 '22 edited Apr 03 '24
My favorite color is blue.