0% found this document useful (0 votes)
368 views23 pages

Python Lab File

This document contains 16 tasks that involve writing Python programs to demonstrate various concepts like data types, arithmetic operations, strings, dates, lists, tuples, dictionaries, conditionals, loops, functions, modules, file handling and more. For each task, example code is provided as an answer to achieve the given task.

Uploaded by

eshanpadhiar3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
368 views23 pages

Python Lab File

This document contains 16 tasks that involve writing Python programs to demonstrate various concepts like data types, arithmetic operations, strings, dates, lists, tuples, dictionaries, conditionals, loops, functions, modules, file handling and more. For each task, example code is provided as an answer to achieve the given task.

Uploaded by

eshanpadhiar3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

Task 1 : - Write a program to demonstrate

different number data types in Python.


Ans - In Python, there are several built-in number data types,
including integers, floats, and complex numbers. Here's a
program that demonstrates these different number data types:-

In this program:
1. We define an integer (int_num) and print it.
2. We define a float (float_num) and print it.
3. We define a complex number (complex_num) and print it.
We also perform basic arithmetic operations on integers:
• Addition
• Subtraction
• Multiplication
• Division
• Integer division (floor division)
• Remainder (modulo)
• Exponentiation
When you run this program, it will display each number data
type along with the results of the arithmetic operations.

Task 2:- Write a program to perform different


Arithmetic Operations on numbers in Python.
Ans - here's a Python program that performs various arithmetic
operations on numbers:

In this program:
1.We prompt the user to enter two numbers, which are then
converted to floating-point numbers for accurate division.
2.We perform various arithmetic operations, including
addition, subtraction, multiplication, division, integer division
(floor division), remainder (modulo), and exponentiation.
3.The results are displayed with meaningful labels.
When you run this program, you can input any two numbers,
and it will calculate and display the results of these arithmetic
operations.
Task 3:- Write a program to create, concatenate and
print a string and accessing sub-string from a given
string.
Here's a Python program that demonstrates creating,
concatenating, and accessing substrings from a given string:-

In this program:
1. We create two strings, string1 and string2.
2. We concatenate these strings using the + operator to create
concatenated_string.
3. We access a substring by specifying the indices (in this
case, from index 0 to 4) and store it in the substring variable.
4. We also demonstrate accessing a substring by using
negative indexing to get the last three characters and store them
in the last_three_chars variable.
When you run this program, it will display the concatenated
string, the substring, and the last three characters of the
concatenated string.

Task 4:- Write a python script to print the


current date in the following format “Sun May
29 02:26:23 IST 2017”.
Ans - You can use Python's datetime module to print the
current date in the desired format. Here's a Python script to
achieve that:
This script will give you the current date in the format you
specified, like "Sun May 29 02:26:23 IST 2017".
Task 5:- Write a program to create, append, and
remove lists in python.
Ans - Certainly! You can create, append, and remove
elements from lists in Python using the following code
examples:-
Creating a List:
You can create a list in Python using square brackets [].

Appending Elements:
You can append elements to a list using the append() method.

Removing Elements:
You can remove elements from a list using various methods
like pop(), remove(), or by slicing.
• Using pop() to remove an element by index:
Using remove() to remove an element by value:

Using slicing to remove elements by index range:

Here's a complete program that demonstrates these operations:

This program will create, append, and remove elements from a


list, and you can observe the final state of the list after these
operations.
Task 6:- Write a program to demonstrate
working with tuples in python.
Ans - Tuples in Python are similar to lists but are immutable,
meaning their elements cannot be changed after creation.
Here's a program that demonstrates various operations you can
perform with tuples:
This program demonstrates creating a tuple, accessing
elements, slicing, tuple unpacking, finding the length, counting
occurrences, finding the index of an element, concatenating
tuples, checking for the existence of an element, and iterating
through a tuple. Tuples are particularly useful when you want
to store a collection of values that should not be modified
during the program's execution.
Task 7:-Write a program to demonstrate
working with dictionaries in python.
Ans- Dictionaries in Python are used to store key-value pairs.
Here's a program that demonstrates various operations you can
perform with dictionaries:-

This program demonstrates creating a dictionary, accessing


values using keys, modifying values, adding key-value pairs,
checking if a key exists, removing key-value pairs, iterating
through keys, values, and key-value pairs, finding the length of
a dictionary, and creating a dictionary using a dictionary
comprehension. Dictionaries are versatile data structures for
organizing and retrieving data based on keys.
Task 8:- Write a python program to find largest of
three numbers.
Ans - You can write a Python program to find the largest of
three numbers using conditional statements. Here's an example
program:-

This program first takes three numbers as input from the user
and then uses conditional statements (if, elif, else) to compare
the numbers and determine which one is the largest. Finally, it
prints the largest number.
Task 9:- Write a Python program to convert
temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9]
Ans - You can write a Python program to convert temperatures
between Celsius and Fahrenheit using the provided formula.
Here's a program that allows you to enter a temperature in either
Celsius or Fahrenheit and converts it to the other scale:-

In this program, you can choose whether you want to convert


