Day1, day
1. The first Python code
Create the hello. py file in the/home/dev/directory. The content is as follows:
1 [root@python-3 scripts]# cat hello.py 2 #!/usr/bin/env python3 4 print("Hello World!")
Output result:
1 [root@python-3 scripts]# python hello.py 2 Hello World!
Ii. Interpreter
When running python/home/dev/hello. py in the previous step, it is clearly indicated that the hello. py script is executed by the python interpreter.
If you want to execute a python script like a shell script, for example:./hello.py
In the header of the hello. py file, specify the interpreter as follows:
#!/usr/bin/env python print "hello,world"
In this way, run :./hello.py
You can.
Ps: You must grant the "hello. py" execution permission before execution. chmod 755 hello. py otherwise, an error is reported!
Iii. Content Encoding
When the python interpreter loads the code in the. py file, it will encode the content (ascill by default)
ASCII (American Standard Code for Information Interchange) is a computer coding system based on Latin letters. It is mainly used to display modern English and other Western European languages, it can only be expressed in 8 bits (one byte) at most, that is, 2 ** 8 = 256. Therefore, the ASCII code can only represent 256 symbols at most.
Obviously, ASCII codes cannot represent all types of characters and symbols in the world. Therefore, you need to create a new encoding that can represent all characters and symbols, that is, Unicode.
Unicode (unified code, universal code, Single Code) is a character encoding used on a computer. Unicode is generated to address the limitations of traditional character encoding schemes. It sets a uniform and unique binary encoding for each character in each language, it is specified that some characters and symbols are represented by at least 16 bits (2 bytes), that is, 2*16 = 65536,
Note: here we will talk about at least 2 bytes, maybe more
UTF-8 is the compression and optimization of Unicode encoding, which no longer uses at least 2 bytes, but classifies all characters and symbols: the content in the ascii code is saved in 1 byte, the European characters are saved in 2 bytes, and the East Asian characters are saved in 3 bytes...
Therefore, when the python interpreter loads the code in the. py file, it will encode the content (ascill by default), if it is the following code:
Error: ascii code cannot indicate Chinese Characters
1 #! /Usr/bin/env python2 3 print "Hello, world"
Correct: Tell the python interpreter what encoding is used to execute the source code, that is
1 #! /Usr/bin/env python2 #-*-coding: UTF-8-*-3 4 print "Hello, world"
Iv. Notes
When watching: # commented content
Multi-line comment: "commented content """
5. Execute the script to input parameters
Python has a large number of modules, making it very simple to develop Python programs. There are three class libraries:
- Python internal modules
- Open-source modules in the industry
- Modules developed by programmers themselves
Python provides a sys module, where sys. argv is used to capture the parameters passed in when the python script is executed.
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 import sys5 6 print sys.argv
Vi. pyc File
When you run the Python code. py file, a file with the same name is automatically generated during execution. pyc file, which is the bytecode generated after the Python interpreter is compiled.
Ps: After the code is compiled, it can generate bytecode. After the bytecode is decompiled, the code can also be obtained.
VII. Variables
1. Declare Variables
1 #!/usr/bin/env python2 3 # -*- coding: utf-8 -*-4 5 name = "nulige"
The code above declares a variable named: name, whose value is: "nulige"
The role of a variable: nickname, which represents the content stored in an address in the memory.
Rule for variable definition:
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of the variable name cannot be a number.
- The following keywords cannot be declared as variable names
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
2. Variable assignment
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 name1 = "nulige"5 name2 = "alex"
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 name1 = "nulige"5 name2 = name1
3. Variable assignment example
1 #Author: huzhihua2 name = "Alex li"3 name2 = name4 print("My name is " ,name,name2)5 6 name = "PaoChe Ge"7 print(name,name2)
Execution result:
1 My name is Alex li Alex li2 PaoChe Ge Alex li
4. Common usage of Variables
1 1) 2 _name = "Alex li" 3 4 2) 5 name = "Alex li" 6 7 3) 8 gf_gf_oldboy ="Chen rong hua" 9 10 4)11 GFOfOldboy = "Chen rong hua"
8. Input
1 #! /Usr/bin/env python2 #-*-coding: UTF-8-*-3 4 # assign the user input content to the name variable 5 name = raw_input ("Enter the User name: ") 6 7 # print the input content 8 print name
When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 import getpass 5 6 # assign the value of user input to name variable 7 pwd = getpass. getpass ("Enter Password:") 8 9 # print input 10 print pwd
9. Process Control and indentation
Requirement 1: user login verification
1 #! /Usr/bin/env python 2 #-*-coding: encoding-*-3 4 # prompt for entering the user name and password 5 6 # verify the user name and password 7 # If the error occurs, the output username or password is incorrect. 8 # If it succeeds, the output is welcome, XXX! 9 10 11 import getpass12 13 14 name = raw_input ('enter your Username: ') 15 pwd = getpass. getpass ('enter password: ') 16 17 if name = "alex" and pwd = "cmd": 18 print "Welcome, alex! "19 else: 20 print" incorrect user name and password"
Requirement 2: Output permissions based on user input
1 # print permissions based on user input 2 3 # alex --> super administrator 4 # eric --> common administrator 5 # tony, rain --> business supervisor 6 # Others --> common user 7 8 name =Raw_input('Enter your Username: ') 9 10 if name = "alex": 11 print "Super administrator" 12 elif name = "eric ": 13 print "General Administrator" 14 elif name = "tony" or name = "rain": 15 print "Business Director" 16 else: 17 print "general user"
Requirement 3: Determine whether the entered user and password are correct
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 4 #import getpass 5 6 _username = 'nulige' 7 _password = '123456' 8 username = input("username:") 9 #password = getpass.getpass("password:")10 password = input("password:")11 if _username == username and _password == password:12 print("Welcome user {name} login...".format(name=username))13 else:14 print("Invalid username or password!")
Execution result:
Enter the correct user name and password, and the prompt is: Welcome user nulige login...
1 username:nulige2 password:1234563 Welcome user nulige login...
Enter the wrong user name and password. The prompt is "Invalid username or password!
1 username:nulige2 password:3212113 Invalid username or password!
Requirement 4: determine the age
Example 1
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 4 5 age_of_oldboy = 56 6 guess_age = int(input("guess age:")) 7 if guess_age == age_of_oldboy: 8 print("yes, you got it. ") 9 elif guess_age > age_of_oldboy:10 print("think smaller... ")11 else:12 print("think bigger!")
Execution result:
1 guess age:232 think bigger!
1 guess age:582 think smaller...
1 guess age:562 yes, you got it.
Example 2
Enter three times to print out your have tried too times .. fuck off.
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 age_of_oldboy = 56 6 count = 0 7 8 while count <3: 9 guess_age = int(input("guess age:"))10 if guess_age == age_of_oldboy :11 print("yes, you got it. ")12 break13 elif guess_age > age_of_oldboy:14 print("think smaller... ")15 else:16 print("think bigger!")17 count +=118 if count == 3:19 print("you have tried too many times..fuck off")
Execution result:
1 guess age:12 think bigger!3 guess age:24 think bigger!5 guess age:36 think bigger!7 you have tried too many times..fuck off
Example 3
Input three times for condition judgment
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 age_of_oldboy = 56 6 count = 0 7 8 while count <3: 9 guess_age = int(input("guess age:"))10 if guess_age == age_of_oldboy :11 print("yes, you got it. ")12 break13 elif guess_age > age_of_oldboy:14 print("think smaller... ")15 else:16 print("think bigger!")17 count +=118 else:19 print("you have tried too many times..fuck off")
Execution result:
1 guess age:232 think bigger!3 guess age:454 think bigger!5 guess age:676 think smaller... 7 you have tried too many times..fuck off
Schematic:
10. while Loop
1. Basic cycle
1 while condition: 2 3 # loop body 4 5 # If the condition is true, the loop body executes 6 # If the condition is false, the loop body does not execute
Example
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #Author: huzhihua4 5 count = 06 while True:7 print("count:",count)8 count = count +1 #count +=19
2. break
Break is used to exit all loops.
Example 1:
1 while True:2 print ("123")3 break4 print ("456")
Execution result:
1 123
Example 2:
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #Author: nulige4 5 count = 06 while True:7 print("count:",count)8 count = count +1 #count +=1
Execution result:
1 count: 605452 2 count: 605453 3 count: 605454 4 count: 605455 5 count: 605456 6 count: 605457 7 count: 605458 8 count: 605459 9 count: 60546010 count: 605461 is omitted later .....
Example 3:
Meaning: Execute to 10 (from 0 to 9)
1 count = 02 while True:3 print("count:",count)4 count = count +1 #count +=15 if count == 10:6 break
Execution result:
1 count: 0 2 count: 1 3 count: 2 4 count: 3 5 count: 4 6 count: 5 7 count: 6 8 count: 7 9 count: 810 count: 9
Exercise questions
1. Use a while loop to input 1 2 3 4 5 6 8 9 10
2. Calculate the sum of all numbers from 1
3. Output all odd numbers in 1-100
4. Output all the even numbers in 1-100
5. Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99.
6. User Login (three chances to try again)