[Speed] to learn python Python grocery store basis

"Life is short, I learned python" programming session quotes. Write a small script in python convenience, so many learners of other languages ​​as the python auxiliary language. Have knowledge of a particular language, come to learn another language should be very fast. Programming concepts are very similar, just look at the diversity of languages. With Java foundation, to learn Python, ready to hit the road.

Based computing

After installing python environment, run win + R, enter directly into the python python's interactive page, here you can perform mathematical operations.

>>> 2+2
4

Table 1 mathematical operators, highest to lowest priority
| Operator | OS | Examples | Value |
|: -----: |: ----: |: ----: |: ---- : |
| ** | index | ** 2. 3 |. 8 |
|% | modulo / take the remainder | 22 is. 8% |. 6 |
| // | divisible / quotient rounding | 22 is //. 8 | 2 |
| / | division | 22 is /. 8 | 2.75 |
| * | multiplication |. 3. 5 * | 15 |
| - | subtractor |. 5 - 2 |. 3 |
| + | addition | 2 + 2 | 4 |

As can be seen from the above two basic types of the first python IntegerFloating-point type. These two concepts are similar and java.

String

As types of foundations, of course, the character string, and as java string splicing operation can be performed. E.g:

>>> 'AAA' + 'BBB'
'AAABBB'
>>> 'CCC'+25
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> 'DEF'*3
'DEFDEFDEF'

As, 'AAA' + 'BBB' string may be spliced directly. However, the difference is the java python not the type conversion force , so when the string 'CCC' + int type 25 when the error is reported. * Python to carry out the operation, the string will be copied.

variable name

Insert here a while, the declaration of variables in java when you need to specify the variable type, do not need in python, python variable name but also need some rules.

  1. Only one word.
  2. Only contain letters, numbers and underscores.
  3. You can not start with a number.

The type of conversion

To make int type 5 can be converted into a string type, and so string concatenation, Python provides some built-in functions.
Table 2 number of built-in functions python
| function | effect | exemplary |
|: -----: |: ----: |: ----: |
| STR () | conversion type String | str (5 ) |
| int () | conversion type are integers | int ( '. 5') |
| a float () | converted to floating point type | a float ( '. 5') |
| Print () | Print function | slightly |
| INPUT ( ) | read user input data | myName = iNPUT () |
| len () | content-length | len (myName) |

Control Flow

Boolean value

Now the introduction of a fourth data types, Boolean value. Boolean value python is also a true or false, but in real python is True (note that T must be capitalized), false is False (F must be uppercase), the same Boolean type can not be compared to other types.

>>> True
True
>>> true
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> True = 1
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

Comparison Type

Table 3 Comparison Type
| Operator | Meaning |
|: -----: |: ----: |
| == | is equal to |
! | = | Is not equal to |
| <| smaller than |
|> | is greater than |
| <= | less |
|> = | greater than or equal |

Meanwhile python binary operator may also be used andornotIn the same java, OR, NOT.

Conditions and code block

You see in front of a Boolean expression can be seen as a condition, it is one thing and expression. "Conditions" only in
a more specific name of the context control flow statements. Condition always evaluates to a Boolean value, True or False. Control
flow statements in accordance with the conditions is True or False, to decide what to do. Almost all the control flow statements are the conditions of use.

Block

No java python {} to distinguish the kind of code blocks, the code blocks all of the python are made by indentation.

  1. Retracted increases, code block starts.
  2. Code block may contain other code blocks.
  3. Indent reduced to zero, or reduce surrounding the outer code block is retracted, the code block ends.
>>> if 'a'=='b':
...     print('真')
... else:
...     print('假')
...
假

The above is a control flow if else, followed by a colon conditions, and the code block needs indentation.

while loop has the following format:

spam = 0
while spam < 5:
print('Hello, world.')
spam = spam + 1

In addition python also can use the break and continue keywords.

for loop

Another single loop for children out to show respect, for the cycle following format:

for keywords;
a variable name;
in keyword;
call range () method, passing up to three parameters;
colon;
from the beginning of the next, the degenerate code block (referred for clause).

for i in range(5):
    print('Jimmy Five Times (' + str(i) + ')')

i 0,1,2,3,4 performed separately.

range()The method in fact has three parameters, only two of which can be omitted. range (start, end, gap), you can specify the start, end and step.

>>> for i in range(0,10,3):
...     print(i)
...
0
3
6
9

function

Import module

Python can call a basic set of functions, which is called "built-in functions," including you ever seen print (), input () and len () function. Python also includes a set of modules, referred to as "standard library." Each module is a Python program that contains a set of related functions can be embedded in your program. For example, mathematical functions Math module has associated Random function module associated random number, and the like.
useimportorfrom importStatement

Custom Functions

In addition to built-in functions, most functions are customizable. (Even if the function module is imported from others self-defined, let you use)