from Celsius to Fahrenheit ('C') or from Fahrenheit to Celsius
('F'). Depending on your choice, it takes the input temperature
in the specified scale and performs the conversion using the
provided formula.
Task 10: Write a Python program to construct the
following pattern, using a nested for loop
*
**
***
****
***
**
*
*
Ans - You can create the desired pattern using nested for loops
in Python. Here's a program to construct the pattern:-

This program will print the pattern with the specified number
of rows, producing the output you described:-
Task 11:- Write a Python script that prints prime
numbers less than 20.
Ans - You can write a Python script to print prime numbers less
than 20 by iterating through numbers and checking if each
number is prime. Here's a script to do that:

In this script, the is_prime function is used to check if a given


number is prime. It then iterates through numbers from 2 to 19
(inclusive) and prints the prime numbers. The output will be:

These are the prime numbers less than 20.


Task 12:- Write a python program to find factorial of
a number using Recursion.
Ans- You can find the factorial of a number using recursion in
Python. Here's a program to do that:

In this program, the factorial function is defined to calculate the


factorial of a number using recursion. If the number is 0, it
returns 1 (0! = 1). Otherwise, it recursively calculates the
factorial by multiplying the number with the factorial of (n - 1).

The program then takes input from the user, calculates the
factorial, and displays the result.
Task 13:- Write a program that accepts the lengths of
three sides of a triangle as inputs. The program
output should indicate whether or not the triangle is
a right triangle (Recall from the Pythagorean
Theorem that in a right triangle, the square of one
side equals the sum of the squares of the other two
sides).
Ans - You can write a Python program to determine if a triangle
is a right triangle by checking whether the Pythagorean theorem
holds. Here's a program that does just that:

In this program, the is_right_triangle function takes the three


side lengths as input, sorts them in ascending order, and then
checks if they satisfy the Pythagorean theorem. If the theorem
holds, it returns True, indicating that it's a right triangle;
otherwise, it returns False.
The program then takes input for the lengths of the three sides
and checks whether it's a right triangle or not.
Task 14:- Write a python program to define a module
to find Fibonacci Numbers and import the module to
another program.
Ans:- you can create a Python module to find Fibonacci
numbers and then import that module into another program.
Here's how you can do it:
Create a Fibonacci module (e.g., fibonacci_module.py) with a
function to generate Fibonacci numbers:-
fibonacci_module.py:-

This module defines a fibonacci function that generates the first


n Fibonacci numbers.
Create another program and import the fibonacci_module to
use the Fibonacci function:
main_program.py:-

In the main_program.py script, we import the


fibonacci_module and use the fibonacci function to generate
and display the first n Fibonacci numbers.
To run the main_program.py, you should have both
fibonacci_module.py and main_program.py in the same
directory. When you run main_program.py, it will ask for the
number of Fibonacci numbers to generate and then use the
fibonacci_module to calculate and display them.

Task 15:- Write a python program to define a module


and import a specific function in that module to
another program.
Ans- You can create a Python module with specific functions
and then import one of those functions into another program.
Here's how you can do it:-
Create a module (e.g., my_module.py) with multiple
functions:-
my_module.py:-

This module defines three functions: function1, function2,


and function3.
Create another program and import a specific function from the
module:-
main_program.py:-
In the main_program.py script, we import function1
specifically from my_module and then call it to execute that
function.
To run main_program.py, you should have both my_module.py
and main_program.py in the same directory. When you run
main_program.py, it will import and execute function1 from
my_module.
Task 16:- Write a script named copyfile.py. This
script should prompt the user for the names of two
text files. The contents of the first file should be input
and written to the second file.
Ans- You can create a Python script named copyfile.py that
prompts the user for the names of two text files and copies the
contents of the first file to the second file.Here's how you can
do it:

Make sure to place this script in the same directory as the text
files you want to copy.
Task 17:- Write a program that inputs a text file. The
program should print all of the unique words in the
file in alphabetical order.
Ans- You can create a Python program to read a text file,
extract and print all the unique words in alphabetical order.
Here's a program to do that:

In this program, you input the name of the text file, and it reads
the content, tokenizes it into words, converts them to
lowercase, removes punctuation and special characters, and
then finds the unique words. Finally, it sorts and prints the
unique words in alphabetical order.
Task 18:- Write a Python class to convert an integer
to a roman numeral.
Ans- You can create a Python class to convert an integer to a
Roman numeral using a method within the class. Here's a
simple example of how you can do it:-

This program defines a IntegerToRoman class with a method


int_to_roman, which converts an integer to a Roman numeral.
Task 19:- Write a Python class to implement pow(x,
n).
Ans - You can create a Python class to implement the pow(x,
n) function using a method within the class. Here's an example
of how you can do it:

In this program, the PowerCalculator class has a power


method that calculates x raised to the power of n. It handles
both positive and negative exponents and uses an iterative
approach to calculate the result. The user inputs the values of x
and n, and the program calculates and prints the result.
Task 20:- Write a Python class to reverse a string
word by word.
Ans- You can create a Python class to reverse a string word by
word using a method within the class. Here's an example of
how you can do it:

In this program, the StringReverser class defines a


reverse_words method that reverses a string word by word. It
splits the input string into words, reverses the list of words, and
then joins them back into a reversed string. The user inputs a
string, and the program reverses and prints the string word by
word.

You might also like