Understanding Type Casting and Input/Output in Python
1. Type Casting in Python
Type casting means converting one data type into another data type. Python provides built-in functions to perform type conversion easily.
Common Type Casting Functions
int()– Converts value to integerfloat()– Converts value to floatstr()– Converts value to stringbool()– Converts value to boolean
Example 1: String to Integer
age = "25"
converted_age = int(age)
print(converted_age)
print(type(converted_age))
Example 2: Integer to Float
number = 10
float_number = float(number)
print(float_number)
print(type(float_number))
2. Input and Output in Python
Input and Output operations allow interaction between the user and the program.
Output in Python
The print() function is used to display output.
print("Hello, World!")
name = "Chandan"
print("My name is", name)
Input in Python
The input() function is used to take user input.
By default, input is always stored as a string.
name = input("Enter your name: ")
print("Welcome", name)
Using Type Casting with Input
age = input("Enter your age: ")
age = int(age)
print("Next year you will be", age + 1)
Conclusion
Type casting and Input/Output are fundamental concepts in Python. They are widely used in real-world applications, automation scripts, and Selenium testing frameworks.
No comments:
Post a Comment