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

What's hot (20)

PPTX
Modules in Python Programming
sambitmandal
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
PPT
File handling in c
David Livingston J
 
PPTX
Pointers in c++
Vineeta Garg
 
PDF
Set methods in python
deepalishinkar1
 
PPTX
Scope rules : local and global variables
sangrampatil81
 
PPTX
Variables in python
Jaya Kumari
 
PPTX
Strings in c++
Neeru Mittal
 
PPT
constants, variables and datatypes in C
Sahithi Naraparaju
 
PPTX
Ternary operator
Hitesh Kumar
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPT
Strings
Nilesh Dalvi
 
PPTX
Introduction to Object Oriented Programming
Moutaz Haddara
 
PDF
Python basic
Saifuddin Kaijar
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PDF
Python file handling
Prof. Dr. K. Adisesha
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Java tokens
shalinikarunakaran1
 
PPTX
File handling in Python
Megha V
 
Modules in Python Programming
sambitmandal
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
File handling in c
David Livingston J
 
Pointers in c++
Vineeta Garg
 
Set methods in python
deepalishinkar1
 
Scope rules : local and global variables
sangrampatil81
 
Variables in python
Jaya Kumari
 
Strings in c++
Neeru Mittal
 
constants, variables and datatypes in C
Sahithi Naraparaju
 
Ternary operator
Hitesh Kumar
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Strings
Nilesh Dalvi
 
Introduction to Object Oriented Programming
Moutaz Haddara
 
Python basic
Saifuddin Kaijar
 
Modules and packages in python
TMARAGATHAM
 
Python file handling
Prof. Dr. K. Adisesha
 
Abstract class in java
Lovely Professional University
 
Java tokens
shalinikarunakaran1
 
File handling in Python
Megha V
 

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

PPTX
Basic Syntax of python thats help to.pptx
MdMujahidHasan1
 
PDF
Python syntax
Learnbay Datascience
 
PDF
Python quick guide
Hasan Bisri
 
PPTX
Basic syntax supported by python
Jaya Kumari
 
PPTX
GE3151- PYTHON PROGRAMMING ANNA UNI.pptx
ReshmaR57
 
DOCX
Python Programming Language
StefanoTermini3
 
PPTX
Python_Interview_Questions_And_Answers..
GajulaYuvaraj
 
PPTX
Python ppt
Anush verma
 
PPTX
Python
Dr. SURBHI SAROHA
 
PPTX
MODULE hdsfsf gefegsfs wfefwfwfg etegeg.pptx
prasathg214
 
PPT
1.3 Basic coding skills_tupels_sets_controlloops.ppt
VGaneshKarthikeyan
 
PPTX
Introduction to Python Programming .pptx
NaynaSagarDahatonde
 
PPTX
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
MalligaarjunanN
 
PPTX
Python basics
Hoang Nguyen
 
PPTX
Python basics
Luis Goldster
 
PPTX
Python basics
Tony Nguyen
 
PPTX
Python basics
Fraboni Ec
 
PPTX
Python basics
Harry Potter
 
PPTX
Python basics
Young Alista
 
PPTX
Python basics
James Wong
 
Basic Syntax of python thats help to.pptx
MdMujahidHasan1
 
Python syntax
Learnbay Datascience
 
Python quick guide
Hasan Bisri
 
Basic syntax supported by python
Jaya Kumari
 
GE3151- PYTHON PROGRAMMING ANNA UNI.pptx
ReshmaR57
 
Python Programming Language
StefanoTermini3
 
Python_Interview_Questions_And_Answers..
GajulaYuvaraj
 
Python ppt
Anush verma
 
MODULE hdsfsf gefegsfs wfefwfwfg etegeg.pptx
prasathg214
 
1.3 Basic coding skills_tupels_sets_controlloops.ppt
VGaneshKarthikeyan
 
Introduction to Python Programming .pptx
NaynaSagarDahatonde
 
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
MalligaarjunanN
 
Python basics
Hoang Nguyen
 
Python basics
Luis Goldster
 
Python basics
Tony Nguyen
 
Python basics
Fraboni Ec
 
Python basics
Harry Potter
 
Python basics
Young Alista
 
Python basics
James Wong
 
Ad

Recently uploaded (20)

PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
PDF
apidays Singapore 2025 - Streaming Lakehouse with Kafka, Flink and Iceberg by...
apidays
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PDF
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
03_Ariane BERCKMOES_Ethias.pptx_AIBarometer_release_event
FinTech Belgium
 
PDF
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
PPTX
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PPTX
04_Tamás Marton_Intuitech .pptx_AI_Barometer_2025
FinTech Belgium
 
PPTX
How to Add Columns and Rows in an R Data Frame
subhashenia
 
PDF
Business implication of Artificial Intelligence.pdf
VishalChugh12
 
PPTX
What Is Data Integration and Transformation?
subhashenia
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
Data Science Course Certificate by Sigma Software University
Stepan Kalika
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
apidays Singapore 2025 - Streaming Lakehouse with Kafka, Flink and Iceberg by...
apidays
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
03_Ariane BERCKMOES_Ethias.pptx_AIBarometer_release_event
FinTech Belgium
 
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
04_Tamás Marton_Intuitech .pptx_AI_Barometer_2025
FinTech Belgium
 
How to Add Columns and Rows in an R Data Frame
subhashenia
 
Business implication of Artificial Intelligence.pdf
VishalChugh12
 
What Is Data Integration and Transformation?
subhashenia
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
Data Science Course Certificate by Sigma Software University
Stepan Kalika
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
Ad

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