🐍
Input In Python
Input in Python
In Python, you can use the input() function to get user input from the keyboard.
Basic Usage
The input() function reads a line from the input and returns it as a string.
Syntax
input(prompt)
prompt : A string, representing a message to display to the user (optional).
Example
name = input("Enter your name: ")
print("Hello, " + name + "!")
Converting Input
Since input() returns a string, you might need to convert it to other types, such as
integers or floats.
Example
age = input("Enter your age: ")
age = int(age) # Convert the string to an integer
print("You are " + str(age) + " years old.")
Example with Multiple Inputs
Input In Python 1
You can also use input() to take multiple inputs.
Example
name = input("Enter your name: ")
age = input("Enter your age: ")
age = int(age)
print(f"Hello, {name}. You are {age} years old.")
Example with Type Conversion
Directly converting input to the desired type.
Example
age = int(input("Enter your age: "))
print(f"You are {age} years old.")
Handling Different Data Types
Make sure to convert the input to the correct type when necessary.
Example
height = float(input("Enter your height in meters: "))
print(f"Your height is {height} meters.")
Using the input() function, you can interact with users and get the necessary data
to use in your programs.
Practice Question
1. Sum and Difference of x and y
2. Area and Perimeter 5
3. Fahrenheit and Celsius
Input In Python 2