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

Comments on Tips for golfing in Python

Parent

Tips for golfing in Python

+2
−0

If you have any tips for golfing in Python, add them as answers to this post.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+2
−0

Reorder expressions in order to save whitespace

Consider the following code:

if c=='U':c='X'

The space between if and c obviously cannot be removed, as that would merge them to the identifier ifc. However by reversing the order of the arguments to ==, the space becomes removable:

if'U'==c:c='X'

As user noted in the comments, another place where whitespace can be saved is between a number and a following keyword. For example, in

if 2+x in{a,b,c}:x=0

the space between x and in cannot be simply removed. However reordering the sum allows to remove it anyway:

if x+2in{a,b,c}:x=0
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

+1. It might also be worth mentioning that you can have numbers before keywords without whitespace in... (2 comments)
+1. It might also be worth mentioning that you can have numbers before keywords without whitespace in...
user‭ wrote over 2 years ago

+1. It might also be worth mentioning that you can have numbers before keywords without whitespace in between and stuff like that.

celtschk‭ wrote over 2 years ago

Thanks; actually I hadn't been aware of the number before keyword case. I'll add this.