Skip to content
← Back to Community
How To Make Your Code Shorter! [Is this even useful?]
Profile icon


Programs can be used for nearly anything. Making programs, websites, games, or even devices - pretty much EVERYTHING!

That being said, our devices don't have unlimited storage. The longer the code, the more data is used right?

Some might argue it's more important for the code to somehow look 'natural' than saving space, but what if there are word limits in the code or you have to make the shortest code possible (maybe for a competition)?


First of All: Make sure your code is finished

After all, when you finish your code, it will be much easier to shorten it rather than shortening an unfinished code! It might be hard to understand!

Remove Comments

Note: I will be using Python since it is one of the most well-known languages

Let's write a random Python code! For instance:

answer = input("Are you a programmer?") #Ask for user input if answer == "no": print("Aww") #Output "Aww" elif answer == "yes": print("Good!") #Output "Good" else: print("I don't understand") #Output "I don"t understand"

Now, if the code is done, the comments are not really that useful anymore right?

That's our first step. Let's get rid of them!

It will be a bit shorter:

answer = input("Are you a programmer?") if answer == "no": print("Aww") elif answer == "yes": print("Good!") else: print("I don't understand")

Next up: Remove Unnecessary Indentations and Spaces

Python is an indentation-based language, and when you're coding with it you'll probably find yourself using a lot of indentations!

In the code above, you can see a lot of 4-space indentations. The code works well even if the indentations are only 1-spaced!

Also, try to use:


Rather than:

var = 1

As I said before, the code might not look polished. But hey, it's shorter!

Also, blank spaces are not really that important right? They just take up space!

So, let's shorten it!

answer=input("Are you a programmer?") if answer=="no": print("Aww") elif answer=="yes": print("Good!") else: print("I don't understand")

So far, so good. The code doesn't look THAT bad and it works just as well!

Shorten Variable Names

No matter if you name a variable user_input_one or simply v, the compiler doesn't care how long or short they are! The code will run normally when you replace all the variable names!

Thus, we can shorten the variable answer to ans or even just a!

So, let's shorten it!

a=input("Are you a programmer?") if a=="no": print("Aww") elif a=="yes": print("Good!") else: print("I don't understand")

There you go! It might be harder to understand, but it's SHORTER!

Note: This is the limit if you only want to save a significant amount of space. If, however, you're challenging yourself or being challenged to make the code even shorter, proceed!

Shorten Keywords

Whoa, you can shorten keywords?

Yes, yes you can.

For instance, you can shorten elif to if (or in other languages, else if or even elseif to just if!)

Besides that, you can assign actual keywords to shorter variables to make them shorter! How?

p=print a=input("Are you a programmer?") if a=="no": p("Aww") if a=="yes": p("Good!") else: p("I don't understand")

Now, the code starts to look "ugly" and that's why if you don't mind using a bit more space, don't use this!

Mush several lines of code to just 1

Yes, you read the title right. In Python (and even most languages), you can mush loops, if ... else conditions, or even entire functions to JUST 1 LINE!


p=print a=input("Are you a programmer?") if a=="no":p("Aww") if a=="yes":p("Good!") else:p("I don't understand")

At this point, the code just looks awful and loses its original characteristics! Unless you are totally desperate, DO NOT USE THIS.

More tips!

  • Try to shorten phrases, for instance 'Good!' to a thumbs up emoji 👍

  • Declare multiple variables at once, for example:


Instead of:

a=0 b=0 c=0 d=0

Ending (or is there a better word for it? Epilog?)

  • Think about one tip by yourself and let me know in the comments below! I might add it and give you credit!

Also, thanks for reading! Have a GREAT day! :D

Profile icon
Profile icon

Lol okay that'll make it unreadable and super hard to read. If you want to make code shorter, just use an online minifier that does all this with a click. That way you can do complicated stuff with just "one line of code" lol

Also that's why pythons bad because you can't delete new lines because it doesn't have semicolons or brackets but use stupid indentation :)

Profile icon

@codingjlu wow, thanks for the info! I tried it, and IT WORKS!

Also, Python is the first language I learnt (like, 1 or 2 years ago) and when I learnt pretty much everything else I realized Python is like the black sheep! I mean, the Python for loop is just... weird and different. No variable type declaration, comments with hashtag #, and indentation-based?

Pretty weird, but yeah. Most other languages uses curly brackets, lots of semicolons, and variable type declaration!

Anyways, thanks for your comment :)

Profile icon

@Th3Coder cool :)

Profile icon

You can define many variables at once with different values, like this:
a,b,c = 2,7,45
That makes a = 2, b = 7, c = 45 (Test this by doing print(a+b+c), it should return 54)
And the long other way:
a = 2
b = 7
c = 45

Profile icon

@RollPitchYaw oh cool! I rarely code using Python nowadays, so I didn't know that :)