Python Fundamentals - User Input in Python
Python Programming Language Fundamentals, Python Data Types, Python Inbuilt Functions, Input & Output Operations in Python Language. Read User Input using input function, Convert String to Integer, String to Float and Evaluate User Input using eval function.
Python provides us with two inbuilt functions to read the input from the keyboard.
raw_input ( prompt )
input ( prompt )
raw_input ( ) : This function works in older version (Python 2.x).
input ( ) : This function works in newer version (Python 3.x).
This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list.
Examples:
------------------------------------------------
1. Input
val= input("Enter a value: ")
print(val)
num1= input("Enter a Number: ")
num2= input("Enter a Number: ")
num3=num1+num2
print (num3) # Concatenate two values
------------------------------------------------
Input - Integer
num1= input("Enter a Number: ")
x=int(num1)
num2= input("Enter a Number: ")
y=int(num2)
num3=x+y
print (num3)# Add two values
num1= int (input("Enter a Number: "))
num2= int (input("Enter a Number: "))
num3=num1+num2
print (num3)# Add two values
------------------------------------------------
Input - Number with Decimal Places
num1= float (input("Enter a Number: "))
num2= float (input("Enter a Number: "))
num3=num1+num2
print (num3)# Add two values
------------------------------------------------
Input - Character
val= input("Enter a Character: " )[0]
print (val)
Evaluate the Input
val= eval (input("Enter an Expression: " ))
print (val)
------------------------------------------------
En esta página del sitio puede ver el video en línea Python Fundamentals - User Input in Python de Duración hora minuto segunda en buena calidad , que subió el usuario G C Reddy Programming 13 agosto 2019, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 144 veces y le gustó 4 a los espectadores. Disfruta viendo!