Can anyone please explain to me python indentation like i am a 10 year old? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone please explain to me python indentation like i am a 10 year old?

26th Oct 2019, 1:01 PM
Musafir
Musafir - avatar
12 Answers
+ 5
In every code block like class, function, while, for, with, try except ... you need to indent codes. This tells python that those equally indented lines are belong to same block. That's same as curly braces in other languages.
26th Oct 2019, 1:26 PM
Qasem
+ 4
Explaining indentation by words is hard, it is easier to explain with images and examples. I'm anyways explaining it in words: (Code block is code that belogs to a statement.) (Code block statement is a statement, that contains a code block.) Indentation is a way to indicate a code block to belong to a certain code block statement, using spaces (or tabs). Each code block has their own indentation level, where each line of the code block must have the same amount of spaces in the start of the line. Indentation level starts after a line, which ends with a colon : , and ends after a line, which has less spaces than the indentation level. The amount of spaces of code block's indentation level does not matter, but it must have atleast 1 space more than the previous indentation level, (or the line where the colon : was, refering to its indentation level). Maybe 10 years old wouldn't have understood.
26th Oct 2019, 1:35 PM
Seb TheS
Seb TheS - avatar
+ 1
After any ":", you should indent once
26th Oct 2019, 1:21 PM
Trigger
Trigger - avatar
+ 1
Błack Jesus❕ 4 space doesn't help.
26th Oct 2019, 3:29 PM
Musafir
Musafir - avatar
+ 1
Ok I'll try to explain it as simply as I can: Basically, when you have a statement like "if" which excecutes a bunch of lines when it's True (or anything that requires adding a ":" at the end), adding indentation will help the program know which code is related to the "if" statement, meaning it will do all the code you until there are no more indentations. Python knows that any line not indented is NOT connected to the "if" condition and will exceute them normally. I hope this helps somehow!
26th Oct 2019, 11:38 PM
Cookie
Cookie - avatar
+ 1
Rora Thanks , it helps a lot.
27th Oct 2019, 4:15 AM
Musafir
Musafir - avatar
+ 1
Furqan Ahmad Can you describe your problem?
28th Oct 2019, 10:20 AM
Seb TheS
Seb TheS - avatar
0
confusing
26th Oct 2019, 3:24 PM
Musafir
Musafir - avatar
0
impreesivee
28th Oct 2019, 8:56 AM
e0ger
 e0ger - avatar
0
My name is furqan pleaze help me
28th Oct 2019, 9:39 AM
Furqan Ahmad
Furqan Ahmad - avatar
0
e0ger what do you mean.
28th Oct 2019, 3:04 PM
Musafir
Musafir - avatar