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

Basic learning of Python crawler Introduction (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 .


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")) # 123print(int(123.78)) # 123print(int(False)) # 0print(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.78print(float(123)) # 123.0print(float(True)) # 1.0print(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 Falseprint(bool(0.0)) # Floating point value is 0print(bool(''))print(bool("")) # An empty string print(bool(0)) # Integer value is 0print(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 = 10b = 3print(a + b) # 13print(a - b) # 7print(a * b) # 30print(a / b) # 3.3333333333333335print(a // b) # 3print(a % b) # 1print(a ** b) # 1000print((a + b) * 5) # 65print(10 + 1.1) # 11.1print((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 = 10print(a) # 10# Multiple assignments b = c = 5print(b) # 5print(c) # 5d, e, f = 1, 2, 3print(d) # 1print(e) # 2print(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 = 5a += 1print(a) # 6 Equivalent to a = a + 1b = 5b -= 1print(b) # 4c = 5c *= 2print(c) # 10d = 5d /= 2print(d) # 2.5e = 5e //= 2print(e) # 2f = 5f %= 2print(f) # 1g = 5g **= 2print(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) # Falseprint(2 != 3) # Trueprint(2 > 3) # Falseprint(2 >= 3) # Falseprint(2 < 3) # Trueprint(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 = 34a > 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 ~
author : I'm a cabbage

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .


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