The input and output Python, Python input determination EOF

Input using input () method. elements are input to input form str saved. Using the output print () method.
Python is not representative of the EOF character, and therefore an error EOFerror direct input is detected EOF. Therefore, we use the following wording to stop the loop is detected after the input to the input EOF:

True the while:
the try:
S = int (INPUT ())
Print (S)
the except:
Print ( 'INPUT Complete')
BREAK
. 1
2
. 3
. 4
. 5
. 6
. 7
if cmd.exe running this code, the input Ctrl + Z i.e. input can be stopped. If it is built to run in Pycharm run this code box, type Ctrl + D to stop input.
If the file is read in Python, the read end of the file is Python returns an empty string, so the python could judge:

= STR ''
with Open ( 'the Readme.txt', 'R & lt', encoding = 'UTF-. 8') AS FP:
the while True:
S = fp.read (10)
IF S == '':
BREAK
STR + = S
Print (STR)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
the Python string type, null type, Unicode encoding, UTF-8 encoded string format
strings are single quotes' or double quotes "any text enclosed If the string contains both internal 'and included ", you can use the escape character \ is identified. \ n newline, \ T represents a tab, \ represents \. R & lt Python also allows '' means '' string inside the default is not escape.
Null is a special value in Python, expressed None. None not be interpreted as 0, because 0 is meaningful, and None is a special null value.
Unicode encoding all languages are unified into a set encodings, so you do not have a garbage problem. The most commonly used Unicode character is represented by two bytes of a (very uncommon characters are encoded into four bytes). For example the ASCII-encoded A represents the Unicode encoded, only the leading zero can, therefore, Unicode code A is 0,000,000,001,000,001.
UTF-8 encoding corresponding to the encoded unicode optimized, it is a 6 byte Unicode characters into different numbers according to the size of the coding, common letters are encoded into a byte characters typically 3 bytes, only a very uncommon character will be encoded into 4-6 bytes. ASCII encoding can actually be seen as part of UTF-8 encoding, so only supports ASCII encoding a large number of legacy software may continue to operate in UTF-8 encoding.
In computer memory, uniform use Unicode encoding, when the need to save time or the hard disk needs to be transmitted, is converted to UTF-8 encoding.
Note:
Python3 type string str, in memory, in Unicode encoding. If you want to transfer over the network or saved to disk, you need to become str to bytes bytes.
Python provides the ord () method to obtain a character Unicode encoding, and CHR () method to convert to the corresponding Unicode character encoding. Such as:

S1 = "A"
S2 = ". 1"
S3 = "you"
Print (the ord (S1))
Print (the ord (S2))
Print (the ord (S3))
Print (CHR (66))
Print (CHR (20500))
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
the Python representation of the type of data bytes with a single quotation mark b prefix or double quotes:

b'ABC = X '
. 1
the Python provides encode () method may be coded as specified type str bytes, decode () method changes the bytes str. Such as:

= S1 "the ABC"
S2 = s1.encode ( 'ASCII')
S3 = "hello"
Print (s1.encode ( 'ASCII'))
Print (s3.encode ( 'UTF-. 8'))
Print (s2.decode ( 'ASCII'))
. 1
2
. 3
. 4
. 5
. 6
STR containing Chinese not encoded in ASCII, since the scope of Chinese encoding exceeds the range of the ASCII code, Python error. In order to avoid the garbage problem, we should stick with UTF-8 encoding for str and bytes for conversion.
When the Python interpreter to read the source code to make it according to UTF-8 code reading, we usually write two lines of command at the beginning of the file:

! # / usr / bin / env python3
# - * - Coding: UTF-8 - * -
1
2
first-line comments is to tell the Linux / OS X system, which is a Python executable, Windows will ignore this comment ; second line comments is to tell the Python interpreter, according to the UTF-8 encoding to read the source code, or else, you write in the source code of Chinese output may be garbled. In addition, we need to ensure that the use of the IDE is using UTF-8 encoding.
Output format string:
% operator is used to format string. % s indicates the replacement string,% d represents an integer Alternatively, a few%? placeholder, several variables or a value just behind, to the corresponding sequence. Integer and floating point format can also specify whether the complement integer and fractional bits 0. We can also format the output format usage. Such as:

Print ( "% 2D% 02d"% (. 3,. 1))
Print (. "%. 2F"% 3.1415926)
Print (. "{} {}" the format (. 3, 1.21))
Print ( "{: 02d} {: . .2f} "the format (. 3, 3.1415926))
. 1
2
. 3
. 4
the Python condition judgment cycle
in the form of the statement is determined:

