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

Python Virtual Machine Overview

The document provides an overview of Python programming, focusing on the Python Virtual Machine (PVM) and its role in executing Python code. It explains how Python compiles code into bytecode, which is then interpreted by the PVM into machine code for execution. Additionally, it covers Python variables, data types, keywords, and operators, emphasizing the dynamic nature of Python as a programming language.

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 PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views55 pages

Python Virtual Machine Overview

The document provides an overview of Python programming, focusing on the Python Virtual Machine (PVM) and its role in executing Python code. It explains how Python compiles code into bytecode, which is then interpreted by the PVM into machine code for execution. Additionally, it covers Python variables, data types, keywords, and operators, emphasizing the dynamic nature of Python as a programming language.

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 PPTX, PDF, TXT or read online on Scribd

NATIONAL INSTITUTE OF ELECTRONICS AND INFORMATION TECHNOLOGY

Sumit Complex, A-1/9, Vibhuti Khand, Gomti Nagar, Lucknow,


1

Python Programming
Day2
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 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4 Internal Working of Python
 Python doesn’t convert its code into machine code, somethingthat hardware
can understand.
 It actually convertsit into something called byte code. So
withinpython, 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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Module: M2-R5: Web Designing & Publishing
Machine
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 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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).
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
1 Python Keywords & Operators
3
Python Keywords:
 Python Keywords are special reserved words which convey a special meaning
to the compiler/interpreter.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
1 Python Keywords & Operators
4
 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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
1
5
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
16
Types of Operators in Python:
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Identity Operators and Membership Operators
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
1
7
Mathematical/Arithmetic Operators:
 Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication and division.
 Assume a=5 and b=3
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
1
8
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
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
1
9
Relational Operators:
 Relational operators compares the values.
 It either returns True or False according to the condition.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
2
0
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
2
1
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
False
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
22
Boolean Operators:
There are two bool type literals. They are True &
False. x=False
Operator Example Meaning
y=True
z=x and y
print(z) And x and y If both x and y are true then it return True otherwise
z=x or y 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
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
2
3
Precedence of Operators:
 Listed from high precedence to low precedence.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
2
4
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).
.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
2
5
Identity operators:
Example:
a=10
b=10
c=a is b
print(c)
print(id(a
))
print(id(b
)) Output
True
14073528
1390512
14073528
1390512
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
2
6
Identity operators:
#for Array
from array
import* Output
a=array('i', False
[1,2,3]) 1692318410608
b=array('i',
1692318410672
[1,2,3]) c= a is
False
b
print(c) 1692313475784
#for list
print(id(a 1692313474888
x=[1,2]
))
y=[1,2]
print(id(b
z= x is y
))
print(z)
print(id(x
))
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
35
Operator Precedence:

35
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
3 Input/Output Operations
6
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
3 Input/Output Operations
7
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
3
8
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
3
9
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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4
0
Example 4:
str=input('enter you city: ')
print(str) Outpu
t: 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.:
print(x) 37.5
37.5

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


print(x)
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4
1
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:
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4
2
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]
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4
3 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="&")
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4
4 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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
4
5 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
[Unit 1: Introduction
Course
Course : Python
: Basic to Web Design]
Programming
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
: Managing
VirtualInput and Output withModule:
Day2 M2-R5: Web Designing & Publishing
Examples
Machine
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=' ')
[Unit 1: Introduction
Course
Course : Python
: Basic to Web Design]
Programming
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
: Managing
VirtualInput and Output withModule:
Day2 M2-R5: Web Designing & Publishing
Examples
Machine
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
[Unit 1: Introduction
Course
Course : Python
: Basic to Web Design]
Programming
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
: Managing
VirtualInput and Output withModule:
Day2 M2-R5: Web Designing & Publishing
Examples
Machine
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:
[Unit 1: Introduction
Course
Course : Python
: Basic to Web Design]
Programming
Python Course: NIELIT ‘O’ Level (IT)
Day 4: Python
: Managing
VirtualInput and Output withModule:
Day2 M2-R5: Web Designing & Publishing
Examples
Machine
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 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
5 Comments & Documentation in Python
0  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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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'''
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
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
"""
return a + b
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Module: M2-R5: Web Designing & Publishing
Python Virtual Machine
5 Assignment:
3
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 operationand 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.
[Unit 1: Introduction
Course to Web Design]
: Basic Python Course: NIELIT ‘O’ Level (IT)
Day2 : Python Virtual Module: M2-R5: Web Designing & Publishing
Machine
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 Module: M2-R5: Web Designing & Publishing
Machine
41 References:

Thank
55
You

You might also like