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

Python Virtual Machine Overview

The document outlines a course on Basic Python as part of the NIELIT 'O' Level (IT) program, focusing on the Python Virtual Machine (PVM) and its internal workings. It covers topics such as Python program execution, variables, data types, keywords, and operators. The course aims to provide foundational knowledge for web design and publishing using Python.

Uploaded by

Yapri Jamatia
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 views55 pages

Python Virtual Machine Overview

The document outlines a course on Basic Python as part of the NIELIT 'O' Level (IT) program, focusing on the Python Virtual Machine (PVM) and its internal workings. It covers topics such as Python program execution, variables, data types, keywords, and operators. The course aims to provide foundational knowledge for web design and publishing using Python.

Uploaded by

Yapri Jamatia
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

NATIONAL

[Unit INSTITUTE
1: Introduction
Course to Web Design]
: Basic Python Course:
OF ELECTRONICS ANDNIELIT ‘O’ Level (IT)
INFORMATION TECHNOLOGY
Day2 Sumit
: Python VirtualComplex,
Machine A-1/9, Vibhuti Khand,
Module: GomtiWeb
M2-R5: Nagar, Lucknow,
Designing & Publishing
1

Python Programming
Day2
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
22 Index
 PVM (Python Virtual Machine)
 How PVM works
 Basics of Python Program Execution
 Variables & Data Types
 Python Keywords & Operators
 Input/Output Operations
 Comments & Documentation in Python
 Flowchart
 Example
 Assignment
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
3 Internal Working of Python
 Python is an interpreted, object-oriented, high-level programming language and
platform independent as well.
 Python doesn’t convert its code into machine code directly.
 Python uses code modules that are interchangeable instead of a single long list of
instructions that was standard for functional programming languages.
 The standard implementation of python is called “cpython”. It is the default and widely used
implementation of the Python.
 We all know that a computer only understands machine language and every programming
language converts its code to machine language. This is done by a compiler of that language.
The Python compiler also does the same thing but in a slightly different manner.

3
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
4 Internal Working of Python
 Python doesn’t convert its code into machine code, something that hardware can
understand.
 It actually converts it into something called byte code. So within python, compilation
happens, but it’s just not into a machine language.
 It is into byte code and this byte code can’t be understood by CPU. So we need actually an
interpreter called the python virtual machine.
 The python virtual machine executes the byte codes.

4
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
5 PVM(Python Virtual Machine)
 Once we write a Python program, it can run on any platform without rewriting once again.
 Since Computers understand only machine code that comprises 1s and 0s. Python Compiler
converts the program source code into another code, called byte code. Each Python program
statement is converted into a group of byte code instructions.
 Python Virtual Machine (PVM) takes those byte codes converts those instructions into
machine code so that the computer can execute those machine code instructions and display
the final output.
 PVM is nothing but a software/interpreter that converts the byte code to machine code for
given operating system.

5
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
6 How PVM works (Bytecode, Interpretation)
 The interpreter in the PVM converts the byte code into machine code and sends that
machine code to the computer processor for execution.
 The PVM handles memory management, garbage collection, and other runtime tasks.
 PVM is also called Python Interpreter and this is the reason Python is called an
Interpreted language.
 We can't see the Byte Code of the program because this happens internally in memory.

6
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
7 Basics of Python Program Execution
 We write the python program in a file with a .py extension
 Than we execute that python code, the execution of the Python program involves two steps:
 Compilation: The byte code instructions are created in the .pyc file. The .pyc file is not
explicitly created as Python handles it internally but it can be viewed with the following
command:

 Interpreter: The next step involves converting the byte code (.pyc file) into machine code.
This step is necessary as the computer can understand only machine code (binary code).

7
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
8 Python Variables & Data Types
 Variable is a name which is used to refer memory location. Variables are also
known as identifier and used to hold value.
 Python has no command for declaring a variable.
 Variables do not need to be declared with any particular type and can even
change type after they have been set.
 Example:
a=10 Hence a is the variable int type
b=10.456 Hence b is the variable float type
c=“Hello” Hence c is the variable String type

8
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
9
 A variable name must start with a letter or the underscore character.
 A variable name cannot start with a number.
 A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
 Variable names are case-sensitive.
 Variables can hold values of different data types. Python is a dynamically
typed language hence we need not define the type of the variable while
declaring it. The data types defined in Python are given below.
• Numeric Type
• Boolean Type
• Sequence Type

9
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
10

1. Numeric Type:
There are four numeric types in Python
• Int – signed integer such as 12, 3, 4 etc.
• Long - long integers used for a higher range of values like
908090800L,
• Float - floating point numbers like 1.9, 9.902, 15.2, etc
• Complex - complex numbers like 2.14j, 2.0 + 2.3j, etc.

