Skip to main content

Structure

Commentโ€‹

Comments allow you to describe the code or the logic written for the program. You can have single or multiple lines of comments. To comment in Python, use #:

# This is a comment

A multi line comment can be declared using โ€œโ€œโ€œ โ€œโ€œโ€œ

"""
First line
Second line
third line
...
...
...
"""

Python shellโ€‹

Similar to Unix shell, Python has a shell prompt >>>. The prompt takes input and shows the output in the next line.

>>>

Here's an example of adding variables using the prompt:

>>> x = 4 + 1
>>> y = 2 + 1
>>> z = x + y
>>> z
8

File extensionโ€‹

The extension of a python script file is .py

Here's the same addition program inside a python script file:

x = 4 + 1
y = 2 + 1
z = x + y

print(z)

To run this python script in a Unix shell:

user@theprogrammingfoundation:~$ python addition.py
user@theprogrammingfoundation:~$ 8