0% found this document useful (0 votes)
26 views20 pages

Class XI

Python is a high-level, interpreted programming language known for its readability, extensive libraries, and portability across platforms. It allows users to write code in both interactive and script modes, supports dynamic variable assignment, and has specific rules for variable naming. The document also covers Python's character sets, tokens, operators, and methods for assigning values to variables.

Uploaded by

Soham Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views20 pages

Class XI

Python is a high-level, interpreted programming language known for its readability, extensive libraries, and portability across platforms. It allows users to write code in both interactive and script modes, supports dynamic variable assignment, and has specific rules for variable naming. The document also covers Python's character sets, tokens, operators, and methods for assigning values to variables.

Uploaded by

Soham Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Python is a high-level (human-readable) programing language that is

processed by the Python “interpreter” to produce results.

Interpreter is a language processor which translates high level language


(Source program) statement line by line into a sequence of machine
instructions and then executes these machine instructions before translating
the next source language statement.

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.

• Python is high-level – has automatic memory management.


• Python is extensible – Extensible feature in python refers that you can write some of your Python code
in other languages like C or C++. It means that it can be extended to other languages which makes python
an extensible language. It's not extending the language itself (syntax, constructs, etc), but it lets you
interface python with libraries written in other languages. Simply means you can write code in other
languages in your python source code. 2
Bytecode is program code
that has been compiled
from source code into
low-level code designed
for a software interpreter.
It may be executed by a
virtual machine (such as a
JVM) or further compiled
into machine code, which
is recognized by the
processor.

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:

Python interactive prompt waitingfor


Python command

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.

Python prompt in Python Shell and


active cursor waiting for Python
command

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

Enter Python code overhere

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

Special symbols: _, (, ), [, ], {, }, +, -, *, /, %, !, &, |, ~, ^, <, =, >, $, #, ?, Comma (,), Dot (.),


Colon (:), Semi-colon (;), Single quote ('), Double quote ("), Back slash (\)

White space: Space, Tab (¦), New line (8)

9
Python Tokens

Python Tokens

Keywords Identifiers Operators Literals

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.

• Numeric literals - 3,4+5


• String literals – ‘XYZ’
• Boolean literals - True or False
• Special literals – None
None is used to specify to that field that is not created. It is also used for end of
lists in Python
• Literal Collections: Collections such as tuples, lists and Dictionary.

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

• Binary Operator: Performs operation on two operands.


Example:
x=a+b
y=a-b

Delimiters: A delimiter is a sequence of one or more characters used to specify the


boundary between separate, independent regions in plain text or other data streams.
Example : , ; , ( ), { }, [ ]

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.

Data to be stored in a variable is assigned in a python program declaration statement with


the assignment operator (=). The assignment statement assigns a value to a variable.

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

Invalid variable names with reason


>>> Your Age
SyntaxError: invalid syntax because variable contains space

>>> 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.

Assigning an expression. An expression can be assigned into a variable.


Example:
>>> x = 10 x 10

>>> x = 3.9 * x * (1 – x) # An expression


>>> print (x) # Returns: –351.0

x –351.0

>>> Fahrenheit = 9.0 / 5.0 * Celsius + 32 # An expression


>>> print (Celsius, Fahrenheit) # Returns: 40, 104.0

17
A variable can be assigned many times. It always retains the value of the most
recent assignment.

Var1 = 7 Var1 = 0 Var1 = Var1 + 10

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)

Now the result will be:


6 6 12

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

You might also like