Class XI
Class XI
1
• Python is free – is open source distributable software.
• Python is easy to read and learn – has a simple English language syntax and is uncluttered by punctuation.
• Python has extensive libraries – provides a large standard libraries for easy integration into your own
programs. The presence of libraries also makes sure that you don’t need to write all the code yourself and
can import the same from those that already exist in the libraries.
• Python is interactive – has a terminal for debugging and testing snippets of code (You can actually sit at a
Python prompt and interact with the interpreter directly to write your programs).
• Python is portable – runs on a wide variety of hardware platforms (Mac/Linux/Windows)and has the
same interface on all platforms.
• Python is interpreted – If you’re familiar with any languages like C++ or Java, you must first compile it, and
then run it. But in Python, there is no need to compile it. Internally, its source code is converted into an
immediate form called bytecode. So, all you need to do is to run your Python code without worrying about
linking to libraries, and a few other things.
3
If Python is installed in your system, then you can start at command line or
IDLE (It is a graphical integrated development environment for Python).
Click Start.
Click All Programs >> Python 3.7 >> Python 3.7 (64-bit)
You will see that the Python (command line) window appears with the Python >>> primary prompt
as shown below:
4
To start Python at shell prompt:
• Click Start >> All Programs >> Python 3.7 >> IDLE (Python 3.7 32-bit)
• Double click on the desktop
When you start the Python GUI program, you will see the Python Shell window containing
the Python >>> primary prompt.
5
There are two modes to use the python interpreter:
i. Interactive Mode
ii. Script Mode
i. Interactive Mode: Without passing python script file to the interpreter, directly
execute code to Python (Command line).
Example:
>>>6+3
output: 9
Note: >>> is a prompt , which python interpreter uses to indicate that it is ready. The
interactive mode is better when a programmer deals with small pieces of code.
6
Script Mode: In this mode source code is stored in a file with the .py extension and use the
interpreter to execute the contents of the file. To execute the script by the interpreter, you
have to tell the interpreter the name of the file.
Example:
To run the script you have to follow the steps given below:
Step-1: In Python Shell window, click File >> New File or click Ctrl+N.
Note. If you already have a program file with .py extension, click File >> Open Or click
Ctrl+O to open .py file.
Notice that an Untitled window appears as shown below:
Click here to create a program in new window
7
Step-2: Click File >> Save or Press Ctrl + S. Like other windows applications, in Save As
window, select a folder, type the name of the file (test) and click Save button. You will
notice that the named file is saves as .py extension.
To run Python program:
Step-3: Click Run >> Run Module command or Click F5 key.
If the code is written correctly, you will see the output in the Python Shell window with
following output:
8
Python Character Sets & Tokens
Letters: Both lowercase (a, b, c, d, ...., etc.) and uppercase (A, B, C, D, ...., etc..) letters
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
9
Python Tokens
Python Tokens
Delimiters
Keywords: Reserved words in the library of a language. All the keywords are in lowercase
except 03 keywords (True, False, None).
Ex: and, as, break, class, continue, def, elif etc
Identifiers: The name given by the user to the entities like variable name, class-name,
function-name etc.
Ex: roll_no, salary, name, sep98_score
10
Literals: Literals are the constant value. Literals can be defined as a data that is given in
a variable or constant.
11
Operators: An operator performs the operation on operands. Basically there are two
types of operators in python according to number of operands.
• Unary Operator: Performs the operation on one operand.
Example:
x=+2
y=-x
12
Variables
Variable is a container in which a data value can be stored within the computer’s memory.
The stored value can then be referenced using the variable’s name. The programmer can
choose any name for a variable, except the python keywords.
Variable = Value/Expression
L-Value Assignment R-Value
Operator
• Variable is an identifier.
• Value/Expression is an expression with a value or some calculation, function, etc.
• Variable refers to an L-value because it resides in memory and it is addressable
and Value/Expression is an R-value i.e., a value that is not L-value.
In concept, R-value correspond to temporary objects returned from functions, while L-values
correspond to objects you can refer to, either by name or by following a L-value reference.
Python first evaluates the RHS expression and then assigns to LHS.
13
Examples:
>>> Num1 = 10 # Assigns an integer value
>>> Num2 = 20 # Assigns an integer value
>>> sumN = Num1 + Num2 # An expression
>>> PiValue = 3.14 # A floating-point value
>>> MyName = "XYZ" # A string
Python is Dynamic – Python variables adjust the memory allocation to suit the various
data values assigned to their variables (dynamic).
Initialize variables with integer values. Assign a string value to the variable.
A, B = 10, 20 Message = 'Python in middle schools.'
C=A+B
Assign a float value to the variable. Assign a Boolean value to the variable.
PiValue = 4.13 Flag = True
14
Variable Declaration Rules
Some basic rules to declare variables in Python:
• Variable names can be as short as a single letter either in uppercase or
lowercase.
• Variable names must begin with a letter or an underscore ('_').
• After the first letter or underscore, they can contain letters, numbers, and additional
underscores. For example totalCost, y2014, _sysVal, _my_name, _my_name, etc.
• Variable names are case-sensitive. For example, myname and myName are not the same
names, they are two different variables.
• Variable names should not be reserved words or keywords. That is, variables cannot have the
same name as a Python command or a library function. For example, while, if, etc. cannot be
declared as variable name.
• No blank spaces are allowed between variable name. Also, it cannot use any special
characters (such as @, $, %, etc. ) other than underscore ('_').
• Variables or identifiers can be of unlimited length. 15
Valid Variable Names
roll_no, Salary ,sep98_score, index_age, percentageamount, dob, Cust_no, D_o_j, _my_name
>>> while
SyntaxError: invalid syntax because variable is a reserve word
>>> myname@
SyntaxError: invalid syntax because variable contains a special character
>>> 98Sep_score
SyntaxError: invalid syntax because variable start with a digit
16
Way to Assign Value to Variable
There are different ways to assign value to a variable.
x –351.0
17
A variable can be assigned many times. It always retains the value of the most
recent assignment.
7 0 10
Example:
>>> Var1 = 7
>>> Var1 # Returns: 7
>>> Var1 = 0 # Returns: 0
>>> Var1
>>> Var1 = Var1 + 10
>>> Var1 # Returns: 10
18
Assigning a value to multiple variables: In Python, we can assign a value to multiple variables.
Example:
>>> a = b = c = d = 5
>>> print (a, b, c, d) # Returns: 5, 5, 5, 5
Here, all the variables a, b, c and d are assigned a single value called 5.
Multiple assignments in a single line. You can even assign values to multiple variables in a single line.
While assigning, the left and the right side must have the same number of elements. The general
format is:
<var1>, <var2>, <var3>, …. <varN> = <expr1>, <expr2>, <expr3>, ….<exprN>
This is called simultaneous assignment. Semantically, this tells Python to evaluate all the
expressions on the right-hand side and then assign these values to the corresponding
variables named on the left-hand side. If the variables in left-
hand side differs with
Example:
the values with right-
>>> a1, a2, a3 = 10, 20, 30
hand side and vice-
>>> print (a1, a2, a3)
versa, then an exception
10, 20, 30
will raised and the
Here, Python evaluates the entire right-hand side (10, 20, 30) of the program will stop.
statement. Then, it matches values with destinations on the left-
hand side (a1, a2 and a3), i.e., the left most value 10 is assigned to
a1, 20 to a2 and 30 to a3. 19
Example:
p, q, r= 5, 10, 7
q, r, p = p+1, q+2, r-1
print (p,q,r)
Note: Expressions separated with commas are evaluated from left to right and assigned in
same order.
>>> a,b,a=1,2,3
>>> print(a,b)
?
>>> a,b,c=b+1,a+1,b+1
>>> print(a,b,c)
?
20