10
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
11

2. Boolean Type:
Data with one of two built-in values True or False. Notice that 'T' and 'F' are capital.
true and false are not valid booleans and Python will throw an error for them.

Example:
a=10>5
print(a) # display True
a=6>10
print(a) # display False

11
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
12
3. Sequence Type:
A sequence is an ordered collection of similar or different data types. Python has the following
built-in sequence data types:
String: A string value is a collection of one or more characters put in single, double or triple
quotes.
List: The list can contain data of different types. The items stored in the list are separated with
a comma (,) and enclosed within square brackets [].
Tuple: Tuples also contain the collection of the items of different data types. The items of the
tuple are separated with a comma (,) and enclosed in parentheses ().
Dictionary: Dictionary is an ordered set of a key-value pair of items. It is like an associative
array or a hash table where each key stores a specific value.
Set: Set is an unordered collection of data type that is inerrable, mutable and has no duplicate
elements. The order of elements in a set is undefined though it may consist of various
elements
Note: List, Tuple, Dictionary and Set data types are discussed in later.
12
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
13 Python Keywords & Operators
Python Keywords:
 Python Keywords are special reserved words which convey a special meaning
to the compiler/interpreter.

13
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
14 Python Keywords & Operators
 Python Keywords in Python are reserved words that can not be used as a variable name,
function name, or any other identifier.
 Python's built-in methods and classes are not the same as the keywords. Built-in methods
and classes are constantly present; however, they are not as limited in their application as
keywords.
 Python contains thirty-five keywords in the most recent version, i.e., Python 3.8.

14
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
15
Python Operators:
 In Python programming, Operators in general are used to perform operations on values and
variables. These are standard symbols used for logical and arithmetic operations.
 They are applied on operand(s), which can be values or variables. Operators when applied
on operands form an expression.
 Operators are categorized as Arithmetic, Relational, Logical and Assignment.
 Value and variables when used with operator are known as operands.

15
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
16
Types of Operators in Python:
[Link] Operators
[Link] Operators
[Link] Operators
[Link] Operators
[Link] Operators
[Link] Operators and Membership Operators

16
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
17
Mathematical/Arithmetic Operators:
 Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication and division.
 Assume a=5 and b=3

17
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
18
Assignment Operators:
 These operator are useful to store the right side value into a left side variable
Assume x=20, y=10 and z=5
Assume a=5 and b=3

18
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
19
Relational Operators:
 Relational operators compares the values.
 It either returns True or False according to the condition.

19
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
20

20
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
21
Logical Operators:
 In the case of logical operators, False indicates 0 and True indicates any other number.
x=1
y=2
z=x and y
print(z)
z=x or y
print(z)
z=not x
print(z)

Output
2
1
21 False
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
22
Boolean Operators:
 There are two bool type literals. They are True & False.
x=False
y=True Operator Example Meaning
z=x and y
print(z)
z=x or y And x and y If both x and y are true then it return True otherwise
False
print(z)
z=not x
Or x or y If either x or y is true then return True otherwise False
print(z)
Not not x If x is true then return false
Output
False
True
True
22
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
23
Precedence of Operators:
 Listed from high precedence to low precedence.

23
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
24
Special Operator:
 Identity operators:
• is and is not are the identity operators in Python.
• They are used to check if two values (or variables) are located on the same part of the
memory.
• Two variables that are equal does not imply that they are identical.

 Membership operators:
• in and not in are the membership operators in Python.
• They are used to test whether a value or variable is found in a sequence (string, list,
tuple, set and dictionary).
.

24
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
25
Identity operators:
Example:
a=10
b=10
c=a is b
print(c)
print(id(a))
print(id(b))
Output
True
140735281390512
140735281390512

25
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
26
Identity operators:
#for Array
from array import*
a=array('i',[1,2,3]) Output
b=array('i',[1,2,3]) False
c= a is b 1692318410608
print(c)
1692318410672
print(id(a))
False
print(id(b))
1692313475784
#for list 1692313474888
x=[1,2]
y=[1,2]
z= x is y
print(z)
print(id(x))
26 print(id(y))
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
27
Membership operators:
x='Hello Python'
y=2
z=[1,2,3,4]
print('H' in x)
print('y' in x)
print('p' not in x)
Output:
print('h' not in x)
True
print(y in z)
True
print(5 in z)
True
print(2 not in z)
False
True
False
False
27
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
28
Bitwise operators:
 The bitwise operators perform bit by bit operation on the values of the two operands. These
Operator is not generally used it is used in special application. List of Bitwise operators are..

28
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
29
Bitwise operators AND(&):
 & Operator

Example:
a=10 0000 1 0 1 0
b=15 0000 1 1 1 1
1010
a=10
b=15
k=a&b
print(k)

Output:10

29
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
30
Bitwise operators OR(|):
 | Operator

Example:
a=10 0000 1 0 1 0
b=15 0000 1 1 1 1
1010
a=10
b=15
k=a|b
print(k)

Output:15

30
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
31
Bitwise operators XOR(^):
 ^ Operator

Example:
a=10 0000 1 0 1 0
b=15 0000 1 1 1 1
1010
a=10
b=15
k=a|b
print(k)

Output:5

31
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
32
Bitwise operators NOT(~):
 ~ Operator

Example:
Work on Single Operand LSB
Binary 1’s comp 1’s complements 2’s Complements
a=10 1 0 1 0 0101 1010 1010+1=1011
MSB
After Using Not ~a: 1 0 1 1 (It Just add the binary value of 10 with 1 use negative sign)
a=10
print(~a)

Output:-11

32
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
33
Bitwise Left and Right Shift Operator:

33
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
34
Bitwise Left and Right Shift Operator:
Example:
x=2 #Binary Equivalent of 2 is 0010 Left Shift
y=7 #Binary Equivalent of 7 is 0111 z=y<<1
Right Shift print(z)
z=y>>1 z=y<<2
print(z)
print(z)
Output z=y<<3
z=y>>2 print(z)
3
print(z)
1
z=y>>3 0
print(z)
34
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
35
Operator Precedence:

35
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
36 Input/Output Operations
1. Reading Input From the Keyboard:
 Programs often need to obtain data from the user, usually by way of input from the
keyboard. The simplest way to accomplish this in Python is with input().
 input([<prompt>])
 Reads a line of input from the keyboard.
 input() pauses program execution to allow the user to type in a line of input from the
keyboard. Once the user presses the Enter key, all characters typed are read and returned as
a string.

36
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
37 Input/Output Operations
Example 1:
 >>> s = input()
 foo bar baz
 >>> s
 'foo bar baz'
 If you include the optional <prompt> argument, input() displays it as a prompt to the user
before pausing to read input.

37
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
38
Example 2:
 >>>
 >>> name = input('What is your name? ')
 What is your name? Winston Smith
 >>> name
 'Winston Smith'
 input() always returns a string. If you want a numeric type, then you need to convert the
string to the appropriate type with the int(), float(), or complex() built-in functions.

