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)
------------------------------------------------
In questa pagina del sito puoi guardare il video online Python Fundamentals - User Input in Python della durata di ore minuti seconda in buona qualità , che l'utente ha caricato G C Reddy Programming 13 agosto 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 144 volte e gli è piaciuto 4 spettatori. Buona visione!