python learning Notes (0) python basic concepts

A character set

Said before the character set, start the next two 229 story, only more stupid computer binary know, what is binary, that is, 0, 1, and maybe only know digital computer, the other did not know, so 0 1 or as a "bit", provides eight to a byte, it is a basic unit of a real-world characters, such as a letter, a byte is 8 bits, which is able to store up to 8 a 0 or 1, 8-bit binary maximum is 255, how the conversion due to my bad math. . . Here is not to say, interested in their conversion right. . In front of bedding so much for what it is, it is to say following these, since computers only recognize 0 and 1, that indicate how these letters, and characters, punctuation do? The computer is old gang buddies invention of the United States, then they created the ASCII code this thing, this thing is doing it, it is to use these symbols to represent numbers, numbers that are experiencing these representatives know what is, and then digital then converted to binary, so that the computer can keep these symbols in English, the Americans gang buddies, with 127 figures, representing all of the English upper and lowercase letters and symbols, this is the ASCII code table, http: //www.96yx .com / tool / ASC2. htm. So the question is, Americans gang buddies computer knowledge in English, then how do we Chinese compatriots, extensive and profound Chinese characters, as well as what the word minority, how to do, we have to use the computer ah! But as already said, a digital byte maximum deposit is 255, the old gang buddies have spent US 127, and the rest is not enough ah, add text characters minorities have at least tens of thousands, which may how to do it, Chinese people are smart, Nazan fewer points, 127 behind and got a few numbers, specifically with how much I remember, as long as the encounter that number range, you know Chinese, to another a code table inside to find that the Chinese are kept inside, as well as documents from other countries, such as Japanese, Korean and so on, this is called gb2312 encoding, which contains 6000 characters, this is the case, you can not solve computer Chinese understanding of the problem. But the Chinese culture, profound, there is more than 6000 characters, then there has been gbk, Unicode, UTF-8 encoding, etc., Unicode encoding, also known as Unicode, the text of which country are applicable, but it's whether you are a letter or a Chinese character is 2 bytes size of the original ASCII code letters on a one byte, which look bigger, something original 100G, and now may have to just keep at 200G, which can not, then one arose UTF-8 character set, it also belongs to Unicode, and Unicode is not the same, it did Unicode compression, such as letters or one byte, so it saves a lot of space this is the reason why we are now using utf8.

Two, python installation and installation pycharm

I can not say here, on-line pile.

Third, run python code

Under Windows, after installing python and configured environment variables, directly on the command line input can enter python python interactive command line, linux below is the same, what is it interactive, interaction is that you give me a word, I respond to you the way, this is interactive, look at the chart below, after you install python Python also comes with an idle, that is, you can write code in it, but the comparison does not work well, not go into here.

 

 

 But this is too inconvenient to write the code, and how the code is written, and then run with it, is to write the code inside the file, and then run the file to .py python is the end of the file, there are a lot of python editor, for example pycharm, sublime text, notepad ++, etc., can be used, using the editor has code hints, you can easily debug and run, I recommend using pycharm. Create a python file, and then write the code, run it, as shown below:

 

 

 The same is true run under linux, and Windows under a little different is that you can run under liunx python files directly, no need to add in front of the python command, with authority to execute, but the foremost need to specify python interpreter python file, plus the path to the python interpreter, there are two ways for writing, look at the code below, the difference between the two is the first one is to go directly to your specified directory to find the python interpreter, the second is the environment variable to configure their own find the python interpreter, and now a new test.py of python files.

#! / usr / bin / python # This wording is to find python under / usr / bin directory interpreter, if there is no interpreter, python or python interpreter in this directory in another directory, the error will be the 
print ( 'the Hello world!') 
    
#! / usr / bin / env python # this wording is to find the environment variable directory of your python interpreter configuration, your configuration in which directory, you will find a directory to which the python interpreter is 
print ( 'Hello world!')

  

The following are the run # Linux 
[the root @ ~ EBS-32534] # the test.py the chmod + X 
[the root @ ~ EBS-32534] # ./test.py 
the Hello World!

 

Fourth, variable

Variables are doing it? It said simply, is keep things come back for use. python define variables is very simple, to get an equal sign, do not need to specify the data type, like the definition of direct, it is worth mentioning that the python variable to store the memory address, the value of which is the existence of this local memory inside, if then assign this variable to another variable, then, before the new variable by variable know that the memory address of the variable value to save it rather than the variable before pointing. code show as below:

= name 'NHY' 
NEW_NAME = sriba is stored in the memory address name #new_name

  

Define rules variables:
variable name to see to know the name of Italy, not Xia Xie, Xia Xie to the back of their own can not read that variable is doing, do not use pinyin, so very low, but do not use Chinese as the variable name, that 2b is a programmer do, but python can indeed use Chinese as the variable name!
Variable names only letters, numbers, or underscore any combination of
the first character of a variable name not a number
of the following keywords can not be declared variable name

 ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 
     'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 
     'try', 'while', 'with', 'yield']

 

