Introduction to Python
  • Introduction
  • Preface
  • Background
  • Installing Python
  • Getting Started
  • Basics
    • More Printing
    • Strings
    • Numbers
    • Exercises
    • Comments
  • Variables
    • Operators
    • Type Conversion
    • Exercises
    • A Brief Introduction to Lists
    • Game Exercise
  • Human Input
  • Functions
    • First Functions
    • Why Functions
    • Exercises
  • Indentation
  • Decisions
    • Booleans
    • Logical Operators
    • If Statements
    • Elif and Else Statements
    • Exercises
    • Rock, Paper, Scissors
    • Game Exercise
    • Game Exercise 2
  • Lists
  • Loops
    • For Loops
    • While Loops
  • More Data Structures
    • Tuples
    • Dictionaries
  • Pygame
  • Extra Content
    • Computers and Code
    • More About Python
    • For Loops with Range
    • List Slicing
Powered by GitBook
On this page

Indentation

We observed that in Python we have to indent the code when creating functions. We'll have to indent the code every time we create a new block of it. Indentation is extremely important in Python, your code will not work if the indentation isn't done correctly. There are some rules we have to adhere to:

  • Indents can be between 1 and 8 spaces

    • We'll use four spaces as it's common convention

  • Indents can be either space characters or tabs

    • We'll use spaces as it's common convention

  • Do not mix and match indent styles - either use spaces or tabs throughout

# Try to run the code below, what error do you get?
total_bananas = 4
price = 2
  bill = total_bananas * price
print('Your bill is {}'.format(bill))

You can indent your code as long as it's defined within a code block. We've seen code blocks created via functions, and we'll see more as we learn more about if statements and loops.

PreviousExercisesNextDecisions

Last updated 6 years ago