1. Input
When we withdraw money from the ATM machine in the bank, we definitely need to enter a password, right? So how do we get the program to know what we just typed? We should know, if you want to complete the ATM machine to take money this matter, you need to first input a data from the keyboard, and then use a variable to save, is not very good understanding.
1.1 Raw_input
In Python, the way to get input data from a keyboard is to use the Raw_input function (as for what is a function, as we'll explain in later chapters), so how does this raw_input work?
See the following example:
Password = raw_input("Please enter password:") Print ' The password you just entered is: ', password |
Note:
Raw_input (), in parentheses, is a simple hint that prompts the message to give the user before getting the data Raw_input () After data is obtained from the keyboard, it is stored in the variable to the right of the equal sign Raw_input () treats any value entered by the user as a string |
Note: If you compile with Python3, you will get an error:
Nameerror: name ' raw_input ' is not defined |
The reason is that the raw_input has been discarded in the python3.x and can be replaced with input. 1.2 Input
The input () function is similar to Raw_input () and must be an expression accepted in Python2. Any input can be received in the Python3.
A = 10 b = 20 #password = raw_input ("Please enter password: \ n") Password = input("Please enter password: \ n") Print ("The password you have entered is:%s"% password) print (type(password)) |
The input a+b,python2 results are:
Python3 results are:
1. Operator
Python supports several of the following operators: 1.1 arithmetic operators
| Operator Description instance + plus Two objects Add a + b output result - minus get negative numbers or one number minus another number a - b output - ten * multiply Two numbers or return a repeated number of times the string a * b output / except x divided by y b / a output 2 strong>// Take Division integer part of the return 9 // 2 output 4 , 9.0 // 2.0 Output 4.0 % remainder returns division B % a output 0 * * Power return x's y power a ** b is 10 of 2 times, output results |
1.2 Assignment Operators
Operator Description Instance = assignment operator puts the result to the right of = number to the left of the variable num=1+2*3 results num has a value of 7 |
1.3 Composite Operators
| Operator Description Instance = = addition assignment operator C + A is equivalent to c = C+ A - = subtraction assignment operator C-= A is equivalent to c = C- A *= multiplication assignment operator C*= A is equivalent to c = C* a /= Division assignment operator C/= A is equivalent to c = C/ A %= modulo assignment operator C%= A is equivalent to c = C% a **= Power assignment operator C**= A is equivalent to c = C* * A //= The Division assignment operator C //= A is equivalent to c= C / A |
1.9 Data type conversions
| Function description int(x [,base]) converts x to an integer Long(x [,base]) converts x to a long integer Float(x ) converts x to a floating-point number Complex(real [,imag ]) creates a complex number Str(x ) converts an object x to a string Repr(x ) converts an object x to an expression string Eval(str ) is used to compute a valid Python expression in a string and returns an object Tuple(s ) converts the sequence s to a tuple List(s ) converts sequence s to a list Chr(x ) converts an integer to a character UNICHR(x ) converts an integer to a Unicode character Ord(x ) converts a character to its integer value Hex(x ) converts an integer to a hexadecimal string Oct(x ) converts an integer to a octal string |
| V1 = "123" V2 = int(v1) print ("v2:%s\n"% (type(v2) ) V3 = 88 V4 = str(v3) print ("v4:%s"% (type(v4) ) V5 = repr(v3) print ("v5 =%s type =%s\n"% (v5,type(v5) ) V6 = eval("10+20") print (V6) print ("v6 =%d\n"% (V6)) V7 = 23 V8 = chr(v7) print (V8) V8 = hex(v3) print ( V8 ) |