Python Module1
Python Module1
Text books
1. Paul Gries , Jennifer Campbell, Jason Montojo, Practical
Programming: An Introduction to Computer Science Using Python 3,
Pragmatic Bookshelf, 2/E 2014
2. James Payne, Beginning Python: Using Python 2.6 and Python 3,
Wiley India, 2010
MODULE 1
INTRODUCTION: Python Overview- History of python,
Features, basic syntax, Writing and executing simple
program, Basic Data Types such as numbers, etc Declaring
variables, Performing assignments, arithmetic operations,
Simple input-output, Precedence of operators, Type
conversion, Control statements: Terminating loops,
skipping specific conditions.
INTRODUCTION
Python Overview- History of python
• Python is a general-purpose interpreted, interactive, object-
oriented and high-level programming language.
• It was created by Guido van Rossum in (1985- 1990) the late
eighties and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands. Python
was named after Monty Python.
• Python is derived from many other languages, including ABC,
Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and
other scripting languages.
• Python is copyrighted. Like Perl, Python source code is now
available under the GNU General Public License (GPL).
Getting Python
The most up-to-date and current source code, binaries,
documentation, news, etc., is available on the official website of
Python https://www.python.org/
You can download Python documentation
from https://www.python.org/doc/. The documentation is
available in HTML, PDF, and PostScript formats.
Python Releases for Windows
Latest Python 3 Release - Python 3.6.4
Latest Python 2 Release - Python 2.7.14
PYTHON'S FEATURES INCLUDE :
OUTPUT
Hello Python
Note: If you are running old version of Python, then you need not
use print statement with parenthesis as in print "Hello, Python!“
would work
2. Script Mode Programming
Invoking the interpreter with a script parameter begins execution
of the script and continues until the script is finished. When the
script is finished, the interpreter is no longer active.
OUTPUT
Hello, Python!
IDLE
IDLE (short for integrated development environment or integrated
development and learning environment) is an integrated development
environment for Python.
A simple program to add two numbers
Open a newfile in IDLE
write the following code
a=10
b=5
c=a+b
print (c)
OUTPUT
15
PYTHON IDENTIFIERS
• 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.
• The triple quotes are used to span the string across multiple lines.
For example, all the following are legal −
1. word = ‘word’
2. sentence = “This is a sentence.”
3. paragraph = """This is a paragraph. It is made up of multiple lines
and sentences."""
COMMENTS IN PYTHON
• A hash sign (#) that is not inside a string literal begins a comment.
• All characters after the # and up to the end of the physical line are
part of the comment and the Python interpreter ignores them.
For example -
# My First comment
print ("Hello, Python!")
# second comment
For Example—
name = “Vriddhi” # This is again comment
# 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.
• Here is a sample snip using the semicolon −
a1=3;b1=4;c=a1+b1;print(c)
MULTIPLE STATEMENT GROUPS AS
SUITES
1. A group of individual statements, which make a
single code block are called suites in Python.
2. Compound or complex statements, such as if, while,
def, and class require a header line and a suite.
3. Header lines begin the statement (with the
keyword) and terminate with a colon ( : ) and are
followed by one or more lines which make up the
suite.
For example −
if expression :
suite
elif expression :
suite
else : suite
DATA TYPES IN PYTHON
Python has five standard data types −
1. Numbers
2. Strings
3. Lists
4. Tuples
5. Dictionary
PYTHON NUMBERS
Syntax
for iterating_var in sequence:
statements(s)
SYNTAX FOR NESTED FOR AND
NESTED WHILE LOOP
You can use one or more loop inside any another while, for
or do..while loop.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
PYTHON CONTROL STATEMENTS
Controlling Loops
• As a rule, for and while loops execute all the
statements in their body on each iteration.
• However, sometimes it is handy to be able to
break that rule.
• Python supports the following control
statements:
1. Break :-- which terminates execution of the loop
immediately
2. Continue :-- which skips ahead to the next iteration.
3. Pass :-- The pass statement in Python is used when a
statement is required syntactically but you do not
want any command or code to execute.
PYTHON BREAK STATEMENT
• It terminates the current loop and resumes execution at the next
statement, just like the traditional break statement in C.
• The break statement can be used in both while and for loops.
Syntax
The syntax for a break statement in Python is as follows −
break
Flow Diagram
USAGE OF BREAK IN FOR STRUCTURE
for letter in 'Python':
if letter == 'h':
break
print 'Current Letter :', letter
OUTPUT
Current Letter : P
Current Letter : y
Current Letter : t
USAGE OF BREAK IN WHILE STATEMENT:
var = 10
while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break
print "Good bye!
OUTPUT
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
PYTHON CONTINUE STATEMENT
• The continue statement rejects all the remaining statements in
the current iteration of the loop and moves the control back to
the top of the loop.
• The continue statement can be used in both while and for loops.
Syntax
The syntax for a continue statement in Python is as follows −
continue
Flowdiagram
USAGE OF CONTINUE IN FOR
STRUCTURE
OUTPUT
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
USAGE OF CONTINUE IN WHILE
STRUCTURE
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
print "Good bye!“
OUTPUT
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!
PYTHON PASS STATEMENT
• It is used when a statement is required syntactically but
you do not want any command or code to execute.
• The pass statement is a null operation; nothing happens
when it executes.
• The pass is also useful in places where your code will
eventually go, but has not been written yet.
Syntax
The syntax for a pass statement in Python is as follows −
pass
USAGE OF PASS IN FOR STRUCTURE
OUTPUT
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!