Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A Tips for golfing in Python

If you have a loop or if, you can save two or more characters (depending on the current indentation level and the number of statements in the body) by putting it right after the colon: For example...

posted 3y ago by celtschk‭  ·  edited 3y ago by General Sebast1an‭

Answer
#4: Post edited by user avatar General Sebast1an‭ · 2021-09-07T05:27:28Z (over 3 years ago)
More golfing.
  • If you have a loop or `if`, you can save two or more characters (depending on the current indentation level and the number of statements in the body) by putting it right after the colon:
  • For example, assume you have this loop with 33 characters:
  • ```
  • for x in a:
  • do_something_with(x)
  • ```
  • Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 31 characters:
  • ```
  • for x in a:do_something_with(x)
  • ```
  • If the body has more than one statement, those can be separated with semicolon. For example
  • ```
  • if (condition):
  • do_something()
  • do_more()
  • ```
  • becomes
  • ```
  • if condition: do_something();do_more()
  • ```
  • Thanks to @Moshi for pointing out that it also works with several statements.
  • If you have a loop or `if`, you can save two or more characters (depending on the current indentation level and the number of statements in the body) by putting it right after the colon:
  • For example, assume you have this loop with 33 characters:
  • ```
  • for x in a:
  • do_something_with(x)
  • ```
  • Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 31 characters:
  • ```
  • for x in a:do_something_with(x)
  • ```
  • If the body has more than one statement, those can be separated with semicolon. For example
  • ```
  • if (condition):
  • do_something()
  • do_more()
  • ```
  • becomes
  • ```
  • if condition:do_something();do_more()
  • ```
  • Thanks to @Moshi for pointing out that it also works with several statements.
#3: Post edited by user avatar celtschk‭ · 2021-09-06T06:43:43Z (over 3 years ago)
It works also for more than one statement
  • If you have just one statement as body of a loop or `if`, you can save two or more characters (depending on the current indentation level) by putting it right after the colon:
  • For example, assume you have this loop with 33 characters:
  • ```
  • for x in a:
  • do_something_with(x)
  • ```
  • Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 31 characters:
  • ```
  • for x in a:do_something_with(x)
  • ```
  • If you have a loop or `if`, you can save two or more characters (depending on the current indentation level and the number of statements in the body) by putting it right after the colon:
  • For example, assume you have this loop with 33 characters:
  • ```
  • for x in a:
  • do_something_with(x)
  • ```
  • Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 31 characters:
  • ```
  • for x in a:do_something_with(x)
  • ```
  • If the body has more than one statement, those can be separated with semicolon. For example
  • ```
  • if (condition):
  • do_something()
  • do_more()
  • ```
  • becomes
  • ```
  • if condition: do_something();do_more()
  • ```
  • Thanks to @Moshi for pointing out that it also works with several statements.
#2: Post edited by user avatar General Sebast1an‭ · 2021-09-05T06:58:14Z (over 3 years ago)
  • If you have just one statement as body of a loop or `if`, you can save two or more characters (depending on the current indentation level) by putting it right after the colon:
  • For example, assume you have this loop with 34 characters:
  • ```
  • for x in a:
  • do_something_with(x);
  • ```
  • Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 32 characters:
  • ```
  • for x in a:do_something_with(x);
  • ```
  • If you have just one statement as body of a loop or `if`, you can save two or more characters (depending on the current indentation level) by putting it right after the colon:
  • For example, assume you have this loop with 33 characters:
  • ```
  • for x in a:
  • do_something_with(x)
  • ```
  • Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 31 characters:
  • ```
  • for x in a:do_something_with(x)
  • ```
#1: Initial revision by user avatar celtschk‭ · 2021-09-04T16:51:17Z (over 3 years ago)
If you have just one statement as body of a loop or `if`, you can save two or more characters (depending on the current indentation level) by putting it right after the colon:

For example, assume you have this loop with 34 characters:
```
for x in a:
 do_something_with(x);
```
Since you've got just one statement, you can delete the newline character and the following indentation space, in order to get 32 characters:
```
for x in a:do_something_with(x);
```