Four space indent python

is four space indent the same as two space indent. i was learning in the python3 intermediate course when i came across a four indent for the first time. i tried making it a 2 indent and it gave the same output so is 2 space indent and 4 space indent the same?

import unittest import entertainment class EntertainmentSystemTests(unittest.TestCase): def test_movie_license(self): daily_movies = entertainment.get_daily_movies() licensed_movies = entertainment.get_licensed_movies() # Write your code below: for movie in daily_movies: print(movie) with self.subTest(movie): self.assertIn(movie, licensed_movies) unittest.main()

python has pep 8, which is a style guide for python code. Which recommends 4 spaces. Although this only a guide, you should generally follow the guide. link

with two spaces its sometimes difficult to see the nesting quickly.

but for example for HTML I would use two spaces indent, given the amount of nesting you can get. If you get so much nesting in python, your code seriously needs a refactor

5 Likes

Can you tell me so that I can understand why I should use a 4 space indent? Should I use tabs or spaces?

if you’re asking why should one use 4 space indent it is because it will make reading the code easier. if you mean how to do 4 space indent then just press spacebar four times

I think the majority of developers prefers spaces, or actually (rather?) soft tabs (where the tab key insert spaces). So you don’t have to press the spacebar 4 times. That becomes tedious. The alternative is hard tabs (where the tab key insert tab character).

I find soft-tabs to be more consistent across editors and operating systems. But it seems to be on ongoing debate among developers. Although it seems to have quit down a little bit over the last few years

So are soft tabs just 4 spaces and hard tabs is not 4 spaces which is 1 tab. I agree that soft tabs a better because if you use hard tabs then not everyone might manually indent by a tab but soft tabs you can indent by spaces and tabs. Is that right?