[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
1
Python Programming
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
20
20
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
23
Precedence of Operators:
❑ Listed from high precedence to low precedence.
23
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
28
Operator Precedence:
28
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
29 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.
29
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
30 Input/Output Operations
Example 1:
❑ name = input("Enter your name: ")
❑ print("Hello,", name, "! Welcome!")
❑ If you include the optional <prompt> argument, input() displays it as a prompt to the user
before pausing to read input.
❑ 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.
30
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
31
Example 2:
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)
31
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
32
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:
32
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
33
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]
33
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
34
Example 2:
print('hello’)
print('hello \tPython’)
print('hello \nPython2’)
print('hello '*3)
print('hello'+'Python')
34
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
35
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.
35
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
36
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
36
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
37 The end= Keyword Argument
The keyword argument end=<str> causes output to be terminated by <str> instead of the
default newline:
Example1: Output1:
print('foo', end='/') foo/30/too
print(30, end='/')
print(‘too’)
Example2: Output2:
for n in range(10):
print(n)
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
38 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 to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
39
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 to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
40 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(‘%i %s cost Rs.%.2f' % (6, 'bananas', 1.74))
Output:
6 bananas cost Rs.1.74
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
41 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.
41
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
42
❑ 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'''
42
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
43 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.
43
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
41 References:
▪ [Link]
▪ [Link]
▪ [Link]
▪ [Link]
▪ [Link]
44
[Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT)
Module: M2-R5: Web Designing & Publishing
41 References:
Thank You
45