SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Lesson 03
 
Python Statement, Indentation
and Comments
Perth AI School
Python Statement, Indentation and Comments
Python Statement
Instructions that a Python interpreter can execute are called statements. For example, is
an assignment statement. statement, statement, statement, etc. are other kinds of
statements which will be discussed later.
a = 1
if for while
Multi-line statement
In Python, the end of a statement is marked by a newline character. But we can make a
statement extend over multiple lines with the line continuation character (). For example:
a = + + + 
+ + + 
+ +
1 2 3
4 5 6
7 8 9
This is an explicit line continuation. In Python, line continuation is implied inside
parentheses , brackets , and braces . For instance, we can implement the above multi-
line statement as:
( ) [ ] { }
a = ( + + +
+ + +
+ + )
1 2 3
4 5 6
7 8 9
Here, the surrounding parentheses do the line continuation implicitly. Same is the case
with and . For example:
( )
[ ] { }
colors = [ ,
,
]
'red'
'blue'
'green'
We can also put multiple statements in a single line using semicolons, as follows:
a = ; b = ; c =1 2 3
Python Statement, Indentation and Comments
Python Indentation
Most of the programming languages like C, C++, and Java use braces to de ne a block of
code. Python, however, uses indentation.
{ }
: ( ); a =if True print 'Hello' 5
A code block (body of a function, loop, etc.) starts with indentation and ends with the rst
unindented line. The amount of indentation is up to you, but it must be consistent
throughout that block.
both are valid and do the same thing, but the former style is clearer.
Generally, four whitespaces are used for indentation and are preferred over tabs. Here is an
example.
i range( , ):
(i)
i == :
for in 1 11
print
if 5
break
The enforcement of indentation in Python makes the code look neat and clean. This results
in Python programs that look similar and consistent.
Indentation can be ignored in line continuation, but it's always a good idea to indent. It
makes the code more readable. For example:
:
( )
a =
if True
print 'Hello'
5
and
Python Statement, Indentation and Comments
Incorrect indentation will result in .IndentationError
Comments are very important while writing a program. They describe what is going on
inside a program, so that a person looking at the source code does not have a hard time
guring it out.
These triple quotes are generally used for multi-line strings. But they can be used as a multi-
line comment as well. Unless they are not docstrings, they do not generate any extra code.
You might forget the key details of the program you just wrote in a month's time. So taking
the time to explain these concepts in the form of comments is always fruitful.
"""This is also a
perfect example of
multi-line comments"""
In Python, we use the hash ( ) symbol to start writing a comment.#
It extends up to the newline character. Comments are for programmers to better understand
a program. Python Interpreter ignores comments.
We can have comments that extend up to multiple lines. One way is to use the hash( ) symbol
at the beginning of each line. For example:
#
Another way of doing this is to use triple quotes, either or .''' """
Python Statement, Indentation and Comments
Docstrings in Python
A docstring is short for documentation string.
Output
Python docstrings (documentation strings) are the string literals that appear right after the
de nition of a function, method, class, or module.
Function to double the value
Triple quotes are used while writing docstrings. For example:
*num
:def double(num)
"""Function to double the value"""
return 2
Docstrings appear right after the de nition of a function, class, or a module. This separates
docstrings from multiline comments using triple quotes.
The docstrings are associated with the object as their attribute.__doc__
So, we can access the docstrings of the above function with the following lines of code:
*num
(double.__doc__)
:def double(num)
"""Function to double the value"""
return 2
print

More Related Content

PPTX
String Manipulation in Python
PDF
Datatypes in python
PDF
Lesson 02 python keywords and identifiers
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
PDF
Operators in python
PPT
Introduction to Python
PDF
Python programming : Standard Input and Output
String Manipulation in Python
Datatypes in python
Lesson 02 python keywords and identifiers
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Operators in python
Introduction to Python
Python programming : Standard Input and Output

What's hot (20)

PPT
Python Dictionaries and Sets
PDF
Python basic
PPT
Looping statements in Java
PPTX
Print input-presentation
PPTX
Introduction to the basics of Python programming (part 1)
PPTX
Functions in python slide share
PDF
Python list
PDF
Introduction to python programming
PPTX
Python Programming Language
PPTX
Variables in python
PPTX
Data Structures in Python
PPTX
Looping statement in python
PPTX
Python strings presentation
PPTX
Loops in Python
PPTX
Python variables and data types.pptx
PDF
Introduction to Python
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PPTX
Beginning Python Programming
Python Dictionaries and Sets
Python basic
Looping statements in Java
Print input-presentation
Introduction to the basics of Python programming (part 1)
Functions in python slide share
Python list
Introduction to python programming
Python Programming Language
Variables in python
Data Structures in Python
Looping statement in python
Python strings presentation
Loops in Python
Python variables and data types.pptx
Introduction to Python
What is Python Lambda Function? Python Tutorial | Edureka
Beginning Python Programming
Ad

Similar to Lesson 03 python statement, indentation and comments (20)

PPTX
Introduction to Python for Data Science and Machine Learning
PDF
Python tutorial
PDF
Python Tutorial
PPTX
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
PDF
Python-01| Fundamentals
PDF
Notes1
PDF
Python Interview Questions PDF By ScholarHat.pdf
PDF
3-Python Python oho pytho hdiwefjhdsjhds
PDF
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
PPTX
Pyhton problem solving introduction and examples
PPTX
Introduction on basic python and it's application
PPTX
PDF
Python syntax
PPTX
Introduction to Python Part-1
PPTX
Learn about Python power point presentation
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
PPTX
11-unit1chapter02pythonfundamentals-180823043011.pptx
PDF
A485937779_22738_27_2025_UNIT-1 Lesson 2 Part 1 Variables, Expression and Sta...
PPTX
Unit -1 CAP.pptx
PDF
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
Introduction to Python for Data Science and Machine Learning
Python tutorial
Python Tutorial
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
Python-01| Fundamentals
Notes1
Python Interview Questions PDF By ScholarHat.pdf
3-Python Python oho pytho hdiwefjhdsjhds
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
Pyhton problem solving introduction and examples
Introduction on basic python and it's application
Python syntax
Introduction to Python Part-1
Learn about Python power point presentation
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
11-unit1chapter02pythonfundamentals-180823043011.pptx
A485937779_22738_27_2025_UNIT-1 Lesson 2 Part 1 Variables, Expression and Sta...
Unit -1 CAP.pptx
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
Ad

Recently uploaded (20)

PDF
[EN] Industrial Machine Downtime Prediction
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
Computer network topology notes for revision
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
Managing Community Partner Relationships
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Database Infoormation System (DBIS).pptx
PDF
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPT
Quality review (1)_presentation of this 21
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
Leprosy and NLEP programme community medicine
[EN] Industrial Machine Downtime Prediction
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Galatica Smart Energy Infrastructure Startup Pitch Deck
Computer network topology notes for revision
STERILIZATION AND DISINFECTION-1.ppthhhbx
oil_refinery_comprehensive_20250804084928 (1).pptx
Managing Community Partner Relationships
Reliability_Chapter_ presentation 1221.5784
Database Infoormation System (DBIS).pptx
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Quality review (1)_presentation of this 21
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
STUDY DESIGN details- Lt Col Maksud (21).pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
IB Computer Science - Internal Assessment.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
ISS -ESG Data flows What is ESG and HowHow
Leprosy and NLEP programme community medicine

Lesson 03 python statement, indentation and comments

  • 1. Lesson 03   Python Statement, Indentation and Comments Perth AI School
  • 2. Python Statement, Indentation and Comments Python Statement Instructions that a Python interpreter can execute are called statements. For example, is an assignment statement. statement, statement, statement, etc. are other kinds of statements which will be discussed later. a = 1 if for while Multi-line statement In Python, the end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (). For example: a = + + + + + + + + 1 2 3 4 5 6 7 8 9 This is an explicit line continuation. In Python, line continuation is implied inside parentheses , brackets , and braces . For instance, we can implement the above multi- line statement as: ( ) [ ] { } a = ( + + + + + + + + ) 1 2 3 4 5 6 7 8 9 Here, the surrounding parentheses do the line continuation implicitly. Same is the case with and . For example: ( ) [ ] { } colors = [ , , ] 'red' 'blue' 'green' We can also put multiple statements in a single line using semicolons, as follows: a = ; b = ; c =1 2 3
  • 3. Python Statement, Indentation and Comments Python Indentation Most of the programming languages like C, C++, and Java use braces to de ne a block of code. Python, however, uses indentation. { } : ( ); a =if True print 'Hello' 5 A code block (body of a function, loop, etc.) starts with indentation and ends with the rst unindented line. The amount of indentation is up to you, but it must be consistent throughout that block. both are valid and do the same thing, but the former style is clearer. Generally, four whitespaces are used for indentation and are preferred over tabs. Here is an example. i range( , ): (i) i == : for in 1 11 print if 5 break The enforcement of indentation in Python makes the code look neat and clean. This results in Python programs that look similar and consistent. Indentation can be ignored in line continuation, but it's always a good idea to indent. It makes the code more readable. For example: : ( ) a = if True print 'Hello' 5 and
  • 4. Python Statement, Indentation and Comments Incorrect indentation will result in .IndentationError Comments are very important while writing a program. They describe what is going on inside a program, so that a person looking at the source code does not have a hard time guring it out. These triple quotes are generally used for multi-line strings. But they can be used as a multi- line comment as well. Unless they are not docstrings, they do not generate any extra code. You might forget the key details of the program you just wrote in a month's time. So taking the time to explain these concepts in the form of comments is always fruitful. """This is also a perfect example of multi-line comments""" In Python, we use the hash ( ) symbol to start writing a comment.# It extends up to the newline character. Comments are for programmers to better understand a program. Python Interpreter ignores comments. We can have comments that extend up to multiple lines. One way is to use the hash( ) symbol at the beginning of each line. For example: # Another way of doing this is to use triple quotes, either or .''' """
  • 5. Python Statement, Indentation and Comments Docstrings in Python A docstring is short for documentation string. Output Python docstrings (documentation strings) are the string literals that appear right after the de nition of a function, method, class, or module. Function to double the value Triple quotes are used while writing docstrings. For example: *num :def double(num) """Function to double the value""" return 2 Docstrings appear right after the de nition of a function, class, or a module. This separates docstrings from multiline comments using triple quotes. The docstrings are associated with the object as their attribute.__doc__ So, we can access the docstrings of the above function with the following lines of code: *num (double.__doc__) :def double(num) """Function to double the value""" return 2 print