Five, Python the single and double quotation marks, and three marks (that is, three single quotes)

In python defined variables are used when a string enclosed in quotation marks, single and double quotes is no different, with consequently the line, if the string inside single quotes, then you will use double quotes outside, there are double quotes, then outside to use single quotes, double if there are both single, so use three quotes, triple quotes can also be multi-line comments in the code, single-line comments, use # code is as follows:

= MSG " the I'm Sriba. " # single quotation marks, so the outside double quotes 
info = ' the Python UES Comments "#." ' ## double quotes, so the outside in single quotes 
new_msg = '' ' the I ' m Sriba , I Love " the Python " . '' ' # double and double quotation marks, so that the outside with three marks 
' '' 
    above code is a single and double quotation marks and three marks of 
    this section is a note about three quotation marks how line comments functionality 
'' '

 

Sixth, input, output

python how it to receive user input using the input function, using the raw_input python2, a character string is received, it outputs, the first program has been written using the Print, the code into the next:

input at the time of receiving input, can see the value you enter, if it is to enter a password so it did not want to let others see your password, how to do it, you need to use a standard library, getpass, what is standard library, that is, you do not need to go to the installation, there Bahrain python library is the standard library, getpass is a standard library, and then import in, directly getpass.getpass method can be entered at the time, does not echo , and this module can not be used inside pycharm, as follows:

python, conditional use to determine if else, if the use of multi-branch if elif ... else, if that is how how to how to how, otherwise how come this way, in the following format:

python indentation is expressed in a block of code, so the code looks very structured, such as the example above, if it rains tomorrow, then tomorrow will take an umbrella, an umbrella tomorrow equivalent if the son, so it can be I think there are all indented parent-child relationship, the actual point of writing example, as follows:

                = Score int (input ( ' Enter your score: ' )) # receive input received is input as a string, it is necessary to use type to an integer function int cast
                 IF Score == 100 : If the score is equal to # 100 points, then 
                    Print ( ' little genius, you're out ' ) 
                elif score > = 90 and score < 100 : # If the score is greater than or equal to 90 points less than 100 points, then 
                    Print ( ' brother, your score is not low ah, yes ' ) 
                score elif > 60 and score < 90 : # If you score more than 60 points less than 90 points, then 
                    Print ( ' brothers, the exam so-so ah' )
                 The else : # If you score less than 60 points, then 
                    Print ( ' Brothers, what you do, that the efforts ' )

 

Eight, cycling

Cycling is doing it, it means you repeat things to do, for example, you want to build a 1000 folder, one by one to build kill you, so that you can write the code segment, using loops you create on 1000 save trouble, python, there are two loops, while and for, the cycle difference between the two is that before the while loop, first determine the time, if the conditions are met, recycled, for cycle time must have an object can be iterative, in order to cycle, for example, have to have an array, it is worth mentioning that other languages, for circulation when the need to define a counter variable, and then began to increase from 0 until the counter reaches a preset value you, then stop cycle, take the time data is also beginning to take through the array index from 0, this is very troublesome, python in the for loop is very simple, the cycle is an element iteration object, the object you how many elements in it the number of loops, for example, an array list, list = [ 'a', 'b', 'c'], in other languages ​​in order to obtain all the values ​​in the list, using a loop have to remove the standard which Ways to get the data, you have to write list [x], list [x], list [x] Thus, the Python is no need to take direct recycle list is inside this value, there is also two cycles more important keywords, continue and break, continue mean, out of this cycle, to continue for the next cycle, break means stop the loop, that is to say in the break and continue the following code is not executed, format as follows:

 

# The while loop 
    count = 0 
    the while count < 10 : # If the count is less than ten words, to execute the following code, if not less than 3 left the else 
            Print (count) 
            IF count == 5 :
                 BREAK # if count is equal to 5, then, it is the end of the cycle 
            count + = 1 # this means that each complete cycle, the value of count is incremented, if not, then, conditions have been true, and on the cycle of death, has been kept circulating
     else : # this is else can not write, meaning that, if the conditions are not met to why 
            Print ( ' conditions is not true ' ) 
    # for loop 
    names = [ ' Marry ' , ' Lily ', ' Lilei ' ]
     for name in names:
         IF name == ' lily ' : 
            contiune # If the name is equal to lily, you would not continue to perform the following code, and a recirculating 
         Print (name) 
    else : there is also #for else , but this is generally no one wrote it, meaning that if the normal cycle is over what to do 
        Print ( ' over ' )

 

Guess you like

Origin www.cnblogs.com/wuzm/p/11535529.html