Introduction :(the article is quite long, it is suggested to read the table of contents to learn as needed ~)
I have heard about Python since I just learned programming, but the school only has C, C++, Java, C#. Just as PHP has the well-known “PHP is the best language “**, Python has “Life is short, I use Python”. The most common place to come across the word Python is in some technical groups, where some big wigs are always talking about Py deals. As a new developer lurking in the group, I can’t help but say something like:
I really got to know Python when I was in my last company. My boss asked me to write a script to download the apK recently packaged in the warehouse. At that time, I managed to put together a running PY file. After being educated by the eldest brother, I knew there was such a thing as tuples. As an Android developer, I didn’t use Python on many occasions, and I found Kotlin interesting, so I didn’t study Python.
Some time ago, do what I do about the same, boring look around, see next to the background Little elder brother thunderbolt download something crazy, build folder, copy paste, I don’t know thought him in to the small film classification, then ask just know, operating let him put all the audio files on the server, and then according to the corresponding category to the corresponding folder, So what he did was: At first glance, the process is quite simple, but there are more than 4700 songs, hundreds of directories, so slowly afraid is to make a few days, and often time mechanization of repeating a certain work, it is easy to make mistakes. Looking at the despairing backstage guy:
I decided to write a PY script to save him, and my mind also had the logic of the program:
- Mysql > install mysql > create table;
- 2. Connect Python to mysql to read the data in the table
- 3. Write SQL query statements with limit to get all categories
- 4. All categories obtained are folders to be created. Create folders iteratively in batches.
- 5. SQL query song download URL and classification, splicing, write into the file;
- 6. Read the file and iterate: download the URL to intercept the file name and the classification path to join the file
The complete path, call download related functions to download the URL to the corresponding path.
The logic was pretty straightforward, but it took a day to get out of the pit and encountered the following problems:
- 1. The most pitting decoding problem: the default use of Python2, Chinese garble, a variety of online search,
Setting the encoding method is useless, scalp tingling, especially when intercepting file names. After that, I changed Python3, which was very comfortable, and there were no problems.
- 2. Failed to catch exceptions, some resources failed 404, down to the middle of the discovery.
- 3. I want to do multithreaded download, because Python many basic do not know, later abandoned;
When I saw all the files downloaded to the corresponding location, I felt a sense of accomplishment. Compared with writing APP and drawing interface every day, requesting, parsing data and displaying data, it was much more interesting. After all, learning and developing interest is very important.
Reason to go to work when the leisure time, lasted two weeks finally is the basic knowledge of things over again, hence have this article, after all beginners, some places may understand wrong, hope to see the big guy is generous to give advice, most of the content of the article excerpts from: The Python 3 Tutorial is designed to teach you how to use Python. The Python 3 Tutorial is designed to teach you how to use Python. Check out this book: The First Python Primer for Programmers
directory
[TOC]
1. Learning resources
The API documentation
- Website: www.python.org/
- API:docs.python.org/release/3.6…
- Chinese API (3.5.2) : python. Usyiyi. Cn/translate/p…
- Awesome-python:github.com/vinta/aweso…
- Awesome-python: github.com/jobbole/awe…
books
- The Python 3 tutorial: www.runoob.com/python3/pyt…
- “Old qi zero based learning Python” : www.gitbook.com/book/looly/…
- Learning Python: A very good introduction to Python. I read this book
- Python Core Programming (3rd edition) : This is a very popular book
2. Learn Python2 or Python3
!!!!!!!!! Python 3 syntax is not fully compatible with Python 2 syntax!!
Python 3 uses UTF-8 by default, which saves a lot of codec problems when dealing with Chinese, while Python 2 uses ASCII by default.
Another reason: Keeping pace with The Times, the IT industry is evolving so fast that a complete transition is only a matter of time; For example, in the early days of Android Studio, Eclipse was not as good as it was because it was jammed, garbage, and could only open one project. However, there is a wave of discrimination against anyone developing Android using Eclipse, and Python 2.7 is officially set to retire in 2020. So: life is short. I use Python3.
And Python 2.7 is expected to retire in 2020
More comparative articles are available:
X and 3.x differences Python2orPython3 Differences between python3. x and python2. x
3. Development environment construction
Python download and Installation
Download website: www.python.org/downloads/, choose their own needs version and operating system.
- Windows
The next dumb step is ok, remember to check Add Python x.x to Path! If python3 is not an internal or external command, you can run the following command: The Python3 environment variable is configured with ~
- Mac
Method one: the official website installation package, fool next step; Method 2: If Homebrew is installed, enter brew install Python3 to install it.
- Ubuntu: Generally built-in, run the following command to view the version, if you want to install your own preference
To install python, type sudo apt-get install Python
PyCharm
In fact, after installing Python, Python programming can be carried out, directly enter the command line python3, you can use IDLE development; Or write code directly with a code viewer such as Sublime Text or NotePad++, save it to a.py file, and python3 executes the py file.
This can be done, but it can be quite inconvenient, such as indentation. Python uses indentation to represent blocks of code, and if one line of code is not properly indent, the result may be quite different from what you expected. Intelligent tips, convenient dependency library management, these two needless to say, the specific you have to experience.
Download website: www.jetbrains.com/pycharm/dow… Download the Professional version and install it in foolproof mode. The registration page is displayed. Select License Server and enter registration server in License Server Address.
The basic use of the software is also very simple. Jetbrains ides are almost the same. One important thing to note is that if you want to switch the Python version number that your project depends on, open Preference or Settings and change it:
4. Basic knowledge
1) package
Sometimes a project needs to introduce other modules or a function in a module, and you need to use import and from… Import, as shown in the following example:
import sys Import the entire module
from sys import argv Import the required parts of the module
from urllib.error import URLError, HTTPError Use commas to separate multiple entries
from sys import * Export all in the module
You can also use the AS keyword to alias the module, such as import sys as S
Call s.arrgv directly.
Copy the code
2) Naming rules for keywords and identifiers
When naming identifiers such as variable or method names, it is important to note that they are not the same as keywords in Python. All keywords can be queried using keywords. kwlist: import the keyword module
The identifiers must be named according to the following rules: They must consist of letters, digits, and underscores (_), and must start with a letter or underscore (_). Python is case-sensitive. As for the naming standard, there is no compulsory provision. It is good to keep the whole project unified. Attached is a naming rule found on the Internet:
- 1. Project name: capitalize the first letter and use underscores to increase readability, such as Ui_test;
- 2. Package name and module name: all lowercase letters;
- 3. Class name: uppercase, lower case, multiple words camel name;
- 4. Method (function) name: all lowercase, multiple words separated by underscores;
- 5. Parameter name: lowercase word. If it conflicts with keywords, underline the parameter name, such as _print.
- 6. Variables: Lowercase, separate multiple words with underscores;
- 7. Constants: all caps, multiple words separated by underscores;
3) comments
Python uses # for single-line comments and triple quotes for multi-line comments, such as:
"This is Python's multi-line comment" "
Copy the code
4) print print and input function
Learning a new programming language, the first program is basically print Hello world, print the result on the screen, is the most intuitive demonstration of the code execution result, so it is necessary to learn a wave of print and input usage!
print():
- 1. Can output a variety of messy types of data directly into a string print output;
- Print (XXX, end = “”)
- Printf (**%**);
input():
Reads a string from the keyboard and automatically ignores newlines. All forms of input are treated as strings. You can write some input prompts in parentheses, such as: input(” Please enter a string: “)Output result:
5) help function
Needless to say, a built-in function (BIF) that can be used in many programming languages, such as help(print), will print:
6) dir function
To view all properties and methods in an object, simply add the object to be queried in parentheses. For example, define a class and use dir to retrieve all properties and methods:
Partial output:
7) View all built-in functions (BIF)
Modules [‘builtins’])) to print all the built-in functions.
8) Multiple statements one line and one statement multiple lines
If you want to write multiple statements on a single line, use **; ** (semicolon) separated; Sometimes statements may be too long, you can use a \ * * * * (backslash) to hook, in * * [] {}, () in the * * don’t need to use the backslash to cohesion.
5. Data types
1) variable
In Python3 it’s very simple and crude to define a variable. The name of a variable equals the initial value of the variable and the assignment determines the data type of the variable. The name of a variable refers to a value and also refers to its type. **type** You can see the data type of the variable (also note that Python is case-sensitive!). :
Output result:
In addition, ** supports multiple variable assignments **. The following two assignments are correct:
a = b = c = 1
a,b,c = 1.2."Python"
Copy the code
By the way, you can also delete references to objects using the **del keyword, but calling variables after deletion will cause an error!
The output:
2) Numbers (Number)
Python3 supports three numeric types: int, float, and complex.
Note: in Python3, int does not distinguish between integers and long integers. Integers are of unlimited length, so it is easy to calculate large numbers. The non-decimal notation is as follows: binary 0b, octal 0O, hexadecimal 0x.
Example: a = 1 + 2j + 3 * 4j. Output a: a = 1 + 2j + 3 * 4j Imag ** gets the imaginary part. Abs () finds the modulus of the complex number (√(a^2 +b ^2)).
Numeric type conversion :(optional in Python documentation, enclosed by square brackets [])
function | role |
---|---|
int(x[,base]) | Converts x to an integer with the second argument specifying the base type of the preceding string |
float(x) | Convert x to a floating point number |
complex(real [,imag]) | Create a complex number |
str(x) | Convert object X to a string |
repr(x) | Convert object X to an expression string |
eval(str) | Evaluates a valid Python expression in a string and returns an object |
tuple(s) | Converts the sequence S to a tuple |
list(s) | Convert the sequence S to a list |
chr(x) | Converts an integer to a character |
unichr(x) | Converts an integer to a Unicode character |
ord(x) | Converts a character to its integer value |
hex(x) | Converts an integer to a hexadecimal string |
oct(x) | Converts an integer to an octal string |
bin(x) | Converts an integer to a binary string |
Mathematical functions:
function | role |
---|---|
abs(x) | Returns the absolute value of a number, such as ABS (-10) returns 10 |
ceil(x) | Returns the uppermost integer of a number, such as math.ceil(4.1) returns 5 |
cmp(x, y) | Returns -1 if x < y, 0 if x == y, and 1 if x > y |
exp(x) | Returns e to the power x (ex), as math.exp(1) returns 2.718281828459045 |
fabs(x) | Returns the absolute value of a number, such as math.fabs(-10) returns 10.0 |
floor(x) | Returns the rounded integer of a number, such as math.floor(4.9) returns 4 |
log(x) | For example,math.log(math.e) returns 1.0 and math.log(100,10) returns 2.0 |
log10(x) | Returns the logarithm of x in base 10, as math.log10(100) returns 2.0 |
max(x1, x2,…) | Returns the maximum value of the given argument, which can be a sequence. |
min(x1, x2,…) | Returns the minimum value of the given argument, which can be a sequence. |
modf(x) | Return the integer and decimal parts of x with the same numeric sign as x, The integer part is represented as a floating point. |
pow(x, y) | X to the y |
round(x [,n]) | Returns the rounded value of the floating point number x, if n is given, rounded to The number of digits after the decimal point. |
sqrt(x) | Returns the square root of the number x, which can be negative, and is of type real, Such as math.sqrt(4) returns 2+0j |
3) Boolean type (Bool)
Use **True and False** to indicate True or False, can also be treated as integers, True is 1, False is 0, but not recommended to participate in operations!
4) List (List)
Similar to array, ordered, variable length of content, using brackets [], elements separated by commas, elements of the data type can be different! Use the following example (dir(list) to view all attributes and methods j) : (Tip: lists can be nested, if you want to access a value in a list, you can write multiple [], for example: list1[1][2])
list1 = [1.2.0."a".True] # define list
print(list1[1]) # Access elements by [subscript], starting at 0, resulting in output: 2.0
print(list1[1:3]) # support interception, such as here [1:3], result output: [2.0, 'a']
print(list1[1:3:2]) # can also take a third parameter, step size, default is 1, result output :[2.0]
print(list1[2:)['a', True]
print(list1[-2]) If the value is negative, start at the back and output: a
print(list1.index("a")) # return the position of the argument in the list, resulting in output: 2
# Change the list element
list1[1] = 3.0 [1, 3.0, 'a', True]
# add element
list1.append('Jay') # append a list element: [1, 2.0, 'a', True, 'Jay']
list1.insert(2.'pig') # insert a list element: [1, 2.0, 'pig', 'a', True]
print(list1.pop()) # remove the last element and return the value of the element. The result is True
# delete element
del list1[0] [2.0, 'a', True]
list1.remove('a') [1, 2.0, True]
# others (use the + sign to combine lists, and the * sign to repeat lists)
print(list1+list1) [1, 2.0, 'a', True, 1, 2.0, 'a', True]
print(list1*2) [1, 2.0, 'a', True, 1, 2.0, 'a', True]
print('a' in list1) # check if this element is present in the list, and print True
print('b' not in list1) # Check if this element is not present in the original, and print True
for x in list: Iterate over the list
print(len(list1)) Get the list length, result output: 4
print(list1.count(1)) # count the number of times an element appears in the list, resulting in 2, since True is also 1
max(list1) Get the maximum number of elements in the list
min(list1) Get the minimum value of the element in the list. The list element type should be a number
list1.sort() # Sort the original list element, local sort (modify the value), return None,
# Can only compare numbers! Default from small to large, from large to small can be optional arguments, parentheses are added:
# key = lambda x:-1*x
list1.reverse() # Invert the list element, which modifies the list and returns None
list2 = list1.copy() # copy list, re-open the memory space! It's not like assigning an equal sign!
list(tuple) # Convert a tuple or string to a list
Copy the code
5) tuple (a tuple)
A restricted list, in which elements in a tuple cannot be modified, is represented by parentheses (). One thing to note: when there is only one element in a tuple, you need to add a comma after the element, otherwise it will be used as the parenthesis operator! Tuples can be passed to functions as arguments that cannot be modified, and they take up less memory. When used, except that there is no way to modify a tuple element, the list method is basically the same.
In addition, elements in a tuple cannot be deleted, but you can use the del statement to delete the entire tuple, though less frequently, because Python’s recycling mechanism automatically deletes the tuple when it is no longer in use (similar to Java’s GC ~). You can also use **tuple(list)** to convert strings or lists to tuples.
The output:
Dict (6) dictionary)
Unlike lists, where tuples index elements by subscript sequences, dictionaries store data as key-value pairs and index values by keys. When creating dictionaries, keys cannot be duplicated. Because keys must be immutable, they can be numbers, strings, or tuples, but not lists! Use ** colons: split keys and values, multiple key-value pairs are separated by commas,**; Dictionaries also support nesting! Examples of usage are as follows:
dict1 = {} Define an empty dictionary
dict2 = {'a': 1.'b': 2.3: "c"} Define a normal dictionary
dict4 = dict2.copy() # shallow copy a dictionary
You can use fromKeys to create and return a new dictionary with two arguments, the key and the corresponding value of the key
The # value may not be provided and defaults to None, but there is a small detail to note in the following example output
# the result is: {1: [' a ', 'b', 'c'], 2: [' a ', 'b', 'c'], 3: [' a ', 'b', 'c']}
list1 = [1.2.3]
list2 = ['a'.'b'.'c']
dict3 = {}
dict3 = dict3.fromkeys(list1, list2)
print(dict3)
TypeError will be reported if there is no such key
print(dict2['b'])
print(dict2.get("d")) Get (); return None
You can also add a default return parameter get(x,y)
print(dict2.setdefault("d")) # Like get(), the key is automatically added if no key is found :None
print("d" in dict2) If this key is present in the dictionary, False is printed
print("d" not in dict2) If there is no such key in the dictionary, print True
print(dict2.keys()) Dict_keys (['a', 3, 'b']) dict_keys(['a', 3, 'b'])
print(dict2.values()) Dict_values ([1, 'c', 2]) dict_values([1, 'c', 2])
print(dict2.items()) # return all key-value pairs in the dictionary.
# dict_items([('a', 1), (3, 'c'), ('b', 2)])
{3: 'c', 'a': 1, 'b': 'HaHa'}
dict2['b'] = 'HaHa'
dict2.update(b:'Pig') Update key (string);
Delete the dictionary element
del(dict2['b']) {3: 'c', 'a': 1}
dict2.clear() # remove all key-value pairs from the dictionary, leaving an empty dictionary: {}
del dict2 # delete the entire dictionary, delete can not be repeated reference!!
print(dict2.pop('a')) Delete the value by key.
print(dict2.popitem()) # delete an item at random, e.g. :('b', 2)
# pass through the dictionary:
for d in dict2:
print("%s:%s" % (d, dict2.get(d)))
for (k,v) in dict2.items():
print("%s:%s" % (k, v))
Copy the code
7) set (set)
The elements stored in the collection are unordered and not repeated, so you can’t index a specific element; Wrap elements with curly braces {}, separated by commas. Duplicate elements are automatically removed! Set (); set(); set(); set(); Examples of usage are as follows:
set1 = set(a)Create an empty collection
set2 = {1.2.3.4.5.1} Create a collection the normal way
print(set2) {1, 2, 3, 4, 5}
set3 = set('12345') # string
print(set3) # output: {'2', '5', '3', '4', '1'}, set element unordered
print(6 in set2) # check if this element is present in the set: False
print(6 not in set2) # check if this element is present in the set: True
set2.add("6") # add element
print(set2) {1, 2, 3, 4, 5, '6'}
set2.remove(2) # delete element, error if deleted element does not exist
print(set2) {1, 3, 4, 5, '6'}
1, 3, 4, 5, 6
for data in set2:
print(data, end="\t")
# Define immutable sets using the frozenset() function
set4 = frozenset({1.2.3.4.5})
Copy the code
8) string
Handling strings is routine in Python, so it’s important to be familiar with string handling. You can use ** single quotation marks (“”) or double quotation marks (“”) to decorate a string. If you want a string to contain newline indentation, you can use three parentheses (”””)** to decorate a string. This is usually used when printing paragraph text. If str1[0] = ‘x’, it is possible to create a string that contains the same string as the original one. If str1[0] = ‘x’, it is possible to create a string that contains the same string as the original one.
Access string:
Str1 = "Hello Python" print(str1[3]) llo print(str1[2:5]) Print (str1[0:8] + str1[8:]) llo print(str1[2:10:2]) Hello Python str2 = str1[6:] * 3 print(str2) #Copy the code
Escape character:
Escape character | role | Escape character | role | Escape character | role |
---|---|---|---|---|---|
The end of each line\ |
Line continuation operator | \ \ |
The backslash | \ ' |
Single quotes |
\a |
Ring the bell | \b |
backspace | \e |
escape |
\ 000 |
empty | \n |
A newline | \v |
Vertical TAB character |
\t |
Horizontal tabs | \r |
enter | \f |
Change the page |
\o |
Octal numbers represent characters | \x |
Hexadecimal numbers represent characters |
Various built-in methods:
The method name | role |
---|---|
capitalize(a) | Put the stringThe first characterInstead ofA capital |
casefold(a) | Put the whole stringAll charactersInstead oflowercase |
center(width) | Center the string and fill it with Spaces to a new string of length width |
count(sub[,start[,end]]) | Return the number of times sub appears in a character, The start and end parameters indicate ranges and are optional |
encode(encoding= ‘utf-8 ‘,errors=’strict’) | Encodes the string in the encoding format specified in encoding |
endswith(sub[,start[,end]]) | Check if the string ends with a substring sub, if True, Otherwise return False. The start and end parameters indicate ranges and are optional |
expandtabs([tabsize= 8]) | Converts the TAB (\t) character in a string to a space. If no argument is specified, The default number of Spaces is tabsize=8 |
find(sub[,start[,end]]) | Check if sub is contained in the string, and return the index if so, Otherwise -1 is returned. The start and end parameters indicate the range and are optional |
index(sub[,start[,end]]) | Same as find, except that an exception is raised if sub is not in string |
isalnum(a) | If there is at least one character in the string and all characters are Returns True for letters or numbers, False otherwise |
isalpha(a) | If the string has at least one string and all characters are Returns True for letters, False otherwise |
isdecimal(a) | Returns True if the string contains only decimal digits, False otherwise |
isdigit(a) | If the stringContains only fewWord returns True, otherwise False |
islower(a) | If the string is at leastContains a case sensitive characterAnd these characters All lowercase, True, otherwise False |
isnumeric(a) | If in a stringContains only numeric characters, returns True, otherwise returns False |
isspace(a) | Returns True if the string contains only Spaces, False otherwise |
istitle(a) | If the string is captioned (all words start in uppercase and the rest in lowercase), Returns True, False otherwise |
isupper(a) | If the string contains at least one case sensitive character, and these Return True if all characters are uppercase, False otherwise |
join(sub) | Insert string as delimiter between all characters in sub, using + to concatenate a large number of characters Strings are inefficient because plus concatenation causes a first-level garbage collection of memory assignment Join ([‘Hello’,’Python’]) |
ljust(width) | Returns a left-aligned string filled with Spaces to a new string of length width |
lower(a) | Converts all uppercase characters in a string to lowercase |
lstrip(a) | Removes all Spaces to the left of the string |
partition(sub) | Find the substring sub and split the string into 3 tuples (pre, pre, and post) Returns (‘ original string ‘,”,”) if not contained in the string |
replace(old, new[,count]) | Replace old substring in string with new, if count is specified, The number of substitutions does not exceed count |
rfind(sub[,start[,end]]) | This is similar to the find() method, except that it starts from the right |
rindex(sub[,start[,end]]) | This is similar to the index() method, but starts from the right |
rjust(width) | Returns a right-aligned string filled with Spaces to a new string of length width |
rpartition(sub) | Similar to partition(), but starting from the right |
rstrip(a) | Remove the space at the end of the string |
split(sep=None,maxsplit=-1) | Defaults to whitespace delimited slicing string if maxspli parameter t If set on the right, only maxsplit substrings are separated and a list of spliced substrings is returned |
splitlines([keepends]) | Separated by ‘\n’, returns a list of rows as elements, if keepends If specified, the previous Keepends row is returned |
startswith(prefix[,start[,end]]) | Checks if the string begins with prefix, returning True if it does, and False otherwise. The start and end parameters can specify a range check, which is optional |
strip([chars]) | Delete all Spaces before and after the string. The chars parameter is optional |
swapcase(a) | Invert case in a string |
title(a) | Returns a captioned string (all words start in uppercase and the rest of the letters are lowercase) |
translate(table) | Convert characters in strings according to the rules of table (which can be customized by str.maketrans(‘a’,’b’)) |
upper(a) | Converts all lowercase characters in a string to uppercase |
zfill(width) | Returns a string of length width, right-aligned and preceded by 0 |
String formatting:
The format method is used as an example:
# position parameter
str1 = "{0} generates {1}, {2}{3}!".format("People"."Short"."I use"."Python")
print(str1) # Output: Life is short, I use Python!
# Keyword argument
str1 = "{a} generates {c}, {b}{d}!".format(a = "People", c = "Short",b = "I use",d = "Python")
print(str1) # Output: Life is short, I use Python!
The position argument can be used with the keyword argument, but the position argument must be before the keyword argument. Otherwise, an error will be reported.
There is also something called the substitution field, where the colon represents the beginning of the formatting symbol, as in the following example:
str1 = "{0}:{1:.4}".format(PI.3.1415926)
print(str1) PI :3.142
Copy the code
Format operator: %; print() : %;
9) operator
Arithmetic operator :(+ – * / % **(power, power) //(floor division, discard decimal))
print("3 + 7 = %d" % (3 + 7)) 3 + 7 = 10
print("3 - 7 = %d" % (3 - 7)) 3-7 = -4
print("3 * 7 = %d" % (3 * 7)) 3 * 7 = 21
print("7 / 3 = %f" % (7 / 3)) 7/3 = 2.333333
print("7 %% 3 = %d" % (7 % 3)) # 7%3 = 1
print("3 ** 6 = %d" % (7 ** 3)) 3 ** 6 = 343
print("3 // 6 = %f" % (7 // 3)) 3 // 6 = 2.000000
Copy the code
Comparison operator :(==! = > < >= <=)
Assignment operator :(== += -= *= /= %= **= //=)
Operator: (& (bitwise and) | (bitwise or) ^ (exclusive or, different is 1) ~ (against) < < > >)
Logical operators :(and or not)
Member operator :(in not in)
Identity operator (to determine whether to refer to the same object) :(is is not)
Operator priority:
* * (index) > ~ + – (invert, plus or minus) % > * / / / (battles, yu, floor except) > < < > > (move) around > & (bitwise and) > ^ | (exclusive or, Bitwise or) > < <= > >=(comparison operator) > equal operator > Assignment operator > Identity operator > member operator > logical operator
10) Date and time
Datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime: datetime
import time, datetime
Get the current time
moment = time.localtime()
print("Years: % s" % moment[0])
print("Month: % s" % moment[1])
print("Day: % s" % moment[2])
print(: "% s" % moment[3])
print("Points: % s" % moment[4])
print("Second: % s" % (moment[5] + 1))
print("Week: %s" % (moment[6] + 1))
print("Day of the year: %s" % moment[7])
print("Daylight saving time: %s" % moment[8])
Strftime is not the same as Strptime.
moment1 = time.strftime('%Y-%m-%d %H:%M:%S')
moment2 = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
moment3 = time.mktime(time.strptime(moment2, '%a %b %d %H:%M:%S %Y'))
print(moment1) # 2017-12-02 11:08:02
print(moment2) Sat Dec 02 11:08:02 2017
print(moment3) 1512184082.0 date converted to timestamp
Get the current timestamp
print(time.time()) # Output: 1512185208.0942981
Get the current time (time array, strftime format)
print(datetime.datetime.now()) 2017-12-02 11:34:44.726843
# timestamp is converted to time
# method 1: (output result: 2017-12-02 11:08:02)
moment4 = 1512184082
moment5 = time.localtime(moment4) # Convert to time array
print(time.strftime('%Y-%m-%d %H:%M:%S', moment5)) # format
# Method 2:
moment6 = datetime.datetime.utcfromtimestamp(moment4)
print(moment6) # Direct output: 2017-12-02 03:08:02
moment7 = moment6.strftime('%a %b %d %H:%M:%S %Y')
print(moment7) Sat Dec 02 03:08:02 2017
# delay executionTime. Sleep (in seconds)Copy the code
6. Process control
1) If
There is no switch-case in python, and we use elif instead of else if. Each condition is followed by a ** colon (:). The following is an example:
The output:
Alternatively, if the condition is true and you don’t want to do anything, you can simply use the **pass null statement **
2) while loop
There is no do-while in Python, again note the colon and zoom! It can be used with **else, and there is such a thing as an infinite loop: while True:** To break out of the loop, use the **break keyword. The following is an example:
The output:
3) the for loop
Unlike C or Java for loops, you can’t just say for(int a = 0; a < 100; A++) is used as follows:
Output result:
4) Break, continue, else
Break c. Continue skips the rest of the operation and goes straight to the next loop; Else can also be used if the loop, for loop condition is not set, if break first will not be executed!
5) Conditional expressions (simplified if-else)
A = x if else y
6) asserts that
AssertionErro When the assert keyword is false, the program crashes and throws an AssertionErro exception
Iterators and generators
Iterator: An object used to access a collection, an object that can remember the position of the traversal, starting with the first element and ending with two basic methods: iter() and next().
The output:
The generator
A special function that uses yield to return a value. When called, it returns a generator object, which is essentially an iterator, but more succintly. The yield value is not immediately returned when the function is called, but only when the next() method is called. The next() method is still called when using for x in XXX. The simplest example is as follows:
The output:
If you use the type() method, the object returned is
. Generators are much cleaner and more elegant than iterators. The classic example is implementing the Fibonacci sequence:
def func(n) :
a, b = 0.1
while n > 0:
n -= 1
yield b
a, b = b, a + b
for i in func(10) :print(i, end="\t")
1 1 2 3 5 8 13 21 34 55
Copy the code
Function of 7.
For some reusable code blocks, we can extract them as a function.
1) Function definition
Use the def keyword, followed by the function name and parentheses (passed as arguments), use the return keyword to return a value, and default to None if not written. Python can dynamically determine the type of a function, return values of different types, and use lists to package multiple types of values at once. You can also use tuples directly to return multiple values; In addition, if there are more than one function parameter, can be separated by ** comma. Another suggestion is to optionally use the docstring in the first line of a function to hold the function description.
2) the form participates in arguments
Functions are defined as parameters and called as arguments.
3) Keyword parameters
For example, **show(a = “a”, b = “b”)**.
4) Default parameters
Def sub(a = “1”, b = “2”), sub(a = “1”, b = “2”), sub(3), sub(3), and keyword arguments can be specified.
5) Variable parameters
Sometimes the number of arguments passed to the function may not be fixed. For example, if you are asked to calculate the sum of a set of values, you can use a variable argument. The argument is mutable if it is preceded by an asterisk (***). If there are more than one parameter, the parameter after the variable parameter must be specified with the keyword parameter **, otherwise the variable parameter will be added to the category!! If you want to pass a list or tuple as a variable parameter, you need to add *** before the argument. In addition, if you want to package parameters as tuples, you can use two asterisks (****) to qualify ~
6) Global and local variables
Global variables are defined as the most external and can be accessed from within a function but cannot be modified directly; A local variable is an argument or variable defined inside a function but not accessible outside the function.
Local variables cannot be accessed externally: Python uses a Stack to store data while running a function, and all data is automatically deleted after the function is executed.
Why you can’t change a global variable in a function: When you try to change the value of a global variable in a function, Python automatically creates a local variable with the same name inside the function instead. If you do, you can use the global keyword inside a function to modify global variables, but this is not recommended because it will increase maintenance costs.
7) Internal functions
In fact, function nesting, a function nested in another function, need to pay attention to one point: the scope of the internal function is only in the internal function of the direct external function, external is not called, if the call will report an error.
8) closure
Closures in Python: An external function is considered a closure if a reference is made to a variable of an external scope (non-global). Here’s a simple example:
The output:
Cannot call an inner function outside of the outer function, reporting undefined method name. An inner function cannot directly modify a variable in an outer function. An UnboundLocalError is reported. As in the case of global variables in the previous function, you can change the variables in the external function indirectly through the container type, or use the **nolocal keyword ** provided in Python3 to modify the changed variables. Examples are as follows:
The output: 400
9) Lambda expressions
In Python, you can use the **lambda keyword ** to create anonymous functions that return a function object without having to define the function. This simplifies code readability. A simple comparison size lambda expression example is as follows:
big = lambda x, y: x > y
print("The first argument is larger than the second: %s" % big(1.2))
The first argument is larger than the second: False
Copy the code
10) recursion
The simplest recursive summation is the function call itself:
Def sum(n): if n == 1: return 1 else: return n + sum(n-1) print(" %d" % sum(100)) print(" %d" % sum(100)Copy the code
8. Exception handling
1) Syntax errors are distinguished from running exceptions
Syntax errors are errors that the compiler can’t pass, such as missing a colon after an if; Running exception is the program running, because the program business logic problems caused by the program crash, such as dividing by 0;
2) Common exceptions in Python
abnormal | Description information |
---|---|
AssertionError |
Assertion statement failure |
AttributeError |
Attempted to access an unknown object property |
IndexError |
The index is out of range of the sequence |
keyError |
Look up a nonexistent Key in the dictionary |
NameError |
Attempting to access a variable that does not exist |
OSError |
Exceptions raised by the operating system, such as FileNotFoundError |
SyntaxError |
Python syntax error |
TypeError |
Invalid operations between different types |
ZeroDivisionError |
Divisor of 0 |
IOError |
Input/output error |
ValueError |
Function parameter type error |
3) Exception capture
Try-expect -else statement, try-finally statement
# 1. Most simply, try catches any exceptions and throws them directly to the except block:
try:
result = 1 / 0
except:
print("Caught an exception!") # output: Exception caught!
# 2. Capture a specific type:
try:
result = 1 / 0
except ZeroDivisionError:
print("Caught a zero divisor error") # output: caught a zero divisor error
# 3. Set multiple except for different exceptions
try:
sum = 1 + '2'
result = 1 / 0
except TypeError as reason:
print("Type error:" + str(reason))
except ZeroDivisionError as reason:
print("Divisor is 0:" + str(reason))
Unsupported operand type(s) for +: 'int' and 'STR'
# 4. Handle multiple exceptions uniformly
try:
result = 1 / 0
sum = 1 + '2'
except (TypeError, ZeroDivisionError) as reason:
print(str(reason)) # output: division by zero
# 5. A block of code that is executed when no exception is detected, else
try:
result = 4 / 2
except ZeroDivisionError as reason:
print(str(reason))
else:
print("No exception, output: %d" % result)
# output: no exception, output result: 2
# 6. A block of code that executes whether or not an exception occurs, such as an IO stream shutdown,
If an exception occurs, except clause is followed by finally clause.
try:
result = 4 / 2
except ZeroDivisionError as reason:
print(str(reason))
else:
print("No exception, output: %d" % result)
finally:
print("Execute ~ regardless of whether an exception occurs.")
# output result:
No exception occurs. Output: 2
# execute ~ regardless of whether an exception occurs
Copy the code
4) Throw an exception
The **raise statement can be used to directly raise exceptions, such as raise TypeError(exception explanation, optional)**
5) Context management-with statement
When your exception catching code is simply to ensure a unique allocation of shared resources (files, data, etc.) and release it at the end of the task, you can use the **with statement **, as shown in the following example:
try:
with open('123.txt'."w") as f:
for line in f:
print(line)
except OSError as reason:
print("An exception occurs:" + str(reason))
An exception has occurred: Not readable
Copy the code
6) sys. Exc_info function
In addition to the above method of obtaining exception information, it can also be obtained from the SYS module exc_info() function:
The output is in order: exception class, class example, trace record object
try:
result = 1 / 0
except:
import sys
tuple_exception = sys.exc_info()
for i in tuple_exception:
print(i)
# output result:
# <class 'ZeroDivisionError'>
# division by zero
# <traceback object at 0x7f3560c05808>
Copy the code
9. File storage
1) Open function and file opening mode
The **open() function opens the file and returns the file object. As you can see from the help command, the open function takes several arguments:
For starters, the first two parameters are enough: file: the name of the file, which will be looked up in the current folder if there is no path. Mode: Enable mode. The options are as follows:
model | role |
---|---|
r |
Open in read-only mode, the default |
w |
Write mode onIf the file exists, delete it first and then create it again |
a |
Append mode openThe file does not exist and will be created automatically |
b |
Binary mode on |
t |
Text mode open, the default |
+ |
Read-write mode, can be used with other modes, such as R +, W + |
x |
Opening the file in this mode raises an exception if it already exists |
U |
General newline support |
2) Method of file object
function | role |
---|---|
close(a) | Close the file. After the file is closed, no further operations can be performed on the file |
read(size=-1) | Reads the specified number of bytes from the file. If unset or negative, reads all |
next(a) | Returns the next line of the file |
readline(a) | Read entire line, including newline ‘\n’ |
seek(offset, from) | Set the position of the current file pointer from(0 file start position, 1 current position, 2 End of the file) offset offset |
tell(a) | Returns the current location of the file |
write(str) | Writes a string to a file |
writelines(seq) | Writes a list of sequence strings. If you want to break a line, you need to add each line’s newline character yourself |
3) Use examples
Try: f1 = open("321.txt", "w") with open("123.txt", "r") as f2: for line in f2: try: f1 = open("321.txt", "w") as f2: for line in f2: Print (line, end="") f1.write(line) except OSError as reason: print(" error "+ STR (reason)) finally: F1.close () # f1.close() #Copy the code
Output result:
4) Common functions about files/directories in OS module
To import OS modules, add a module reference, such as os.getcwd()
function | role |
---|---|
getcwd(a) | Returns the current working directory |
chdir(path) | Change the current working directory |
listdir(path=’.’) | The do not write parameter defaults to listing all files and folders in the current directory, ‘.’ Current directory, ‘.. ‘Upper level of directory |
mkdir(path) | Creates a folder, raising FileExistsError if it exists |
mkdirs(path) | Can be used to create multi-level directories |
remove(path) | Delete a specified file |
rmdir(path) | Delete the directory |
removedirs(path) | Deleting a Multi-level Directory |
rename(old,new) | Rename a file or folder |
system(command) | Call a system-provided gadget, such as a calculator |
walk(top) | Returns one by traversing all subdirectories in the path specified by the top argumentA triple(Path, [include directory], [include file]) |
curdir | Current directory (.) |
pardir | Last program record (..) |
sep | Path separator, ‘\’ in Windows, ‘/’ in Linux |
linesep | The line terminator used by the current platform, ‘\r\n’ for Win and ‘\n’ for Linux |
name | The operating system in use |
Os. path module (file path dependent)
function | role |
---|---|
dirname(path) | Get pathname |
basename(path) | Get file name |
join(path1[,path2[,…]]) | Concatenate the pathname and filename into a full path |
split(path) | Split path and filename, return tuple (f_path, f_name), if use directory entirely, It also separates the last directory as a filename and does not determine whether a file or directory exists |
splitext(path) | Separate file names and extensions |
getsize(file) | Gets the file size, in bytes |
getatime(file) | Get files recentlyAccess time, returns the number of floating point seconds |
getctime(file) | document-obtainingCreation time, returns the number of floating point seconds |
getmtime(file) | document-obtainingModify the time, returns the number of floating point seconds |
exists(path) | Check whether the path (file or directory) exists |
isabs(path) | Check whether the path is determined |
isdir(path) | Check whether a directory exists |
isfile(path) | Check whether it exists and is a file |
islink(path) | Determines whether a symbolic link exists |
ismount(path) | Determines whether a mount point exists |
samefile(path1,path2) | Determine whether two paths point to the same file |
10. Classes and objects
1) The simplest example
PS: If you want to define a private property or function, you can name it with two underscores __. This is a false private property. **_ class name __ private property/method name, such as the following call people._Person__skill**, is to access private members! Properties in a class are static variables.
Output result:
2) __init__(self)
A constructor
It is called automatically when an object is instantiated, and you can use it when you want to pass arguments
Output result:
3) inheritance
Here are the rules:
- Class subclass (parent):
- 2. A subclass can inherit all attributes and methods of its parent class.
- 3. Attributes and methods defined by subclasses with the same name as their parent classes are automatically overwritten;
- 4. When overwriting, you can use ** if you want to call a method of the same name as the parent
Super () function.
** method name call;
Python supports multiple inheritance. Multiple parent classes are separated by commas. Subclasses can inherit attributes and methods from multiple parent classes at the same time. All three parent classes have a show method, so the subclass calls show() in Name! If it is not necessary to use multiple inheritance, you should avoid using it as much as possible, sometimes causing unexpected bugs.
There is also a combination routine, which is to drop the required classes into the combination class to instantiate, and then use, for example, Book, Phone, Wallet in the Bag:
Output result:
4) Some built-in functions related to objects
function | role |
---|---|
issubclass(class, classinfo) | If the first argument is the second argumentA subclass, returns True, otherwise returns False |
isinstance(object, classinfo) | If the first argument is the second argumentInstance objects, returns True, otherwise returns False |
hasattr(object, name) | Test an objectWhether there are specified attributesAttribute names must be quoted! |
getattr(object, name, [,default]) | Returns the specified attribute value of the object. No default value is returned. ArttributeError is not set |
setattr(object, name, value) | Sets the value of the specified property in the object. properties that do not exist will be created and assigned |
delattr(object, name) | Removes the value of the specified attribute from the object. ArttributeError is not reported |
property(fget,fset,fdel,doc) | Returns a property where properties can be set |
11. The module
1) Import modules
Py files are independent modules, such as a.py and b.py files. You can import B from A and then use functions from B.py. Module import rule 4.1 guide package ** is written in detail, here will not repeat the description.
2) Specify the module
When other modules are imported, the test part of the code is also executed, and Python can be told by __name__ whether the module is run as a program or imported into another program. The value of this property is __main__ when run as a program, and is only executed when run separately. Such as:
if __name__ == '__main__':
test()
Copy the code
3) Search path
Python module imports have an ImportError path search. If none of these paths can be found, ImportError is reported. You can see the search paths by printing sys.path, as in mine:
If your module is not in any of these paths, you will get an error. You can also add the path to the search path via sys.path.append(” path “)!
4) Download and install third-party libraries
Method 1: Install Pycharm directly
File -> Default Settings -> Project Interpreter -> Select the current Version of Python to see all third libraries currently installed. Click the + sign to enter the library search. Click Install Package on the search page to find the library you want. Click – to uninstall the library you don’t need.
Method 2: Command line Use PIP command to install
PIP is available in Python3. In the Scripts folder of the Python installation directory, you need to configure environment variables in Win to use PIP:
Add this to Path:
Python3 -m PIP install python3 -m PIP install python3 PIP install --upgrade PIP # PIP uninstall library name # PIP list #Copy the code
5) Let others use your module
When you start to learn crawler, the computer will gradually become more and more. Python3 environment variables are set to Python3. Python3 environment variables are set to Python3 environment variables. Python3 environment variables are set to Python3. You are not interested in learning Py, you can’t make him install PIP commands one by one, can you export a dependent file thing, perform the following automatic installation module? The answer is definitely yes:
Pipreqs: PIP install Pipreqs: PIP install Pipreqs
Directory to which pipreqs are exportedCopy the code
You can export a requirements. TXT file that contains the Py library you need to install for your project. Send this file to your buddy and ask him to execute it on the command line:
pip install -r requirements.txt
Copy the code
After running the library to install the basic:
In addition, if you want to move all the packages in your Python environment to another computer, you can use:
pip freeze > requirements.txt
Copy the code
conclusion
It took two weeks to complete the basic knowledge of Python. Of course, there will be some missing things later. I think I can make up the basic knowledge