Python Lab Manual Indexed
Python Lab Manual Indexed
Lab Index
Sub: PYTHON PROGRAMMING Code: 22PLC25B
Expt . No Date Title of the experiment Marks Signature
1 Write an algorithm and Draw Flowchart to
compute Simple interest
2 Write an algorithm and Draw Flowchart to find
the Largest among Two Integer Numbers
3 Program to Compute simple interest and
Compound interest for a given principal amount,
rate of Interest and duration in years.
4 Write a Python program that prompts the user to
enter the quantity of items (an integer) and the
price per item (a floating-point number). Format
the output string to display the total cost with
appropriate formatting.
5 Write a Python program that accepts angle value
in degrees, length X and Y and evaluate the
equation Z=(X*cos(A)) +(Y*sin(A))
6 Write a Python program to find roots of a
quadratic equation.
7 Write a Python program to compute the Factorial
for a given number.
8 Write a python program to find the average of
first n natural numbers.
9 Write a Python program to convert a lowercase string to
uppercase and vice versa and swap the case of the string
which is a mix of lower and upper cases.
10 Write a Python function program to find the sum of
even numbers or odd numbers up to the number
entered by the user
11 Write a python program to find all the positive and
negative numbers in a given list.
12 Write a Python program that creates a list of tuples
called "students" to store the details of multiple students.
Each tuple should contain the student's name, age, and
average score. Prompt the user to enter the details for
three students and store them in the "students" list. Print
the list of tuples
13 Create a dictionary to store the country name and
capitals of the country. Perform the following operations
on dictionary
i) Adding a country to the
dictionary
ii) Deleting a country from the
dictionary
1
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
2
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Lab Exercise -1
Algorithms and Flowcharts
Objective: To understand the concepts of algorithms and drawing flowcharts which
are important to solve any given problem.
Theory
1. Definition of Algorithm: -
• A set of rules/instructions that must be followed when solving a particular problem or
a specific task.
• Algorithms can be simple and complex depending on what you want to achieve.
• Algorithm is generally developed before the actual coding is done.
3
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
3. Flowchart
The solution of any problem in picture form is called flowchart. It is the one of the most
important techniques to depict an algorithm.
4. Advantage of Flowchart:
• Easier to understand
• Helps to understand logic of problem
• Easy to draw
• Complex problem can be represent using less symbols
• It is the way to documenting any problem
• Helps in debugging process
5. Various symbols – used in flogorithm tool
4
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Application Given: -
1. Compute simple interest
Write Algorithm: -
Observation/ Output
Attach printout of flowchart from Flogarithm tool:
5
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Write Algorithm: -
Observation/ Output
Attach printout of flowchart from Flogarithm tool:
6
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Outcomes: -
1. Each student will be able to write the algorithm and draw the flowchart for given
problems which will help them in problem solving. [ PO-1,3,5,10,12]
Category Advanced Proficient Approaching Beginning Marks
( 5 Marks) ( 3 Marks) Proficiency ( 1 Marks)
( 2 Marks)
1. Syntax Program compiles Program compiles Program compiles, Program does not compile
Ability to and contains no and is free from but contains errors or (in a dynamic
understand and evidence of major syntactic that signal language) contains
follow the rules misunderstanding misunderstandings, misunderstanding typographical
of the or misinterpreting but may contain of syntax errors leading to
programming the syntax of the non- standard usage undefined names.
language. language. or superfluous
elements.
2. Logic Program logic is Program logic is Program logic is Program contains some
Ability to correct, with no mostly correct, on the right track conditions that specify the
Specify known boundary but may contain with no infinite opposite of what is
conditions, errors, and no an loops, but shows required (less than vs.
control flow, redundant or occasional boundary no recognition greater than), confuse
and data contradictory error or redundant of boundary Boolean AND/OR
structures that are conditions. or contradictory conditions operators, or lead to
appropriate for condition. (such as < vs. infinite loops.
the problem <=)
domain.
3. Correctness Program produces Program produces Program Program does not produce
Ability to code correct answers correct answers or approaches correct answers or
formulae and or appropriate appropriate results correct answers appropriate results for
algorithms that results for all for most inputs. or appropriate most inputs.
reliably produce inputs tested. results for most
inputs, but can
correct answers or
contain
appropriate results.
miscalculations in
some cases.
4.Completeness Program shows Program shows Program shows Program shows little
Ability to apply evidence of evidence of case some evidence of recognition of how different
rigorous case excellent case analysis that is case analysis, but cases must be handled
analysis to the analysis, and all mostly complete, may be missing differently
problem domain. possible cases are but may have missed significant cases
handled minor or unusual or mistaken in
appropriately. cases. how to handle
some cases.
7
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Total Marks:
Signature of faculty: Checked on:
8
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Lab Exercise -2
Objective: To understand the basic concepts of Operators, Input/output function and
String formatting options in Python.
Theory
Python Operators: Operators are used to perform operations on variables and values.
Arithmetic operators are used with numeric values to perform common mathematical
operations:
Operator Name
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor division
9
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
1
0
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
The format() method formats the specified value(s) and insert them inside the
string's placeholder.
The placeholder is defined using curly brackets: {}. Read more about the placeholders in
the Placeholder section below.
Syntax
string.format(value1, value2...)
The Placeholders
The placeholders can be identified using named indexes {price}, numbered indexes {0}, or
even empty placeholders {}.
10
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Applications Given: -
1)Program to Compute simple interest and Compound interest for a given principal amount,
rate of Interest and duration in years.
Note:
Simple interest is calculated with the following formula: S.I. = (P × R × T)/100,
Compound interest is calculated using the formula: C.I.= P × [(1 +R/100) T ] – 1
where P = Principal, R = Rate of Interest in % per annum, and T = Time, usually calculated as
the number of years.
Write Algorithm: -
11
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
2). Write a Python program that prompts the user to enter the quantity of items (an integer) and the
price per item (a floating-point number). Format the output string to display the total cost with
appropriate formatting. Ensure that the price is displayed with two decimal places and the quantity is
displayed as an integer. For example, if the user enters the quantity as 3 and the price as 9.99, the
output should be: "Total cost: 3 items at ₹9.99 each = ₹29.97".
Write Algorithm: -
12
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
3) Program that accepts angle value in degrees, length X and Y and evaluate the
equation Z=(X*cos(A)) +(Y*sin(A))
Note: use math library
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
13
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Outcomes: -
1. Each student will be able to demonstrate the knowledge of python operators and
format types by writing python programs for the given problems. [ PO-1,2,3,5,9,10,12]
Category Advanced Proficient Approaching Beginning Marks
( 5 Marks) ( 3 Marks) Proficiency ( 1 Marks)
( 2 Marks)
1. Syntax Program compiles Program compiles Program compiles, Program does not compile
Ability to and contains no and is free from but contains errors or (in a dynamic
understand and evidence of major syntactic that signal language) contains
follow the rules misunderstanding misunderstandings, misunderstanding typographical
of the or misinterpreting but may contain of syntax errors leading to
programming the syntax of the non- standard usage undefined names.
language. language. or superfluous
elements.
2. Logic Program logic is Program logic is Program logic is Program contains some
Ability to correct, with no mostly correct, on the right track conditions that specify the
Specify known boundary but may contain with no infinite opposite of what is
conditions, errors, and no an loops, but shows required (less than vs.
control flow, redundant or occasional boundary no recognition greater than), confuse
and data contradictory error or redundant of boundary Boolean AND/OR
structures that are conditions. or contradictory conditions operators, or lead to
appropriate for condition. (such as < vs. infinite loops.
the problem <=)
domain.
3. Correctness Program produces Program produces Program Program does not produce
Ability to code correct answers correct answers or approaches correct answers or
formulae and or appropriate appropriate results correct answers appropriate results for
algorithms that results for all for most inputs. or appropriate most inputs.
reliably produce inputs tested. results for most
inputs, but can
correct answers or
contain
appropriate results.
miscalculations in
some cases.
4.Completeness Program shows Program shows Program shows Program shows little
Ability to apply evidence of evidence of case some evidence of recognition of how different
rigorous case excellent case analysis that is case analysis, but cases must be handled
analysis to the analysis, and all mostly complete, may be missing differently
problem domain. possible cases are but may have missed significant cases
handled minor or unusual or mistaken in
appropriately. cases. how to handle
some cases.
14
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Total Marks:
Signature of faculty: Checked on:
15
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Theory
Introduction:
Programs are written for the solution to the real-world problems. A language should have the
ability to control the flow of execution so that at different intervals different statements can
be executed. Structured programming is a paradigm aims at controlling the flow of execution
of statements in a program by using control structures.
A language which supports the control structures is called as structured programming
language
1. Sequence
2. Selection
3. Iteration Or Looping
4. Branching Or Jumping Statements
1. Sequence
Sequence is the default control structure; instructions are executed one after
another. Statement 1
Statement 2
Statement 3
……..
……..
……..
16
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
2. Selection
A selection statement causes the program control to be transferred to a specific flow based
upon whether a certain condition is true or not.
17
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Each condition is a Boolean expression, and each body contains one or more
commands that are to be executed conditionally.
If the first condition succeeds, the first body will be executed; no other
conditions or bodies are evaluated in that case.
If the first condition fails, then the process continues in similar manner with
the evaluation of the second condition. The execution of this overall construct
will cause precisely one of the bodies to be executed.
There may be any number of elif clauses (including zero), and
The final else clause is optional.
18
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
19
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
3. Iteration or Looping
while loop
A while loop allows general repetition based upon the repeated testing of a Boolean
condition
while condition:
body
Where, loop body contain the single statement or set of statements (compound
statement) or an empty statement.
The loop iterates while the expression evaluates to true, when expression becomes
false the loop terminates.
21
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
22
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
23
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
for LOOP
Python’s for-loop syntax is a more convenient alternative to a while loop when
iterating through a series of elements. The for-loop syntax can be used on any type of
iterable structure, such as a list, tuple str, set, dict, or file
24
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
25
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
27
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
1. break STATEMENT
28
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Break can be used to unconditionally jump out of the loop. It terminates the execution
of the loop. Break can be used in while loop and for loop. Break is mostly required,
when because of some external condition, we need to exit from a loop.
2. continue STATEMENT
The continue statement in Python returns the control to the beginning of the while
loop. The continue statement rejects all the remaining statements in the current
iteration of the loop and moves the control back to the top of the loop. The continue
statement can be used in both while and for loops.
29
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
pass STATEMENT
The pass statement in Python is used when a statement is required syntactically but
you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes.
The pass is also useful in places where your code will eventually go, but has
not been written yet (e.g., in stubs for example):
21
0
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
30
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0
The solution to the quadratic equation is given by two roots x1 and x2 and is computed with
the following formula.
=( )
−𝑏±√𝑏2−4𝑎𝑐
x
2𝑎
(1,2)
The content inside the square root is known as the discriminant and specified by Δ
Δ=b2-4ac
If Δ>0 the roots are real and distinct and is given as below:
(−𝑏+√𝛥)
2𝑎
x1 =
(−𝑏−√𝛥)
2𝑎
x2 =
If Δ<0 the roots are imaginary and are represented as complex roots as given below:
(−𝑏+𝑖√𝛥)
2𝑎
x1 =
(−𝑏−𝑖√−𝛥)
2𝑎
x2 =
If Δ==0 then the roots are equal and are a given below:
−𝑏
x1= x2=
2𝑎
31
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
32
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
3B) Write a Python program to compute the Factorial for a given number.
Note:
33
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Write Algorithm: -
34
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
3C) Write a python program to find the average of first n natural numbers.
Note:
Natural numbers are a part of the number system, including all the positive numbers from 1 to
infinity. Natural numbers are also known as counting numbers because they do not include
zero or negative numbers. They are a part of real numbers including only the positive
integers, but not zero, fractions, decimals, and negative numbers.
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
35
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
4a) Write a Python program to convert a lowercase string to uppercase and vice versa and swap
the case of the string which is a mix of lower and upper cases
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
36
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
4b) Write a Python function program to find the sum of even numbers or odd numbers up to
the number entered by the user.
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
37
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Outcomes: -
Each student will be able to demonstrate the knowledge of conditional statements and
looping by writing python programs for the given problems. [ PO-1,2,3,5,9,10,12]
Category Advanced Proficient Approaching Beginning Marks
( 5 Marks) ( 3 Marks) Proficiency ( 1 Marks)
( 2 Marks)
1. Syntax Program compiles Program compiles Program compiles, Program does not compile
Ability to and contains no and is free from but contains errors or (in a dynamic
understand and evidence of major syntactic that signal language) contains
follow the rules misunderstanding misunderstandings, misunderstanding typographical
of the or misinterpreting but may contain of syntax errors leading to
programming the syntax of the non- standard usage undefined names.
language. language. or superfluous
elements.
2. Logic Program logic is Program logic is Program logic is Program contains some
Ability to correct, with no mostly correct, on the right track conditions that specify the
Specify known boundary but may contain with no infinite opposite of what is
conditions, errors, and no an loops, but shows required (less than vs.
control flow, redundant or occasional boundary no recognition greater than), confuse
and data contradictory error or redundant of boundary Boolean AND/OR
structures that are conditions. or contradictory conditions operators, or lead to
appropriate for condition. (such as < vs. infinite loops.
the problem <=)
domain.
3. Correctness Program produces Program produces Program Program does not produce
Ability to code correct answers correct answers or approaches correct answers or
formulae and or appropriate appropriate results correct answers appropriate results for
algorithms that results for all for most inputs. or appropriate most inputs.
reliably produce inputs tested. results for most
inputs, but can
correct answers or
contain
appropriate results.
miscalculations in
some cases.
4.Completeness Program shows Program shows Program shows Program shows little
Ability to apply evidence of evidence of case some evidence of recognition of how different
rigorous case excellent case analysis that is case analysis, but cases must be handled
analysis to the analysis, and all mostly complete, may be missing differently
problem domain. possible cases are but may have missed significant cases
handled minor or unusual or mistaken in
appropriately. cases. how to handle
some cases.
38
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Total Marks:
Signature of faculty: Checked on:
39
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
5a) Write a python program to find all the positive and negative numbers in a given list.
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
31
0
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
5b) Write a Python program that creates a list of tuples called "students" to store the details of multiple students. Each
tuple should contain the student's name, age, and average score. Prompt the user to enter the details for three students
and store them in the "students" list. Print the list of tuples
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
31
1
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
5c) Create a dictionary to store the country name and capitals of the country. Perform the following operations on
dictionary
i) Adding a country to the dictionary
ii) Deleting a country from the dictionary
iii) Find the length of the Dictionary
Write Algorithm: -
Observation/ Output
Attach printout of program and output:
31
2
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Outcomes: -
Each student will be able to demonstrate the knowledge of conditional statements and
looping by writing python programs for the given problems. [ PO-1,2,3,5,9,10,12]
Category Advance Proficient Approaching Beginning Marks
d(5 ( 3 Marks) Proficiency ( 1 Marks)
Marks) ( 2 Marks)
1. Syntax Program Program compiles Program compiles, Program does not compile
Ability to compiles and and is free from but contains errors or (in a dynamic
understand and contains no major syntactic that signal language) contains
follow the rules evidence of misunderstandings, misunderstanding typographical
of the misunderstandin but may contain of syntax errors leading to
programming g or non- standard usage undefined names.
language. misinterpreting or superfluous
the syntax of the elements.
language.
2. Logic Program logic is Program logic is Program logic is Program contains some
Ability to correct, with no mostly correct, on the right track conditions that specify the
Specify known but may contain with no infinite opposite of what is
conditions, boundary an loops, but shows required (less than vs.
control flow, errors, and no occasional boundary no recognition greater than), confuse
and data redundant or error or redundant of boundary Boolean AND/OR
structures that are contradictory or contradictory conditions operators, or lead to
appropriate for conditions. condition. (such as < vs. infinite loops.
the problem <=)
domain.
3. Correctness Program Program produces Program Program does not produce
Ability to code produces correct correct answers or approaches correct answers or
formulae and answers or appropriate results correct answers appropriate results for
algorithms that appropriate for most inputs. or appropriate most inputs.
reliably produce results for all results for most
inputs, but can
correct answers or inputs tested.
contain
appropriate results.
miscalculations in
some cases.
4.Completeness Program Program shows Program shows Program shows little
Ability to apply shows evidence of case some evidence of recognition of how different
rigorous case evidence of analysis that is case analysis, but cases must be handled
analysis to the excellent case mostly complete, may be missing differently
problem domain. analysis, and all but may have missed significant cases
possible cases minor or unusual or mistaken in
are handled cases. how to handle
appropriately. some cases.
5.Clarity Program Program contains Program contains Program contains no
Ability to format and contains some documentation some documentation, or grossly
document code for appropriate on major functions, documentation (at misleading indentation.
human documentation variables, or non- least the student’s
consumption. for all major trivial algorithms. name and
functions, Indentation and program’s
variables, or other formatting is purpose), but has
non- trivial appropriate. occasionally
algorithms. misleading
Formatting, indentation.
indentation, and
other white
space
aids readability.
31
3
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
Total Marks:
Signature of faculty: Checked on:
Index
Experiment No. Date Title of the Experiment Marks Signature
1 Write an algorithm
and Draw Flowchart
to compute Simple
Interest
2 Write an algorithm
and Draw Flowchart
to find the Largest
among Two Integer
Numbers
3 Program to Compute
simple interest and
Compound interest for
a given principal
amount, rate of
Interest and duration
in years.
4 Write a Python
program that prompts
the user to enter the
quantity of items and
the price per item,
formatted output.
5 Write a Python
program to evaluate
Z=(X*cos(A)) +
(Y*sin(A)) for given
values.
6 Write a Python
program to find roots
of a quadratic
equation.
7 Write a Python
program to compute
the Factorial for a
given number.
8 Write a python
program to find the
average of first n
natural numbers.
9 Write a Python
31
4
KLS, GIT-PYTHON PROGRAMMING LAB MANUAL (22PLC25B)
program to convert a
lowercase string to
uppercase and vice
versa.
10 Write a Python
function program to
find the sum of even
or odd numbers up to
user input.
31
5