def hello(name):
 print('Hello ' + name)

def function name (parameter):
    function body

None

None has a value referred to in Python, which represents no value. None is the unique value NoneType data types (in other programming languages ​​may call this value is null, nil or undefined). Like Boolean True and False, like, None must capitalize the first letter N.

Exception Handling

def temp_convert(var):
    try:
        return int(var)
    except ValueError, Argument:
        print "参数没有包含数字\n", Argument

Similar to java, python capture a treatment with a try, except, except that in addition to with the exception classes (such as ValueError example), you can also keep spark a parameter in the back except (such as the example of Argument, in fact, try var)

List

The list can be viewed as a collection of java in the array can store a number of elements, such as>>> spam = ['cat', 'bat', 'rat', 'elephant']. We can get through an element index, such as spam [0] = 'cat', and can take the data from the back, such as spam [-1] = 'elephant'.

slice

In addition to direct operation index, to obtain an element, you can also get some sub-list by slice.

>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']

Stitching and copy of the list

The difference is that with java, python list can be spliced ​​copy of freedom.

>>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']

Combined with certain keywords and a list of methods

  1. To delete an element of a list, you can be directly used del keywords.

  2. It can also be combined for cycle
>>> supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
>>> for i in range(len(supplies)):
print('Index ' + str(i) + ' in supplies is: ' + supplies[i])
  1. Use in, not in direct determine whether an element in the list
>>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
True
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> 'cat' in spam
False
  1. index () method for determining whether a first occurrence of the element in the index list, if not on the error.
>>> spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']
>>> spam.index('Pooka')
1
  1. A method of adding append () and insert () value in the list
    append () method call, add parameters to the end of the list. insert () method can be inserted in a list of an arbitrary scale values at the lower. The first parameter insert () method is a new index value, the second parameter is the new value to be inserted.

  2. () Method removes value from the list with the remove
  3. With sort () method of the sorted list of values

Tuple

In java, we know that String is modified final, so the String itself can not be changed, if String to modify, in fact, created a new String in memory. Similar python also has a non-modifiable data type - tuple. Tuples and lists almost, many methods are common. But, tuple with "()", and the elements within a tuple can not be changed, can not be increased deletion of tuples.

Tuples and lists of transformation

Function list () and tuple () will return to the lists and tuples pass version of their values.

>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']

And references cited function

>>> spam = [0, 1, 2, 3, 4, 5]
>>> cheese = spam
>>> cheese[1] = 'Hello!'
>>> spam
[0, 'Hello!', 2, 3, 4, 5]
>>> cheese
[0, 'Hello!', 2, 3, 4, 5]

See the above code, very easy to understand, spam will copy the reference to cheese, instead of replicated objects, so the reference to an operation that will change the unique object. If you want to generate a new object reference to cheese, you need to use the copy function copy () and deepcopy (). In fact, a shallow copy and deep copy of java.

>>> import copy
>>> spam = ['A', 'B', 'C', 'D']
>>> cheese = copy.copy(spam)
>>> cheese[1] = 42
>>> spam
['A', 'B', 'C', 'D']
>>> cheese
['A', 42, 'C', 'D']

DeepCopy () refers to a deep copy, not the same as generating a new object spam, spam is also connected with the internal data copy out of a bunch of new objects. Like saying that the 'A', 'B', respectively, into a sub-element of this list, shallow copy does not copy them, while deep copy these objects will be copied.

dictionary

Like lists, "dictionary" is a collection of values. But unlike the list of index, dictionary index can use many different data types, not just integers. Dictionary of the index is known as the "key", the key and its associated value called "key - value" pairs. It can be said that the dictionary java in the Map.

 myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}

The list is different, there is no concept dictionary order can not be acquired by the operation of an element index, the slice can not be used, can not use the sort function.

Operating dictionary

Basic Operations

Therefore dictionary operation, need to open a new path, using the function keys (), values ​​() and items (). As the name suggests, are the key values ​​they get a dictionary, val values ​​are, and who the key for itself.

>>> spam = {'color': 'red', 'age': 42}
>>> for v in spam.values():
        print(v)
red
42
>>> for k in spam.keys():
        print(k)
color
age
>>> for i in spam.items():
        print(i)
('color', 'red')
('age', 42)

It is worth mentioning that spam.keys () is a tuple obtained, if desired, to the corresponding list with list conversion function need.

Whether there is a key or value check dictionary

Can be used in or not in the keywords to determine whether the data in the dictionary.

>>> spam = {'name': 'Zophie', 'age': 7}
>>> 'name' in spam.keys()
True
>>> 'Zophie' in spam.values()
True
>>> 'color' in spam.keys()
False
>>> 'color' not in spam.keys()
True
>>> 'color' in spam
False

get () and setdefault () method

get () can find the value corresponding to the value by key value. get two parameters, the first parameter is a key value, the second parameter is the default value when the key-value pair does not exist (or key does not exist), and administered.

>>> picnicItems = {'apples': 5, 'cups': 2}
>>> 'I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.'
'I am bringing 2 cups.'
>>> 'I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.'
'I am bringing 0 eggs.'

setDefault () has two parameters, a first key to be inserted to the key value, the second value is the default. When the original dictionary had this key value, do nothing, if not in the dictionary the key original value, it will default value as value, makes up the key-value pairs inserted into the dictionary with the same key.

>>> spam = {'name': 'Pooka', 'age': 5}
>>> spam.setdefault('color', 'black')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
>>> spam.setdefault('color', 'white')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}

String Manipulation

We already know that string and its basic operation, and when we learned lists, tuples, dictionaries after then look richer approach to the string.

Transfer characters and original characters

And, like java, python is used to escape special characters. There are also original character r. R can be added before the start of the quoted string, making it the original string. "Original string" completely ignores all the escape character, print out all back slashes string.

>>> print(r'That is Carol\'s cat.')
That is Carol\'s cat.

Notes and multi-line character

Single-line comments python can use special characters #, and multi-line comments use "". "

# 这是一行注释
"""
这是多行注释
这是多行注释
这是多行注释
"""

Sometimes when we use the print function to print a string of very, very long, in order to look good page, you can use '' 'will be split into several large string line.

print('''Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob''')

String sections and subscripts

A string list can also be used as treatment, in which the element can be seen as a character. It may be a subscript or slice a string. The same can be use in, not in determining whether a character within a string.

>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[4]
'o'
>>> spam[-1]
'!'
>>> spam[0:5]
'Hello'
>>> spam[:5]
'Hello'
>>> spam[6:]
'world!'

Some string built-in methods

  1. upper () String uppercase all modifications
  2. All lower () String to lowercase
  3. isupper () has at least one letter string, and all the letters are capitalized
  4. islower () has at least one letter string, and all the letters are in lowercase
  5. the isalpha () string contains only letters, and the non-empty
  6. isalnum () string contains only letters and numbers, and is non-empty
  7. isdecimal () string contains only numeric characters, and non-empty
  8. isspace becomes () string contains only spaces, tabs and line feed, and non-empty
  9. startsWith () method of the string starts with the string passed
  10. endsWith () method of the end of the string to the string passed
  11. join () string concatenation, you can specify the delimiter
  12. split () by cutting a string value
  13. rjust () left to fill, if the output data is insufficient, supplementary spaces
  14. ljust () right padding, if the output data is insufficient, supplementary spaces
  15. center () string centered around lack of data, supplementary spaces

End

Alas, python syntax even if the foundation over again, of course, this is only the basis of this foundation. Like how python multi-threading concepts like memory management and so there is time to look at it. Beginning of the heart do not forget, most start learning python just treat it as a secondary language, writing a small script or something, do not intend to study too, after all, time is limited, java there are going to study a lot of things, and, in addition to programming, but also the world there are many good things waiting for me to experience. Apply their knowledge, I also learn to write a reptile python's power, wrote a small script crawling IMDb TOP250. (As if this is a classic case of reptile entry, ha ha) Code stickers

import requests as rq
from bs4 import BeautifulSoup
import pandas as pd

def get_data(url):
    print(url)
    html = rq.get(url).content.decode("utf-8")
    soup = BeautifulSoup(html,"lxml")
    divItems = soup.find_all("div","item")

    for item in divItems:
        name = item.find('span',{'class':'title'}).text
        rating_num = item.find('span',{'class':'rating_num'}).text
        number = item.find('em').text
        imgUrl = item.find("img").attrs['src']
        print("----------->")
        print("number:"+number)
        numbers.append(number)

        print("name:"+name)
        names.append(name)

        print("rating_num:"+rating_num)
        rating_nums.append(rating_num)

        print("imgUrl:"+imgUrl)
        imgUrls.append(imgUrl)
        print("----------->")


data_df = pd.DataFrame()
numbers = []
names = []
rating_nums = []
imgUrls = []

for num in range(0,10):
    get_data("https://movie.douban.com/top250?start={}&filter=".format(int(num)*25))

data_df["序列"] = numbers
data_df["名称"] = names
data_df["评分"] = rating_nums
data_df["海报"] = imgUrls
writer = pd.ExcelWriter('movie_top250.xlsx')
data_df.to_excel(writer, 'page_1', float_format='%.5f')  # float_format 控制精度
writer.save()

Of course, the need to install modules in advance
Requests
BeautifulSoup4
PANDAS
openpyxl
lxml (the parser BeautifulSoup4)

End.

Guess you like

Origin www.cnblogs.com/pjjlt/p/11615722.html