PWP - Chapter 1
PWP - Chapter 1
Python Statement
• Instructions that a Python interpreter can execute
are called statements.
• For example, a = 1 is an assignment
statement. if statement, for statement, while
statement etc. are other kinds of statements.
Multi-line statement
• In Python, 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 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 could also put multiple statements in a
single line using semicolons, as follows
a = 1; b = 2; c = 3
Python Indentation
• Most of the programming languages like C,
C++, Java use braces { } to define a block of
code. Python uses indentation.
• A code block (body of a function,loop etc.)
starts with indentation and ends with the first
unindented line. The amount of indentation is
up to you, but it must be consistent throughout
that block.
• Generally four whitespaces are used for
indentation and is preferred over tabs. Here is
an example.
for i in range(1,11):
print(i)
if i == 5:
break
• Indentation can be ignored in line continuation.
But it's a good idea to always indent. It makes the
code more readable. For example:
if True:
print('Hello')
a=5
and
if True: print('Hello'); a = 5
both are valid and do the same thing. But the former
style is clearer.
• Incorrect indentation will result
into IndentationError.
Python Comments
• Comments are very important while writing a
program. It describes what's going on inside a
program so that a person looking at the source
code does not have a hard time figuring it out.
You might forget the key details of the
program you just wrote in a month's time. So
taking time to explain these concepts in form
of comments is always fruitful.
• In Python, we use the hash (#) symbol to start
writing a comment.
• It extends up to the newline character.
Comments are for programmers for better
understanding of a program. Python Interpreter
ignores comment.
#This is a comment
#print out Hello
print('Hello')
Python Keywords :
• Keywords are the reserved words in Python.
• We cannot use a keyword as a variable
name, function name or any other identifier. They are
used to define the syntax and structure of the Python
language.
• In Python, keywords are case sensitive.
• There are 33 keywords in Python 3.7. This number
can vary slightly in the course of time.
• All the keywords except True, False and None are in
lowercase and they must be written as it is. The list of
all the keywords is given below.
False class finally is return
none continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Python Identifiers :
• An identifier is a name given to entities like class,
functions, variables, etc. It helps to differentiate one
entity from another.
>>> a@ = 0
File"<interactive input>", line 1
a@ = 0
^
SyntaxError: invalid syntax