Python Lab File
Python Lab File
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.
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.
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:
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:-
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:
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:
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:-