F.E. PPS Unit 2 Practical 1 solution

Practical 1: To demonstrate type conversion type casting Python program to demonstrate type conversion type casting and the use of comments in Python.

Problem Statement

Write a Python program to demonstrate type conversion, type casting, and the use of comments in Python.

  • Type conversion refers to the process of converting one data type to another.
  • Type casting is used to change the type of a variable explicitly in Python.

The program should:

  • Take an integer input from the user.
  • Convert it to a string.
  • Perform type casting back to integer and print the results.
  • Demonstrate the use of comments in Python code

Solution Code

				
					# Program to demonstrate type conversion and type casting
 
# Input: Take an integer input from the user
num = int(input("Enter an integer: "))
 
# Convert the integer to a string
num_str = str(num)
 
# Type casting: Convert string back to integer
num_int = int(num_str)
 
# Output results
print(f"Original Integer: {num}")
print(f"Converted to String: {num_str}")
print(f"Converted back to Integer: {num_int}")

				
			

Output:

  • The program will prompt the user to input an integer.
  • It will then display the following:
    1. The original integer.
    2. The integer converted to a string.
    3. The string converted back to an integer
				
					
Enter an integer: 10
Original Integer: 10
Converted to String: 10
Converted back to Integer: 10
Process finished with exit code 0
				
			

Explanation

Below is the explanation of F.E. PPS Unit2 Practical 1 solution where we have written Python program to demonstrate type conversion type casting Python program to demonstrate type conversion type casting and the use of comments in Python.

  1. Input of Integer:
    • The program starts by asking the user to input an integer using the input() function. The input is then converted to an integer type using int().
  2. Type Conversion (Integer to String):
    • The integer is then converted to a string using str(). This demonstrates type conversion where the value of the integer is now represented as a string.
  3. Type Casting (String back to Integer):
    • The string value is converted back into an integer using int(). This demonstrates type casting, where we explicitly change the data type from string back to integer.
  4. Displaying the Results:
    • The program prints all three values: the original integer, the string representation, and the integer obtained after casting back.

Key Concept Learned

  • Type Conversion and Type Casting:
    • The practical helps in understanding how type conversion works, with explicit examples of converting an integer to a string and then back to an integer.
  • Comments in Python:
    • The program also demonstrates how to use comments effectively in Python. Comments are added using the # symbol, and they are not executed by the program. They help explain what the code is doingt
Tech Amplifier Final Logo