Program:
A computer program is a set of instructions that perform some task.
Features of Python Programming Language:
• Python is a high-level language. It is a free and open source language.
• It is an interpreted language, as Python programs are executed by an interpreter.
• Python programs are easy to understand as they have a clearly defined syntax and relatively simple
structure.
• Python is case-sensitive. For example, NUMBER and number are not same in Python.
• Python is portable and platform independent, means it can run on various operating systems and hardware
platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many popular web services and applications are built using
Python.
• Python uses indentation for blocks and nested blocks.
Execution Modes:
Interactive Mode:
To work in the interactive mode, we can simply type a Python statement on the >>> prompt directly. As soon as
we press enter, the interpreter executes the statement and displays the results.
Working in the interactive mode is convenient for testing a single line code for instant execution. But in the
interactive mode, we cannot save the statements for future use and we have to retype the statements to run them
again.
Script Mode:
In the script mode, we can write a Python program in a file, save it and then use the interpreter to execute it.
Python scripts are saved as files where file name has extension “.py”.
Keywords:
Keywords are reserved words. Each keyword has a specific meaning to the Python interpreter, and we can use a
keyword in our program only for the purpose for which it has been defined.
Python Keywords:
,Identifier:
In programming languages, identifiers are names used to identify a variable, function, or other entities in a
program.
The rules for naming an identifier in Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_). This may be
followed by any combination of characters a–z, A–Z, 0–9 or underscore (_). Thus, an identifier cannot
start with a digit.
• It should not be a keyword.
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
Operators:
An operator is used to perform specific mathematical or logical operation on values. The values that the operators
work on are called operands.
For example, in the expression 10 + num, the value 10, and the variable num are operands and the + (plus) sign
is an operator.
Variables:
A variable is a named storage location that holds some data.
A Variables in Python is only a name given to a memory location, all the operations done on the variable affects
that memory location.
For example, >>>num=100
Here, num is a variable.
Literals:
They are values directly used in programs.
For example,
>>> A=4*L
>>> S=’Hello’ * 3
Here, 4, 3, ‘Hello’ are literals.
Comments:
Comments are used to add a remark or a note in the source code. Comments are not executed by interpreter. They
are added with the purpose of making the source code easier for humans to understand.
In Python, a comment starts with # (hash sign). Everything following the # till the end of that line is treated as a
comment and the interpreter simply ignores it while executing the statement.
Example:
#Variable amount is the total spending on grocery
amount = 3400
#totalMarks is sum of marks in all the tests of Mathematics
totalMarks = test1 + test2 + finalTest
,Tokens:
They are the smallest individual units in a program.
Keywords, Identifiers, Literals, Operators, etc. are tokens.
, Introduction to Programming in Python
Arithmetic Operators
Operator Operation Description Expression Result
Adds two operands
10 + 5 15
+ Addition If the operands are strings,
‘Comp’ + ‘Sc’ ‘CompSc’
this operator concatenates the
strings.
Subtracts right operand from 10 - 5 5
- Subtraction
the left
Multiplies two operands
10 * 5 50
* Multiplication If one operand is string and
the other one is integer n, the ‘study’ * 3 ‘studystudystudy’
string is repeated n times.
Divides left operand by right 2.0
/ Division
(float result)
Divides and returns only the 10 // 3 3
// Floor Division
integer part
Returns remainder after 10 % 3 1
% Modulus
division
Raises left operand to the 2 ** 3 8
** Exponentiation
power of right
Relational Operators
Operator Meaning Description Expression Result
==
Evaluates to True if both operands are equal; 5 == 5 True
Equal to 6 == 4 False
otherwise evaluates to False
!=
Evaluates to True if operands are not equal; 5 != 3 True
Not equal to 4 != 4 False
otherwise evaluates to False
>
Evaluates to True if left operand is greater than 5 > 3 True
Greater than 10 > 12 False
right; otherwise evaluates to False
<
Evaluates to True if left operand is less than right; 6 < 5 False
Less than 4 < 6 True
otherwise evaluates to False
>=
Greater than Evaluates to True if left operand is greater than or 5 >= 5 True
or equal to equal to right; otherwise evaluates to False 10 >= 12 False