![]() |
|
[Tut] Python raw_input() vs input() - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python raw_input() vs input() (/thread-97214.html) |
[Tut] Python raw_input() vs input() - xSicKxBot - 09-13-2020 Python raw_input() vs input() Summary: The key differences between Code: raw_input()Code: input()
Overview Before looking at the differences between Code: raw_input()Code: input()A user-friendly code is one that is interactive. To make a code interactive instead of hard coding values, a developer/programmer must aim to allow the user to input their own values into the program. We use the Code: raw_input()Code: input()Example: The following program is an example to accept user input in Python: Code: name = input("Please enter your full name: ")
age = input("Please enter your age: ")
# In Python2.x use raw_input() instead print("Name: ", name)
print("Age: ", age)Output Code: Please enter your full name: FINXTER
Please enter your age: 25
Name: FINXTER
Age: 25In this article, we shall be discussing the key differences between the Code: input()Code: raw_input()Problem: What is difference between Code: raw_input()Code: input()Let us have an in-depth look at each difference one by one: Existential Difference raw_input() | input() Inbuilt function present only in Python 2.x and is not a part of Python 3.x | Inbuilt function present in both, Python 2.x and Python 3.x Functional Difference Based on Python Versions | Python 2.x | Python 3.x raw_input() | ◆ Code: raw_input()◆ Since it accepts the input as it is, it does not expect the input to be syntactically correct. | ◆ Code: raw_input()input() | ◆input() accepts the input from the user as a statement or expression and returns the output after evaluating the input. In other words, it accepts the user entry as raw_input(), performs an eval() on it, and then returns the result as output. ◆ It expects a syntactically correct input (statement/expression) from the user. | ◆ In Python 3.x, Code: raw_input()Code: raw_input()Thus Code: input()Examples Python 2.x ✎ input() function Code: a = raw_input("What is your name? ")
print "Name: %s" %a)
b = raw_input(" Enter a mathematical expression: ")
print Output": %d", %bOutput Code: What is your name? Finxter
Name: Finxter Enter a mathematical expression: 2+5
Output: 2+5✎ raw_input() function Code: a = input("Enter Your Full Name: ")
print "Name: %s " %a
b = input("Enter a Mathematical Expression: ")
print "Output: %d" %bOutput Code: Enter Your Full Name: 'Finxter Shubham'
Name: Finxter Shubham
Enter a Mathematical Expression: 5**2
Output: 25Python 3.x And Above ✎ input() function Code: a = input("What is your name? ")
print("Name: ", a)
b = input("Enter a mathematical expression: ")
print("Output: ", b)Output Code: What is your name? Finxter Shubham
Name: Finxter Shubham
Enter a mathematical expression: 3+5
Output: 3+5If you want to implement or leverage the functionality of Code: input()
Example Code: a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Addition: ", a+b)
x = eval(input("Enter a mathematical expression: "))
print("Result: ", x)Output: Code: Enter first number: 25
Enter second number: 75
Addition: 100
Enter a mathematical expression: 10**2
Result: 100But you must avoid the usage of Code: eval()I would strongly recommend you to read this article in connection with this topic. It will help you have a broader understanding of this concept. Also, if you are wondering about the version of python installed in your system, you may want to have a look atthis article. Conclusion In this article, we discussed the key differences between Code: input()Code: raw_input()Code: input()Code: raw_input()Please stay tuned and Subscribe for more interesting articles! Where to Go From Here? Enough theory, let’s get some practice! To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |