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)
------------------------------------------------
On this page of the site you can watch the video online Python Fundamentals - User Input in Python with a duration of hours minute second in good quality, which was uploaded by the user G C Reddy Programming 13 August 2019, share the link with friends and acquaintances, this video has already been watched 144 times on youtube and it was liked by 4 viewers. Enjoy your viewing!