38
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
39
Example 3:
 >>> n = input('Enter a number: ')
 Enter a number: 50
 >>> print(n + 100)
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 TypeError: must be str, not int
 >>> n = int(input('Enter a number: ‘))
 Enter a number: 50
 >>> print(n + 100)
 11 150

 In the example above, the expression n + 100 on line 3 is invalid because n is a string
and 100 is an integer. Line 8 converts n to an integer so the print() statement succeeds.

39
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
40

Example 4:
str=input('enter you city: ')
print(str) Output:
enter you city: Lucknow
str=input('enter a number: ') Lucknow
x=int(str) enter a number: 4
4
print(x) enter any no.: 54
54
x=int(input('enter any no.: ')) enter any no.: 37.5
print(x) 37.5

x=float(input('enter any no.: '))


print(x)

40
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
41

2. Writing Output to the Console

 In addition to obtaining data from the user, a program will also usually need to present data back
to the user. You can display program data to the console in Python with print().
 Unformatted Console Output
 To display objects to the console, pass them as a comma-separated list of argument to print().
 print(<obj>, ..., <obj>)
 Displays a string representation of each <obj> to the console.
 By default, print() separates each object by a single space and appends a newline to the end of
the output:

41
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
42

Example 1:
 >>> fname = 'Winston'
 >>> lname = 'Smith'

 >>> print('Name:', fname, lname)


 Name: Winston Smith

 Any type of object can be specified as an argument to print(). If an object isn’t a string,
then print() converts it to an appropriate string representation displaying it:
 >>>
 >>> a = [1, 2, 3]
42
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
43
Example 2:
print('hello')
print('hello \tPython')
print('hello '*3)
print('hello'+'Python')
a,b=2,4
print(a)
print(a,b)
print(a,b,sep=",")
print(a,b,sep=":")
print(a,b,sep="____")
print("hello")
print("welcome",end="")
print("python")
print(1,2,3,sep=",",end="&")

43
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
44
Keyword Arguments to print():
 print() takes a few additional arguments that provide modest control over the format of the output.
Each of these is a special type of argument called a keyword argument.
 This introductory series of tutorials will include a tutorial on functions and parameter passing so
you can learn more about keyword arguments.
 Important points About keyword arguments.
• Keyword arguments have the form <keyword>=<value>.
• Any keyword arguments passed to print() must come at the end, after the list of objects to display.

44
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
45
How these keyword arguments affect console output produced by print().
Example:
The sep= Keyword Argument
Adding the keyword argument sep=<str> causes objects to be separated by the string <str> instead of
the default single space:
>>> print('foo', 42, 'bar')
foo 42 bar
>>> print('foo', 42, 'bar', sep='/')
foo/42/bar
To squish objects together without any space between them, specify sep='':
>>> print('foo', 42, 'bar', sep=‘’”)
foo42bar
45
Course
[Unit : Python
1: Introduction
Course : Basic Programming
to Web Design]
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
Day2 : Managing
VirtualInput and Output with Examples
Machine Module: M2-R5: Web Designing & Publishing
46 The end= Keyword Argument
 The keyword argument end=<str> causes output to be terminated by <str> instead of the default
newline:
Output1:
Example: foo/30/too
print('foo', end='/')
Output2:
print(30, end='/') print('foo', end='/')
print('too’) print(30, end='/')
print('too’)

Example2:
for n in range(10):
print(n)
Example3:
Output3:
for n in range(10):
print(n,end=' ')
Course
[Unit : Python
1: Introduction
Course : Basic Programming
to Web Design]
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
Day2 : Managing
VirtualInput and Output with Examples
Machine Module: M2-R5: Web Designing & Publishing
47 The String Modulo Operator
 The String Modulo Operator
 The modulo operator (%) is usually used with numbers, in which case it computes remainder
from division:
 >>>
 >>> 11 % 3
 2
Course
[Unit : Python
1: Introduction
Course : Basic Programming
to Web Design]
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
Day2 : Managing
VirtualInput and Output with Examples
Machine Module: M2-R5: Web Designing & Publishing
48
 With string operands, the modulo operator has an entirely different function: string formatting.
(The two operations aren’t really much like one another. They share the same name because
they are represented by the same symbol: %.)
 Here’s what the syntax of the string modulo operator looks like:
Course
[Unit : Python
1: Introduction
Course : Basic Programming
to Web Design]
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
Day2 : Managing
VirtualInput and Output with Examples
Machine Module: M2-R5: Web Designing & Publishing
49 Example of Format String:
 <format_string> % <values>
 On the left side of the % operator, <format_string> is a string containing one or more
conversion specifiers. The <values> on the right side get inserted into <format_string> in
place of the conversion specifiers. The resulting formatted string is the value of the expression.
 Example:
 print('%d %s cost Rs.%.2f' % (6, 'bananas', 1.74))
 Output:
 6 bananas cost Rs.1.74
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
50 Comments & Documentation in Python
 Comments
Comments in Python can be used to explain any program code. It can also be used to hide the code
as well. Comments are non-executable code.
 Single Line Comment:
In case user wants to specify a single line comment, then comment must start with # (hash).
# single line comment
 Multi Line Comment:
# This is a comment
# that spans multiple
# lines.

50
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
51
 Docstring as Comments:
You can use triple quotes (''' or """) to write multi-line comments, although this is technically a
string (docstring). When not assigned or used, Python ignores it.
'''This is another way to write multi-line comments'''

51
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
52
 Documentation (Docstrings) in PythonA docstring is a special string used to document a specific
block of code, like functions, classes, and modules. Unlike regular comments, docstrings can be
accessed programmatically via the _doc_ attribute.
 Function Docstring: Describes what the function does.
 Example:
def add(a, b):
""" This function adds two numbers.
Parameters:
a (int): First number
b (int): Second number
Returns:
int: Sum of a and b
"""
52 return a + b
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
53 Assignment:
1. The value of a=10 and b=20 find all operation on these two Variables values.
2. Write a Program Perform All Arithmetic Operation (like add, sub, division,
multiply and remainder).
3. Write a Program Perform All Relational operation and find output (like.
>,<,>=,<=,==,!=).
4. Write a Python Program to Calculate Area of circle where radius=5.0.
5. The length & breadth of a rectangle and radius of a circle are input through the
keyboard.
6. Write a program to calculate the area & perimeter of the rectangle, and the
area & circumference of the circle.
7. Employee’s basic salary is input through the keyboard. His dearness
allowance is 40% of basic salary, and house rent allowance is 20% of basic
salary. Write a program to calculate his gross salary.

53
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
41 References:
 [Link]
 [Link]
 [Link]
 [Link]
 [Link]

54
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Machine Module: M2-R5: Web Designing & Publishing
41 References:

Thank You
55

You might also like