Module 1.1
Module 1.1
Introduction To Python
Text Book - Allen Downey, Jeffrey Elkner, Chris Meyers, “ How to think like a Computer Scientist-
Learning with Python”, Green Tea Press, First edition, 2002.
Module 1 Topics 9 Hours
• Introduction To Python:
• Understanding Python-identifiers, variables, keywords, expressions and
statements.
• Evaluation of expressions
• Operators and operands
• Operator precedence
• Indentation
• Python Program Flow Control:
• Decision making- if, if..else, elif.
• Loops - for, while, for...else, while...else
• Control statements using pass, continue, break.
The Python programming language
• Python is an example of a high-level language
• Other high-level languages you might have heard of are C, C++, Perl, and Java.
• Like high-level language there are also low-level languages, sometimes referred to
as machine languages or assembly languages.
• Computers can only execute programs written in low-level languages.
• Thus, programs written in a high-level language have to be processed before they can
run. This extra processing takes some time, which is a small disadvantage of high-
level languages.
ADVANTAGES OF HIGH-LEVEL LANGUAGES
• It is much easier to program in a high-level language.
• Programs written in a high-level language take less time to write
• They are shorter and easier to read, and they are more likely to be correct.
• High-level languages are portable, meaning that they can run on different kinds of
computers with few or no modifications.
• Low-level programs can run on only one kind of computer and have to be
rewritten to run on another. So almost all programs are written in high-level
languages and Low-level languages are used only for a few specialized
applications.
Converting high-level languages into low-level languages
• Two kinds of programs are available
1. Interpreters
2. Compilers.
• An interpreter reads a high-level program and executes it, meaning that it
does what the program says.
• It processes the program a little at a time, alternately reading lines and
performing computations.
• A compiler reads the program and translates it completely before the
program starts running.
• In this case, the high-level program is called the source code, and the
translated program is called the object code or the executable.
• Once a program is compiled, you can execute it repeatedly without further
translation.
• Python is considered an interpreted language because Python programs are
executed by an interpreter.
• There are two ways to use the interpreter
• Command-line mode
• Script mode.
• In command-line mode, you type Python programs and the interpreter prints
the result:
>>> print (1 + 1)
2
• We can write a program in a file and use the interpreter to execute the
contents of the file. Such a file is called a script.
• Files that contain Python programs have names that end with.py.
• To execute the program, we have to tell the interpreter the name of the script:
>>> python add.py
Print Statement
• It displays a value on the screen.
• Eg: print (“ Hello, world !”)
• The quotation marks in the program mark the beginning and end of the
value; they don’t appear in the result.
• When we type a large integer, we use commas between groups of three digits, as
in 1,000,000. This is not a legal integer in Python, but it is a legal expression:
>>> print (1,000,000)
100
• Python interprets 1,000,000 as a comma-separated list of three integers, which it
prints consecutively.
Exercise Questions
To Familiarize Command-line mode
1. Python Program to Print Hello world
2. Python Program to Add Two Numbers
3. Python Program to Subtract Two Numbers
4. Python Program to read and display a string
5. Python Program to Add Two Numbers using input method
6. Python Program to Subtract Two Numbers using input method
To Familiarize Script mode
8. Python Program to read and display a string
9. Python Program to read and Add Two Numbers
10. Python Program to read and Multiply Two Numbers
Variables or Identifiers
• An Identifiers or Variable is a sequence of characters invented by the
programmer to identify or name a specific object.
• The sequence of characters include letters, digits and underscore.
The following are five rules for naming Identifiers
1. Variable names are case sensitive
eg: A variable named ‘NUMBER’ is not same as the variable named ‘number’.
This two refers to different variables.
2. The first character must be an alphabetic character or an underscore ‘_’.
3. A numeric digit should not be used as the first character of an Identifier.
4. Identifiers may be of any reasonable length, generally eight to ten
characters long. >>> _a=1
5. The identifier name should not be a keyword of Python. >>> print(_a)
1
>>> 76trombones = ‘big parade’
Syntax Error: invalid syntax
• ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class’,
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global’,
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return’,
'try', 'while', 'with', 'yield']
Statements
• A statement is an instruction that the Python interpreter can execute.
• We have seen two kinds of statements:
print and assignment.
• Print statement
• When you type a statement on the command line, Python executes it and
displays the result.
• The result of a print statement is a value.
• Assignment statement
• Assignment statements don’t produce a result.
• A script usually contains a sequence of statements.
• If there is more than one statement, the results appear one at a time as the
statements execute.
• For example, the script
print 1
x=2
print x
• produces the output
1
2
Evaluating expressions
• An expression is a combination of values, variables, and operators.
• If you type an expression on the command line, the interpreter evaluates it
and displays the result:
>>> 1 + 1*5
6
>>> 17
17
• evaluating an expression is not quite the same thing as printing a value.