1. The first Python program (running in Pycharm, it feels good )
Comments:
When line comment: #被注释内容
Multi-line Comment: ' Annotated content'
2. Variables
(1) Definition of self-understanding (for reference only): A custom legal identifier (in memory) to store data for repeated invocation.
For example: name = "Congcong" #name即为一个变量.
(2) Rules for variable definitions:
<1> variable names can only be any combination of letters, numbers, or underscores.
<2> the first character of a variable name cannot be a number.
The <3> keyword cannot be declared as a variable name.
Includes [' and ', ' as ', ' assert ' (claim), ' Break ', ' class ', ' Continue ', ' def ', ' If ', ' elif ', ' Else ', ' for ', ' except ', ' finaly ',
' exec ' (execution sequence), ' form ', ' Global ', ' import ', ' in ', ' is ', ' Lambda ' (anonymous program), ' not ', ' or ', ' pass ', ' print ', ' Rais E ' (Increase),
' Return ', ' try ', ' while ', ' with ', ' yield ' (yield)].
3. Representation of constants
The letter must be uppercase and cannot be modified, or an error will occur.
Example: PHR = 3
4. Character encoding
(1) When the Python interpreter loads the code in the. py file, the content is encoded (by default, ASCII)
(2) ASCII code (American Standard Information Interchange Code) is a set of computer coding system based on Latin alphabet, which is mainly used to display modern English and other Western European languages,
It can be used at most 8 bits to represent (that is, one byte, 2**8=256), so the ASCII code can only represent 255 characters. if (material from online):
(3) History of character encoding:
ASCII code--1980, GB2312, support more than 7,000 Chinese characters--1995 years, GBK1.0, support more than 20,000 Chinese characters--2000 years, GB18030, support more than 27,000 Chinese characters
-- Unicode, unified to 2bytes (bytes), as a result of the controversy, and then developed a utf-8--utf-8, support in English and Chinese, English 1 letter is 1byte, and the English character is 3bytes.
5. User interaction Program (if no special instructions, the following Python code is running in Pycharm, available)
1 #Congcong for Huangyuting2 ImportGetpass#导入getpass模块,set the password to not visible3_usename ='Congcong' #预存用户名4_password ='huangyuting' #预存用户名密码5Usename = input ("Usename:") #用户输入用户名6Password = input ("Password:") #用户输入密码7 if_usename = = Usename and_password = =Password: #表判断, continue when two conditions are established8 Print('Welcome User {name} login ...'. Format (name =usename)) #.format () Get user name9 Else:Ten Print('Invalid usename or password')
6.IF...ELSE judgment structure (with while)
1 #Congcong for Huangyuting2Age_of_erha = 213 4Count =05 whileCount < 3:6guess_age = Int (input ('Guess Age:'))7 ifGuess_age = =Age_of_erha:8 Print('You are right!')9 BreakTen elifGuess_age >Age_of_erha: One Print('Think smaller.') A Else : - Print('Think bigger ...') -Count + = 1 the ifCount = = 3: - Print('want to continue ....') -Is_continue = input ('Continue:') - ifIs_continue! ='N': +Count =0 - #Else:
View Code
7. while and for loops
1 #Congcong for Huangyuting2 " "3 For i in range (0,15,2): Starts at #从 0, 15 ends, each time difference is 24 print (' number ', i)5 " "6 7 " "8 Age_of_erha =9 For I in range (4):Ten guess_age = Int (input (' Guess Age: ')) One if Guess_age = = Age_of_erha: A print (' You're right! ') - Break - elif guess_age > Age_of_erha: the print (' Think smaller. ') - Else: - print (' Think bigger ... ') - Else: + print (' You have tried too many times ... ') - " " + A #use of Continue at " " - For i in range (0,20,2): - if (i > 7): - print (' Congcong ', i) - Else: - continue #跳出本次循环, continue the next loop, and break is the end loop in print (' Hello ') - " " to #Multiple for Loop + forIinchRange (0,8,1): #小于8, not including 8 - Print('hello ... ...', i) the forNinchRange (0,6,1): * Print(' World', N) $ ifn > 3:Panax Notoginseng Break #End the nearest loop -#多重循环打印结果如下:
1 hello ...... 02 World 03World 14World 25World 36World 47Hello ...... 18 World 09World 1TenWorld 2 OneWorld 3 AWorld 4 -Hello ...... 2 - World 0 theWorld 1 -World 2 -World 3 -World 4 +Hello ...... 3 - World 0 +World 1 AWorld 2 atWorld 3 -World 4 -Hello ...... 4 - World 0 -World 1 -World 2 inWorld 3 -World 4 toHello ...... 5 + World 0 -World 1 theWorld 2 *World 3 $World 4Panax NotoginsengHello ...... 6 - World 0 theWorld 1 +World 2 AWorld 3 theWorld 4 +Hello ...... 7 - World 0 $World 1 $World 2 -World 3 -World 4
View Code
Python First Knowledge-day1