Space in python | Sololearn: Learn to code for FREE!

0

Space in python

Hello! i want ask that when we write things in python like for num in range(1,1000): print(num) πŸ‘† why need to put 4 space? and when need to put it? please tell me below.

5/5/2018 3:52:46 AM

Ayaan

4 Answers

New Answer

+7

This is called "indentation" and in Python this is used to group certain parts of your code. All what is located in the body of the for-loop has to be indentated by the same amount. Same goes for while-loops or if-conditions. First line of these structures have to end with a colon. Take care that you do not mix tabulators with spaces, even if the indentations look identical on your screen but the interpreter will raise an error because "tab" and "space" are totally different characters. In other languages indentation is voluntary to do an optical grouping. In JavaScript or C++ you combine head and body of structures with curly braces. In Python indentation is a necessary thing and Python does not know the concept of curly braces for this purpose. You will learn this and much more if you take the Python course at SoloLearn.

+5

Indentations are enforced in Python to group blocks of codes together. Mainly used in composing functions, conditional blocks, loops, etc. The number of spaces you use for indentations has to be the same for lines grouped within the same block. In other programming languages, curly braces are used in place: if (condition) { // do something } Python: if condition: # do something

+2

Rule of thumb: when a line of code finishes with a colon, then 'at least the following line is intendated.

+1

Donna you are wrong, write code look at above and don't give space and you got "IndentationError"