Python study note _ week1, python_week1
I. Classification of programming languages
- Compiled and interpreted, static, dynamic, strong, and weak definition languages. Python is a strong type definition language with dynamic interpretation.
- Advantages and disadvantages of Python
- Python Interpreter: CPython, IPython (many financial products, interactive tools), PyPy (recommended, fast), JyPython, IronPython
Ii. Python Development History 3. Python 2 or 3? Iv. Python installation 5. Hello World Program
- Pycharm development tool, high development efficiency, help debugging
Vi. Variables
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 '] indicates constants in uppercase.
1 name = "JYH" 2 name2 = name 3 print ("My name is", name) 4 5 name = "PaoChe Ge" 6 print (name, name2) 7 8 gf_of_oldboy = "xxx" 9 GFOfOldboy = "xxx" # Windows variable names like this write 10 11 PIE = 3.1415926 # all uppercase Constants
View Code 7. character encoding
The bottom layer of the computer only knows 0, 1 (power-on or power-off)-binary (Wolf smoke indicates the number of enemy troops). The Python explanation is loading. when the code in the py file is used, the content is encoded (the default is ASCII ).
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), that is, 2 ** 8 = 256-1. Therefore, the ASCII code can only represent 255 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...
8. user input
Input is a string by default.
1 name=input('''name:''')2 age=input("age:")3 job=input('job:')4 salary=input('salary:')5 6 info='''Name:'''+name+''+ '\nage:'+age+'\njob:'+job+'''\nsalary:'''+salary7 print(info)
Interaction
1 name = input (''' name: ''') # raw_input 2.x is the same as input 3.x, and input 2.x is a redundant 2 age = int (input ("age: ") # force convert to integer (integer) 3 job = input ('job: ') 4 salary = input ('salary:') 5 print (type (age )) # print variable type 6 age = str (age) 7 print (type (age) 8 9 info0 = ''' Name: ''' + name + ''+ '\ nage:' + age + '\ njob:' + job + ''' \ nsalary: ''' + salary # Do not use 10 11 info1 = ''' Name: % s \ nage: % s \ njob: % s \ nsalary: % s ''' % (name, age, job, salary) # % s placeholder, equivalent to $12 13 info2 = ''' Name: {_ name} \ nage in shell: {_ age} \ njob: {_ job} \ nsalary: {_ salary }'''. format (_ name = name, _ age = age, _ job = job, _ salary = salary) 14 15 info3 = '''name: {0} \ nage: {1} \ njob: {2} \ nsalary: {3 }'''. format (name, age, job, salary) 16 print (info0)
Interaction2 9. Initial Knowledge of the module
(Library) 1. Standard Library: can be imported directly without installation. 2. Third-party libraries must be installed (such as Django external framework ). The import module is first located in the current directory, so do not name the new. py with the same name as the module.
Third-party libraries usually exist in the site-package, and some installed libraries are also included. The standard library is in lib, such as re. py (regular), socket. py (used for network programming), threading. py (used for multithreading )...
You can write a module by yourself. During the import, the module is automatically located in the current directory and environment variables.
1 import OS 2 3 # export _res = OS. system ("dir") # OS. system runs the command once it is called and outputs the result to the screen without saving the result 4 # print ("-->", performance_res) #0 indicates that the command is successfully executed. 5 6 # Running _res = OS. popen ("dir") 7 # print ("-->", cmd_res) # print the memory object address 8 9 # cmd_res = OS. popen ("dir "). read () # The result of the command is stored in a temporary place in the memory. You must use the read method to obtain 10 # print ("-->", resource_res) 11 12 OS. mkdir ("new_dir") # create a new folder in the current directory
OS _mod
10. What is. pyc?
Python, like Java/C #, is also a virtual machine-based language.
Before talking about this, let's talk about two concepts: PyCodeObject and pyc files.
The pyc we see on the hard disk naturally does not need to say much, but in fact, PyCodeObject is actually compiled by the Python compiler. We can simply know it and continue to look down.
When the python program runs, the compilation result is saved in the PyCodeObject in the memory. When the Python program runs, the Python interpreter writes the PyCodeObject back to the pyc file.
When the python program runs for the second time, the program first looks for the pyc file in the hard disk. if it finds the file, it loads it directly; otherwise, the above process will be repeated.
Therefore, we should locate the PyCodeObject and pyc files in this way. We say that the pyc file is actually a persistent storage method of PyCodeObject.
If modified, compare the Update Time of. pyc and source code. If the same, load it directly.
11. Initial Knowledge of Data Types
Python2.x contains long integers and integers. Python3.x does not have long integers.
Float (float type) can be considered as decimal, but not completely equivalent to decimal.
Boolean value: true or false, 1 or 0
12. Data Operations
- Arithmetic Operators
- Comparison (relational) Operator
- Value assignment operator
- Logical operators
- Bitwise operators
- Member Operators
- Identity Operators
- Operator priority
Appendix
- Hexadecimal, 0123456789 ABCDEF binary to hexadecimal conversion http://jingyan.baidu.com/album/47a29f24292608c0142399cb.html? Picindex = 1
- Bytes type (byte data type (Binary ))
- The text is always Unicode, represented by str. Binary data is represented by bytes (audio and video ). The conversion of text and binary is mainly used for transmission, encoding, decoding, and conversion.
For:
1 for i in range(0,10,2):2 print("loop",i)
For
Continue:
1 while True: 2 s = input ("enter:") 3 if len (s) <:4 print ("too small") 5 continue # Skip this loop, continue next loop 6 if s = "quit": 7 break # End loop 8 print ("enough ")
Continue
Guess:
1 age_of_oldboy=562 guess_age=int(input("guess age:"))3 if guess_age==age_of_oldboy:4 print("Yes!you got it...")5 elif guess_age>age_of_oldboy:6 print("Think smaller...")7 else:8 print("Think bigger!")
Guess
For_guess:
1 age_of_oldboy = 56 2 count = 0 3 for I in range (3): 4 guess_age = int (input ("guess age:") 5 if guess_age = age_of_oldboy: 6 print ("Yes! You got it... ") 7 break 8 elif guess_age> age_of_oldboy: 9 print (" Think smaller... ") 10 else: 11 print (" Think bigger! ") 12 count + = 113 else: # Same as if count = 3: 14 print (" you tried too wrong times... fuck off ")
For_guess
Guess_at_will
1 age_of_oldboy=56 2 count=0 3 while count<3: 4 guess_age=int(input("guess age:")) 5 if guess_age==age_of_oldboy: 6 print("Yes!you got it...") 7 break 8 elif guess_age>age_of_oldboy: 9 print("Think smaller...")10 else:11 print("Think bigger!")12 count +=113 if count == 3:14 continue_confirm=input("Do you want to keep guessing? Press ant key to keep guessing and press N or n to quit:")15 if continue_confirm !="N" and continue_confirm!="n":16 count=0
Guess_at_will
Loop_in_loop
1 for I in range (10): 2 print ("-----", I) 3 for j in range (10): 4 print (j) 5 if j> break # ends the current loop
Loop_in_loop
Passwd:
1 import getpass # getpass ciphertext 2 username = input ("username:") 3 password = getpass. getpass ("password:") # It is difficult for the getpass module to make 4 print (username, password) in pycharm)
Passwd
1 _username='jyh'2 _password='1234'3 username=input("username:")4 password=input("password:")5 6 if username==_username and password==_password:7 print('Welcome user {} login...'.format(_username))8 else:9 print('Invalid username or password')
Passwd2
1 import passwd2
Passwd_import
Sys:
1 _ author __= "jyh" 2 3 import sys4 # print (sys. path) # print the environment variable 5 6 print (sys. argv) # print the absolute path in pycharm, which is actually the relative path 7 print (sys. argv [2])
Sys_mod
While:
1 count = 12 while True: 3 count = count + 1 # Same as count + = 1 4 print ("count:", count)
While
1 age_of_oldboy = 56 2 count = 0 3 while count <3: 4 guess_age = int (input ("guess age:") 5 if guess_age = age_of_oldboy: 6 print ("Yes! You got it... ") 7 break 8 elif guess_age> age_of_oldboy: 9 print (" Think smaller... ") 10 else: 11 print (" Think bigger! ") 12 count + = 113 else: # Same as if count = 3: 14 print (" you tried too wrong times... fuck off ")
While2