Python Programming - Statments, Indentation, comments - code examples

Statements

>>> a

Traceback (most recent call last):

  File "<pyshell#0>", line 1, in <module>

    a

NameError: name 'a' is not defined

>>> a=5

>>> a

5

>>> a="string value"

>>> a

'string value'

>>> del a

>>> a

Traceback (most recent call last):

  File "<pyshell#6>", line 1, in <module>

    a

NameError: name 'a' is not defined

Statement continuation in next line

>>> a=10+12+35+\

   40+50+60+\

   23+19

>>> a

249

>>> a=(23+42+

   45+56

   +31)

>>> a

197

>>> a=10; b=15; c=25

>>> print("{}, {}, {}".format(c,a,b))

25, 10, 15

>>> colors=['silver',
'yellow',
'red']
>>> colors
['silver', 'yellow', 'red']

Indendation (used for blocks)

>>> for i in range(1,10):
print(i)
if i == 4:
break

1
2
3
4

Incorrect indentation will result in IndentationError

Comments

Single line comment
>>> #single line comment

Multiline comment
>>> '''
This is
multiline
comment
'''
'\nThis is\nmultiline\ncomment\n'
>>> """
This is
another
way to write
multi-line
comment
"""
'\nThis is\nanother\nway to write\nmulti-line\ncomment\n'


Comments

Popular Posts