determining if the condition:
execute statement ......
elif Analyzing conditions:
execute statement ......
else:
execute statement ......
. 1
2
. 3
. 4
. 5
. 6
Note elif, optionally adding the else sentence is determined .
Python is provided for and while loops (not do while loop in Python ...), the following form:

for x in sequence:
statements(s)

while judgment conditions:
execute statement ......
. 1
2
. 3
. 4
. 5
BREAK statement will interrupt the cycle of the current layer, continue statement would direct the end of this cycle, the next cycle begins.

Python immutable and variable objects
immutable object refers to the value of the object points in the memory can not be changed. When changing a variable time, since the value to which it refers can not be changed, the new value corresponding to the new address in a variable point to the new address.
Variable object refers to the value of the object pointed to memory can be changed. After the variables change, in fact, is the value to which it refers directly change occurs, replication behavior did not happen, nor open up a new address, popular point that place change.
In Python, numeric types (int and a float), strings, tuples are immutable. The lists, dictionaries, collections are variable types.

Python in parameter passing
in Python variable names can all be understood in memory of an object "reference", like in C ++ pointer.
Python is the type belonging to the object, rather than a variable. And the object and the variable object points immutable objects. Such as string, a tuple values, and types are immutable objects, and a list of dictionary objects and collections are variable.
When a variable name (that is, a reference to an object) passed to the function, the function automatically copy reference. When this variable points is immutable, we modify the content of the variable when, in fact, is in memory opened up a new address, new content written here, then the variable name points to open up a new address; and when this variable point is variable object, similar to C ++ function names from the incoming pointers as we modify the content of the variable changes directly on the memory address of the variable is pointing.
For example:

a = 1


def fun(b):
print(id(b))
b = 2
print(id(b), id(2))


print(id(a), id(1))
fun(a)
print(a)

c = []


def fun(d):
print(id(d))
d.append(1)
print(id(d))


Print (ID (C))
Fun (C)
Print (C)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
the Python dictionary and set of
dictionaries can store any type of object , other containers such as model strings, numbers, and other tuples. Each key-value dictionary key: value pair separated by a colon, between each key pair comma-delimited, comprising the entire dictionary in curly braces {}. A value corresponds to only one key, the same key value into the plurality of values will overwrite the previous value of the values loaded into.
.get () method can be obtained corresponding to the specified key value, if the key does not exist None is returned. pop () method to delete a key: value.
Note: The
order independent internal dictionary to store the order and put the key. Dictionary lookup and insertion of extremely fast, the key will not increase slowed, but it would take a lot of memory, memory and more waste. It is a time of a way to use space in exchange.
Dictionary key must be immutable objects. Dictionary (Dictionary) value is calculated based on the storage location key, the algorithm calculates the position referred to by the hash algorithm key. To ensure the correctness of the hash as a key target can not be changed. In Python, string, integer, etc. are immutable, and therefore, can be safely used as key. The list is variable, it can not serve as key.

Collection can be viewed as a set of unordered and no duplicate elements in a mathematical sense. The same can not be set into the variable object. Set for storing a set key, but the value is not stored, in the set, no duplicate key.
We can use curly braces {} or the set () function to create a collection. Create an empty set must be set () instead of {}, {} as is used to create an empty dictionary.
add () method can be added to the set of elements, remove () method to remove elements, the intersection of two set can also do a mathematical sense, the union and other operations.

Set of principles to Python weight
to weight set the two functions by binding __eq__ __hash__ and implemented. When the hash values of two variables are not the same, I think that these two variables are different; when the same hash values of two variables, call __eq__ method, when the return value is True think that these two variables are the same should remove one. Return FALSE, not heavy.

Python lists and tuples
listing i.e., a comma-separated values in square brackets appear. Data items list need not have the same type, and the list element in the list may be another. Such as:

= M [ 'A',. 1, for 1.5]
. 1
len () can be obtained by the number of list elements, .append () add an element end, .insert () specifies a position of the insertion element, pop () Removes the specified position of the element.
Tuples and a list of similar, except that the elements of the tuple can not be modified. Tuple using parentheses (), a list of square brackets []. Tuple no .append () ,. insert (), pop () method. Tuple access and access to other elements of the method and list is the same. When defining a tuple, wherein the elements must be determined.
You must add a comma when only the definition of a tuple elements ,, to disambiguate:

T = (1,) # which is a tuple
a
tuple is immutable, but if a tuple is a list, the elements in the list is variable.

Guess you like

Origin www.cnblogs.com/dianziyihao/p/12446604.html