0% found this document useful (0 votes)
54 views

Python Lab Practical

The document outlines various Python exercises covering basics like running code interactively versus through scripts, raising and correcting indentation errors, and performing calculations. Later exercises involve control flow with conditions, loops, and functions as well as data structures including lists, strings, and dictionaries to solve problems like sorting, searching, and counting characters. The goal is to practice common Python programming concepts and techniques through examples and prompts for interactive coding exercises.

Uploaded by

ranaashutosh698
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Python Lab Practical

The document outlines various Python exercises covering basics like running code interactively versus through scripts, raising and correcting indentation errors, and performing calculations. Later exercises involve control flow with conditions, loops, and functions as well as data structures including lists, strings, and dictionaries to solve problems like sorting, searching, and counting characters. The goal is to practice common Python programming concepts and techniques through examples and prompts for interactive coding exercises.

Uploaded by

ranaashutosh698
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Exercise 1 – Basics

1.Running instructions in Interactive interpreter and a Python Script

In Python, there are two options/methods for running code:

 Interactive mode

 Script mode

Interactive Mode
Example :

Script Mode
You Write your code in a text file then save it with a .py extension which stand for
“Python”.

Example :

Output : -
2.Write a program to purposefully raise Indentation Error and Correct it
Example : Python Indentation Error

Example : Now Fixing the indentation Error in the example of python


programs

Output : -
Exercise 2 - Operations

1.Write a program to compute distance between two points taking input


from the user (Pythagorean Theorem)

We will use the distance formula derived from Pythagorean theorem. The
formula for distance between two point (x1, y1) and (x2, y2) is
Distance =

Example : -

Output : -
1.Write a program add.py that takes 2 numbers as command line
arguments and prints its sum.

Save a program add.py

Output : -
Exercise - 3 Control Flow

1. Write a Program for checking whether the given number is a


even number or not.

Output : -
# Number is 4 and it is even number

# Number is 5 and it is not even number


2.Using a for loop, write a program that prints out the decimal equivalents of
1/2, 1/3, 1/4, . . . ,1/10

Output : -
1/2=0.5
1/3= 0.3333333333333

…..

1/10=

0.1
2. Write a program using a for loop that loops over a sequence.

Output : -
3. Write a program using a while loop that asks the user for a
number, and prints a countdown from that number to zero.

Output : -
Exercise 4 - Control Flow –
Continued
1. Find the sum of all the primes below two million. Adding the previous
two terms, each new term in the Fibonacci sequence is generated. By
starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, ...

# The sum of all the primes below two million

Output : -
# Fibonacci sequence

Output : -
2. By considering the terms in the Fibonacci sequence whose values
do not exceed four million, find the sum of the even-valued terms.

Output :-
3.Linear search and Binary search

# Linear Search

Output : -
#Binary Search

Output : -
4. Selection sort, Insertion sort

# Selection sort

Output : -
# Insertion Sort

Output : -
Exercise - 5 – DS
1. Writea program to count the numbers of characters in the string
and store them in a dictionary data structure

Output : -
2:Write a program to use split and join methods in the string
and trace a birthday with a dictionary data structure.

output:

You might also like