And let us all remain beginner's mind--
Have known the universe, and still pity the green grass.
I. Introduction of Python
First, we popularize the basics of programming languages. In any programming language to develop the program, is to let the computer work, such as downloading a MP3, writing a document, and so on, and the computer working CPU only know the machine instructions, so, although the different programming languages are very big differences, and finally have to "translate" the CPU can execute machine instructions. and different programming languages, the same work, the amount of code written, the gap is also very large.
For example, to complete the same task, C language to write 1000 lines of code, Java only need to write 100 lines, and Python may be as long as 20 lines.
Two, the first Python program
Third, variable
A variable can only use a combination of letters, underscores, numbers, and cannot start with a number.
The commonly used variable naming methods are:
Underline naming method: Gf_name = "xxx" #python建议使用这种命名方法
Hump naming method: Gfname = "XXX"
Another uppercase representation is a constant and is not recommended to change, such as:
PIF = 12345
Python reserved words cannot be used for variable naming
[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' fi Nally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ', ' return ', ' Try ', ' while ', ' with ', ' yield ']
This example can be noted: Name2 is the "Alex Li" string pointed to by name.
!/usr/bin/env Python3author:alnk
name = "Alex Li"
name2 = Name
Print ("My name is", name,name2)
name = "Paoche GE"
Print (name,name2)
Four, character encoding
Binary: The bottom of the computer only knows 0 and 1
ASCII code: 8-bit 1-byte, up to 255 characters
gb2312:7000 multiple Chinese characters, 1980
gbk:21000 multiple Chinese characters, compatible with gb2312,1995 year
gb18030:27000 a number of Chinese characters, but also included Tibetan, Mongolian, Uyghur, etc., 2000
ASCII--GB2312--GBK-GB18030
Mobile, MP3 generally only support gb2312,pc must support GB18030 encoding
Unicode: Compatible with all character encodings, 2 bytes 16 bits, 65,525 characters, but each character occupies 2 bits, which is wasteful relative to ASCII code.
Utf-8: Solving the problem of the waste of English characters in Unicode, English characters are 1 bytes, Chinese characters account for 3 bytes
Five, user input, output
Input () function: The value entered is a string
get integers using the Int () function
Using the float () function to get the floating-point number
Ciphertext input using the Getpass module
Formatted output
% for formatted output
Output results
Format formatted Output Method 1
Output results
Method 2
Output results
Vi. If ... else judgment statement of Elif
If.. Else statement: If the IF condition is true, execute the code after the IF statement, and then end. Otherwise execute the code of the Else statement.
!/usr/bin/env Python3author:alnk
_username = "Alnk"
_password = "123"
Username = input ("Username:")
Password = input ("Password:")
If _username = = Username and _password = = password:
Print ("Welcome name%s Login ..."% username)
Else
Print ("Invalid usernmae or password!")
If Elif ... else: first determine if the IF condition is true, if it is, execute the IF statement's code, and judge the end. Otherwise determine whether the Elif statement condition is true, if it is established, execute the code of the Elif statement, otherwise execute the code of the Else statement.
!/usr/bin/env Python3author:alnk
Age_of_oldboy = 56
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_oldboy:
Print ("Yes,you got it.")
Elif guess_age > Age_of_oldboy:
Print ("Think smaller ...")
Else
Print ("Think bigeer!")
Seven, circular statements
While loop statement: Stops the loop when the condition is not met.
Break: Terminates the deepest loop, typically for a while loop and for loop.
Continue: Jumps out of this loop and goes to the next loop, typically for the while loop and for loop.
While ... else statement: Go if the while condition is not true
!/usr/bin/env Python3author:alnk
Age_of_oldboy = 56
Count = 0
While Count < 3:
guess_age = Int (input ("Guess Age:"))
if guess_age == age_of_oldboy: print("yes,you got it.") breakelif guess_age > age_of_oldboy: print("think smaller...")else: print("think bigeer!")count +=1
Else
Print ("You've lost too many times, end")
Guess number game, guess wrong three times prompt whether to continue
!/usr/bin/env Python3author:alnk
Age_of_oldboy = 56
Count = 0
While Count < 3:
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_oldboy:
Print ("Yes,you got it.")
Break
Elif guess_age > Age_of_oldboy:
Print ("Think smaller ...")
Else
Print ("Think bigeer!")
Count +=1
if Count = = 3:
Continue_confim = input ("Does want to keep guessing?" ( y/n) ")
If Continue_confim! = "N":
Count = 0
For loop
For loop printing 99 multiplication table
!/usr/bin/env python3author:alnk99 Multiplication Table
For I in Range (1,10):
For j in Range (1,i+1):
Print ("%s *%s ="% (j,i), i*j,end= ' \ t ')
Print ()
For ... else statement: The for code block executes the Else statement when it ends normally
!/usr/bin/env Python3author:alnk
Age_of_oldboy = 56
Count = 0
For I in range (3):
guess_age = Int (input ("Guess Age:"))
if guess_age == age_of_oldboy: print("yes,you got it.") breakelif guess_age > age_of_oldboy: print("think smaller...")else: print("think bigeer!")count +=1
Else
Print ("You've lost too many times, end")
Python introduction, First Python program, variable, character encoding, user interaction program, If...else, while, for