Python Module 1 Assessment, Python module Assessment

Source: Internet
Author: User
Tags virtual environment

Python Module 1 Assessment, Python module Assessment

1 .#! /Usr/bin/python and #! /Usr/bin/env python meaning

#! /Usr/bin/python or #! /Usr/bin/env. This statement is mainly related to the running mode,

If we use a normal running mode such as (linux): python *. py, this statement is invalid in this running mode. If you want python programs to run like normal programs, for example :. /*. py (the file must have the executable permission chmod a + x *. this statement is used to specify the interpreter for the script language #! /Usr/bin/env python = #! /Usr/bin/python is better, because the python interpreter is sometimes not installed in the default path, for example, in a Virtual Environment.

Explanation:

#!/usr/bin/env python
This method makes sense when multiple versions of python are installed on your machine. In this way, the first python specified in your machine's PATH will be used to execute your script. If you configure the virtual environment again at this time, you can ensure that the script will be executed using python in your virtual environment.
#!/usr/bin/python
It indicates that if the script is written to the/usr/bin/python directory, python will execute your script. In this way, the portability of writing programs is poor. If the python command in this path does not exist, an error is returned.

Therefore, the first method is usually used.

2. 1 byte = 8 bits
3. variable naming rules
    • 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 ']
4. global variables and local variables
5. What type can the dictionary key be?
Immutable data can be used as keys, such as numbers, strings, and tuples.
6. Relationship between ascii unicode UTF-8 gbk
7. The output result of the following code is "growth % is troublesome"
1 s = "growth % is annoying % s" 2 n = s % ('annoyance ') 3 print (n)
 

8. Can the following data types be changed? Can the internal "k2" be changed? If yes, how can I add a string. What is the difference between two copies?

("Alex", [11, 22, {"k1": 'v1 ', "k2": ["age", "name"], "k3": (11,22, 33)}, 44])

A = ("alex", [11, 22, {"k1": 'v1 ', "k2": ["age", "name"], "k3 ": (11,22, 33)}, 44])

A [1] [2] ["k2"]. append ("string ")

The shortest copy only copies the first layer.

9. Difference Between break continue pass

10. Evaluate the difference set s. difference (t)

11. lambda anonymous Functions

12. The running result of the following code is None. If the function does not return a value, None is returned by default.

1         def func(arg):2             arg.append(55)3 4         li = [11, 22, 33, 44]5         li = func(li)6         print(li)

13. Use the while loop to output the sum of 1-2 + 3-4 + 5...-100. The answer is-50.

Three methods have been developed:
1 # ------------------- Method 1: modulo ------------------------------- 2 3 s = 0 4 I = 1 5 while (I <101): 6 if (I % 2 = 1 ): 7 s = s + I 8 else: 9 s = s-i10 I = I + 111 print (s) 12 13 # --------------------- Method 2: exponential functions ---------------------------- 14 15 s = 016 I = 117 while (I <101): 18 s = s + pow (-1, I + 1) * i19 I = I + 120 print (s) 21 22 # ---------------------- method 3: Variable variable Variable Sign ----------------------------- 23 s = 024 I = 125 j = 126 while (I <101 ): 27 s = s + I * j28 I = I + 129 j =-j30 print (s) 31-
 

14. print the number of 1-100 elements that can be divisible by 3 and divisible by 5

1 for I in range (1,101): 2 if (I % 3 = 0 and I % 5 = 0): 3 print (I) 4 5 ''' 6 answer: 7 15 8 30 9 4510 6011 7512 9013 '''

 

 
 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.