http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Statements, Code Blocks and Indentation
Pavan Verma
Python Programming Essentials
1
@YinYangPavan
© SkillBrew http://skillbrew.com
Python statements
 A statement is a one Python instruction
 Most of the time, there is exactly one Python
statement per line
>>> x = 4 + 6
>>> y = x**2
>>> print y
100
2
© SkillBrew http://skillbrew.com
Multiple statements on one line
 It’s possible to have multiple statements,
separated by semi-colon, on a single line
 Doing so is NOT recommended as it reduces
code readability
>>> x = 4 + 6; y = x**2; print y
100
3
© SkillBrew http://skillbrew.com
Statements can span multiple lines
 There are two ways to make a statement span
multiple lines
 Use it if it improves code readability by
breaking a very long statement
4
>>> x = 4 + 
6
>>> x
10
>>> x = (4
+
6)
>>> x
10
© SkillBrew http://skillbrew.com
Code blocks
 A code block is a set of statements that will be
executed together, one after the other
 if statements, for loops, while loops, functions, are all
code blocks
if x > 10:
is_greater = True
print "Greater than 10"
else:
is_greater = False
print "Not greater than 10"
5
© SkillBrew http://skillbrew.com
Code blocks and Indentation
 Code blocks have no explicit begin or end. There
are no explicit braces, brackets, or keywords.
 Code blocks are defined by their indentation
• Indenting starts a block and unindenting ends it
• The only delimiter is a colon (:) and the indentation
of the code itself
 This means that whitespace is significant, and
must be consistent
6
© SkillBrew http://skillbrew.com
Sample code blocks
def fib(n):
print 'n = ', n
if n > 1:
return n * fib(n-1)
else:
print 'end'
return 1
7
© SkillBrew http://skillbrew.com
Sample code blocks (2)
8
def fib(n):
print 'n = ', n
if n > 1:
return n * fib(n-1)
else:
print 'end'
return 1
© SkillBrew http://skillbrew.com
Sample code blocks (3)
9
def fib(n):
print 'n = ', n
if n > 1:
return n * fib(n-1)
else:
print 'end'
return 1
© SkillBrew http://skillbrew.com
Sample code blocks (4)
10
def fib(n):
print 'n = ', n
if n > 1:
return n * fib(n-1)
else:
print 'end'
return 1
© SkillBrew http://skillbrew.com
Indentation Tips
 Use 4 spaces per indentation level
 Spaces are the preferred indentation method
 Use spaces for indentation; not tabs
• 1 tab == 1 indent level
 Python 3 explicitly disallows mixing the use of
tabs and spaces for indentation
11
© SkillBrew http://skillbrew.com
Summary
 Statements
 Relationship between Python statements
and lines
 Code blocks
 Relationship between code blocks and
indentation in Python
12
13

Python Programming Essentials - M6 - Code Blocks and Indentation

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Statements, Code Blocks and Indentation Pavan Verma Python Programming Essentials 1 @YinYangPavan
  • 2.
    © SkillBrew http://skillbrew.com Pythonstatements  A statement is a one Python instruction  Most of the time, there is exactly one Python statement per line >>> x = 4 + 6 >>> y = x**2 >>> print y 100 2
  • 3.
    © SkillBrew http://skillbrew.com Multiplestatements on one line  It’s possible to have multiple statements, separated by semi-colon, on a single line  Doing so is NOT recommended as it reduces code readability >>> x = 4 + 6; y = x**2; print y 100 3
  • 4.
    © SkillBrew http://skillbrew.com Statementscan span multiple lines  There are two ways to make a statement span multiple lines  Use it if it improves code readability by breaking a very long statement 4 >>> x = 4 + 6 >>> x 10 >>> x = (4 + 6) >>> x 10
  • 5.
    © SkillBrew http://skillbrew.com Codeblocks  A code block is a set of statements that will be executed together, one after the other  if statements, for loops, while loops, functions, are all code blocks if x > 10: is_greater = True print "Greater than 10" else: is_greater = False print "Not greater than 10" 5
  • 6.
    © SkillBrew http://skillbrew.com Codeblocks and Indentation  Code blocks have no explicit begin or end. There are no explicit braces, brackets, or keywords.  Code blocks are defined by their indentation • Indenting starts a block and unindenting ends it • The only delimiter is a colon (:) and the indentation of the code itself  This means that whitespace is significant, and must be consistent 6
  • 7.
    © SkillBrew http://skillbrew.com Samplecode blocks def fib(n): print 'n = ', n if n > 1: return n * fib(n-1) else: print 'end' return 1 7
  • 8.
    © SkillBrew http://skillbrew.com Samplecode blocks (2) 8 def fib(n): print 'n = ', n if n > 1: return n * fib(n-1) else: print 'end' return 1
  • 9.
    © SkillBrew http://skillbrew.com Samplecode blocks (3) 9 def fib(n): print 'n = ', n if n > 1: return n * fib(n-1) else: print 'end' return 1
  • 10.
    © SkillBrew http://skillbrew.com Samplecode blocks (4) 10 def fib(n): print 'n = ', n if n > 1: return n * fib(n-1) else: print 'end' return 1
  • 11.
    © SkillBrew http://skillbrew.com IndentationTips  Use 4 spaces per indentation level  Spaces are the preferred indentation method  Use spaces for indentation; not tabs • 1 tab == 1 indent level  Python 3 explicitly disallows mixing the use of tabs and spaces for indentation 11
  • 12.
    © SkillBrew http://skillbrew.com Summary Statements  Relationship between Python statements and lines  Code blocks  Relationship between code blocks and indentation in Python 12
  • 13.