


Detailed explanation of the difference between raw_input() and input()
raw_input and input are both built-in functions of python, which interact with the user by reading input from the console. But their functions are not the same. Here are two examples to illustrate the difference in usage.
Example 1
Python 2.7.5 (default, Nov 18 2015, 16:26:36) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> raw_input_A = raw_input("raw_input: ") raw_input: PythonTab.com >>> print raw_input_A PythonTab.com >>> input_A = input("Input: ") Input: PythonTab.com Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'PythonTab' is not defined >>> >>> input_A = input("Input: ") Input: "PythonTab.com" >>> print input_A PythonTab.com >>></module></string></module></stdin>
Example 2
Python 2.7.5 (default, Nov 18 2015, 16:26:36) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> raw_input_B = raw_input("raw_input: ") raw_input: 2015 >>> type(raw_input_B) <type> >>> input_B = input("input: ") input: 2015 >>> type(input_B) <type> >>></type></type>
Example 1 You can see: Both functions can receive String, but raw_input() Read input directly from the console (it can accept any type of input). As for input(), it hopes to be able to read a legal python expression, that is, when you enter a character string, you must use quotation marks to surround it, otherwise it will trigger a SyntaxError.
Example 2 You can see: raw_input() treats all inputs as strings and returns the string type. And input() has its own characteristics when dealing with pure numeric input. It returns the type of the number entered (int, float); at the same time, as shown in Example 1, input() can accept legal python expressions. Formula, for example: input(1 + 3) will return 4 of type int.
Check the python manual and learn:
input([prompt])
Equivalent to eval(raw_input(prompt))
input() is essentially implemented using raw_input(). It just calls the eval() function after calling raw_input(). Therefore, you can even use an expression as a parameter of input(), and it will calculate the expression. The value of the formula and returns it.
But there is a sentence in Built-in Functions that says: Consider using the raw_input() function for general input from users.
Unless there is something special about input() Required, otherwise we generally recommend using raw_input() to interact with users.
The above is the detailed content of Detailed explanation of the difference between raw_input() and input(). For more information, please follow other related articles on the PHP Chinese website!

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

ThefastestmethodforlistconcatenationinPythondependsonlistsize:1)Forsmalllists,the operatorisefficient.2)Forlargerlists,list.extend()orlistcomprehensionisfaster,withextend()beingmorememory-efficientbymodifyinglistsin-place.

ToinsertelementsintoaPythonlist,useappend()toaddtotheend,insert()foraspecificposition,andextend()formultipleelements.1)Useappend()foraddingsingleitemstotheend.2)Useinsert()toaddataspecificindex,thoughit'sslowerforlargelists.3)Useextend()toaddmultiple

Pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)Theyarestoredincontiguousmemoryblocks,whichmayrequirereallocationwhenappendingitems,impactingperformance.2)Linkedlistswouldofferefficientinsertions/deletionsbutslowerindexedaccess,leadingPytho

Pythonoffersfourmainmethodstoremoveelementsfromalist:1)remove(value)removesthefirstoccurrenceofavalue,2)pop(index)removesandreturnsanelementataspecifiedindex,3)delstatementremoveselementsbyindexorslice,and4)clear()removesallitemsfromthelist.Eachmetho

Toresolvea"Permissiondenied"errorwhenrunningascript,followthesesteps:1)Checkandadjustthescript'spermissionsusingchmod xmyscript.shtomakeitexecutable.2)Ensurethescriptislocatedinadirectorywhereyouhavewritepermissions,suchasyourhomedirectory.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
