程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Introduction to Python crawler (II)

編輯:Python

Hello everyone , I'm cabbage . Because of the needs of the project , Studying recently Python The reptiles of . This one is about Python The basic knowledge of , It is also an introductory knowledge point for learning reptiles ! If you get something after reading the article , You can support bloggers three times in a row ~, Hee hee .


List of articles

  • One 、 Preface
  • Two 、 Type conversion
    • 1、 Convert to integer
    • 2、 Convert to floating point
    • 3、 Convert to string
    • 4、 Convert to Boolean
  • 3、 ... and 、 Operator
    • 1、 Arithmetic operator
    • 2、 Assignment operator
    • 3、 Compound assignment operator
    • 4、 Comparison operator
    • 5、 Logical operators

One 、 Preface

  • Last article 《Python Basic learning of reptile ( One )》 highlighted Python Data type of , This article mainly explains the mutual conversion of data types 、 The use of five operators , Dry cargo is full. ~

Two 、 Type conversion

  • stay Python in , The conversion of a type requires the use of functions , Here we mainly introduce four functions
function explain int(x) take x Convert to an integer float(x) take x Convert to a floating point number str(x) take x Convert to a string bool(x) take x To a Boolean value

1、 Convert to integer

  • Using functions int()
# Convert to an integer 
print(int("123")) # 123
print(int(123.78)) # 123
print(int(False)) # 0
print(int(True)) # 1

It should be noted that : The conversion will fail in the following two cases

2、 Convert to floating point

  • Using functions float()
print(float("123.78")) # 123.78
print(float(123)) # 123.0
print(float(True)) # 1.0
print(float(False)) # 0.0

3、 Convert to string

  • Using functions str()
print(str(1))
print(type(str(1)))
print(str(1.0))
print(type(str(1.0)))
print(str(False))
print(type(str(False)))
print(str(True))
print(type(str(True)))

Running results :

4、 Convert to Boolean

  • Using functions bool()
# The following operation results are all False
print(bool(0.0)) # Floating point value is 0
print(bool(''))
print(bool("")) # An empty string 
print(bool(0)) # Integer value is 0
print(bool({
})) # The dictionary is empty 
print(bool([])) # The list is empty. 
print(bool(())) # Tuple is empty 

Summary : Dictionary type 、 A tuple type 、 When the list type and string are empty ( That is, there is no data ), Convert to boolean type is False; Integer and floating point numbers Values for 0 when , Converting to boolean type is also False; Everything else is True

3、 ... and 、 Operator

Let's first look at the operators we need to learn :

1、 Arithmetic operator

  • Arithmetic operators have a total of 8 Kind of :

Look at their functions through the code :

a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
print((a + b) * 5) # 65
print(10 + 1.1) # 11.1
print((5 + 1.5) * 2) # 13.0
# Add strings 
message1 = "hello,"
message2 = "world"
print(message1 + message2)
# Add numbers and strings , Direct error :TypeError: can only concatenate str (not "int") to str
# print(message1 + a)
# String multiplied by number , How many times will this string be repeated 
print(message1 * b)

It should be noted that :

  • stay Python in , Operator / differ Java、C Programming language , Its operation is a division operation rather than an integral division operation !
  • Numbers and strings cannot be added
  • String multiplied by number , The string will be repeated

2、 Assignment operator

  • The assignment operator is just a kind of , We've been using

There are two main methods of assignment :

# Single assignment 
a = 10
print(a) # 10
# Multiple assignments 
b = c = 5
print(b) # 5
print(c) # 5
d, e, f = 1, 2, 3
print(d) # 1
print(e) # 2
print(f) # 3

3、 Compound assignment operator

  • Compound assignment operators have a total of 7 Kind of :

The use of compound assignment operators is also very simple , Similar to arithmetic operators :

a = 5
a += 1
print(a) # 6 Equivalent to a = a + 1
b = 5
b -= 1
print(b) # 4
c = 5
c *= 2
print(c) # 10
d = 5
d /= 2
print(d) # 2.5
e = 5
e //= 2
print(e) # 2
f = 5
f %= 2
print(f) # 1
g = 5
g **= 2
print(g) # 25

4、 Comparison operator

  • The comparison operators have 6 Kind of :

Python The use of comparison operators in is the same as in other programming languages :

print(2 == 3) # False
print(2 != 3) # True
print(2 > 3) # False
print(2 >= 3) # False
print(2 < 3) # True
print(2 <= 3) # True

5、 Logical operators

  • Logical operators have 3 Kind of :

    Before using logical operators , We need to understand their function :

The code for :

print(True and False)
print(False or True)
print(not False)
a = 34
a > 10 and print('hello world')
a < 10 and print('hello world')
a > 10 or print(' Hello world ')
a < 10 or print(' Hello world ')

Are you right about the running results ?


Thank you for reading , Progress together , Hee hee ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved