INTRODUCTION TO PYTHON By C P Sahu, PGT CS
PYTHON
Python is a high level, interpreted, interactive and object oriented scripting language.
Python is designed to be highly readable.
PYTHON FEATURES
[Link]-to-learn: Python has few keywords, simple
structure, and a clearly defined syntax. This allows the
student to pick up the language quickly.
[Link]-to-use: Due to less number of lines of code python
requires less time to make programs. The larger program
require more attention.
[Link]/Platform independent: Python can run on a
wide variety of hardware platforms and has the same
interface on all platforms.
PYTHON FEATURES
4. Scalable: Python provides a better structure and support
for large programs than shell scripting.
5. Free & Open Source – Can be downloaded freely
and source code can be modify for improvement.
6. Completeness – Support wide rage of library and
can be used for developing any type of software.
7. Extendable
8. Expressive: The programs written in Python are easily
readable and understandable.
DEMERITS
1. Python programs are not directly convertible to other languages.
2. It is not fastest.
3. Libraries available with python is not vast as of C, C++, java etc.
USE OF PYTHON
• The language is used by companies in real revenue
generating products, such as:
• In operations of Google search engine, youtube, etc.
• Bit Torrent peer to peer file sharing is written using
Python
• Intel, Cisco, HP, IBM, etc use Python for hardware
testing.
• Maya provides a Python scripting API
• i–Robot uses Python to develop commercial Robot.
• NASA and others use Python for their scientific
programming task.
INTERACTIVE MODE PROGRAMMING
Symbol ‘>>> ‘ represents consol
>>>print("Hello, Python!“)
Hello, Python!
SCRIPT MODE PROGRAMMING
1. Lets us write a simple Python program in a script file.
2. Python files have extension .py.
3. Type the following source code in a [Link] file and run −
print ("Hello, Python!“)
PYTHON IDENTIFIERS
1. A Python identifier is a name used to identify a variable, function, class,
module or other object.
2. An identifier starts with a letter A to Z or a to z or an
underscore _ Followed by zero or more letters, underscores and digits 0
to 9.
3. Punctuators such as . , ) ] } ; “ ? and special characters such as @, $,
and % are not allowed within identifiers.
4. Python is a case sensitive programming language.
Thus, Hello and hello are two different identifiers in Python.
5. Reserved words are not allowedas identifier.
RESERVED WORDS
1. These are reserved words that have some fixed meaning in
language.
2. you cannot use them as constant or variable or any other identifier
names.
3. All the Python keywords contain lowercase letters only.
LINES AND INDENTATION
1. Python provides no braces to indicate blocks of code for class and
function definitions or flow control.
2. Blocks of code are denoted by line indentation, which is rigidly
enforced.
3. The number of spaces in the indentation is variable, but all statements
within the block must be indented the same amount.
For example −
MULTI-LINE STATEMENTS
1. Statements in Python typically end with a new line.
2. However, Python does allow the use of the line continuation character ‘
\’ to denote that the line should continue.
3. For example −
QUOTATION IN PYTHON
1. Python accepts single ′, double " and triple ′ ′ ′or “ “
" quotes to denote string literals, as long as the same type of
quote starts and ends the string.
2. The triple quotes are used to span the string across multiple
lines.
3. For example, all the following are legal −
COMMENTS IN PYTHON
1. A hash sign # that is not inside a string literal begins a comment.
2. All characters after the # and up to the end of the physical line are part of the
comment and the Python interpreter ignores them.
#!/usr/bin/python
# First comment
print("Hello, Python!" # second comment )
This produces the following result −
Hello, Python!
COMMENTS IN PYTHON
1. You can type a comment on the same line after a statement or
expression −
name = "Madisetti" # This is again comment
2. You can comment multiple lines as follows −
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.
MULTIPLE STATEMENTS ON A SINGLE LINE
The semicolon ; allows multiple statements on the single line given that
neither statement starts a new code block.
example:
a=2; b=a+2 ; print(a,b)
MULTIPLE STATEMENT GROUPS AS SUITES
A group of individual statements, which make a single code block are called suites in Python.
MULTIPLE STATEMENT GROUPS AS SUITES
Compound or complex statements, such as if, while, def, and class
require a header line and a suite.
Header lines begin the statement with the keyword and terminate with a colon : it is followed by suite
PRINT FUNCTION
print(*objects, sep=' ', end='\n', file=[Link], flush=False)
objects - object to the printed. * indicates that there may be more than one object
sep - objects are separated by sep. Default value: ' '
end - end is printed at last
file - must be an object with write(string) method. If omitted it, [Link] will be used which prints objects on the
screen.
flush - If True, the stream is forcibly flushed. Default value: False
PRINT FUNCTION
>>> a,b,c=2,3,5
>>> print(a,b,c)
235
>>> print(a,b,c, sep=',')
2,3,5
>>> print(a,b,c, sep=' ', end='hello')
2 3 5hello
#by default end=\n and sep=‘ ‘
>>> print(a,b,c, sep=‘ ', end='\n')
235
INPUT STATEMENT
The following line of the program displays the prompt, the statement
saying “Press the enter key to exit”, and waits for the user to take
action −
input("\n\nPress the enter key to exit.")