Start from getting started, learn python (3)

This article is used to record, python process control and python read and write data

One: Process control

1.1 Introduction to python process control

1.1.1 Sequence structure

Complete each step in order.
Code example:

a=5
b=a+1
c=b/2
print(a,b,c)
5,6,3.0
1.1.2 Branch structure

if statement: Decide whether to execute a certain statement by judging whether the condition is established.
If: It’s not raining today, do
n’t wear rain boots

if-else statement: When the original if judgment condition is not established, perform another operation
if: it will not rain today, do
n’t wear rain boots
else:
wear rain boots

if-elif-else statement: Deal with judgments in multiple situations
if: It’s raining today,
don’t play football
elif: It’s too hot today
don’t play football
elif: Today’s condition is not good,
don’t play football
else:
go to play football

代码实例:要求输出a,b,c的最大者
  a=8
  b=9
  c=6
  if a>b and a>c
       print(a)
  elif b>a and b>c
       print(b)
   else:
       print(c)    
 输出结果:9
代码实例:要求输出a,b,c的最大者,if嵌套
 a=6
 b=8
 c=5
 if a>b
      if a>c
         print(a)
      else:
         print(c)
 elif b>c
      print(b)
 else:
      print(c)    
输出结果:8
1.1.3 Loop structure

while loop
While loop, the format is as follows:
while boolean expression: the
program segment
execution logic is as follows: as long as the boolean expression is true, the program segment will be executed. After the execution is completed, the boolean expression is calculated again, if the result is still true, then again Execute the program segment until the Boolean expression is false

代码实例:计算从110的和
count=1
sum=0
while count<11
	sum=sum+count
	print(sum,count)
	count=count+1
输出结果:
11
32
63
104
155
216
287
368
459
5510

for loop
The for loop and the range() function are used together with the
range() function:
range (start value, end value, step size)

代码实例:计算从110的和
sum=0
for i in range(1,11,1);
	sum=sum+i
	print(sum)
输出结果:
11
32
63
104
155
216
287
368
459
5510

Two: read and write data

2.1 Native read and write

The benefits of native read and write: no memory

2.1.1 Open file

f=open(file,mode='r',encoding)
file is the file path, mode is the file opening method, encoding is the encoding format
Commonly used mode parameters
'r' is read-only, the default
value'w' opens the file by writing, it will overwrite the original
file'x', create a new file and open it for writing. If the file already exists, an error'a
' will be reported to write Open the file in the way, when the write operation is performed, the written content will be appended to the original file

代码实例:
一:读取整个文件,字符串提示
f.read():一次读一行,指针在该行末尾
f.readline():取整个文件,以列表显示
f.readlines():用于移除字符串头尾指定的字符,默认为空格或换行符,该方法只能删除开头或者结尾的字符,不能删除中间部分的自费
strip()
print( "1abcdef2")
输出结果:1abcdef2
print( "1abcdef2".strip())
输出结果:abcdef
代码实例:读取txt文件
f=open("E:/学习资料/test1.txt","r",encoding="utf-8")
date=[]
for line in f.readlins();
	line=line.strip().split('\t')
	print(line)
	date.append(line)
f.close

2.2 pandas file read and write

Pandas has an important data structure: data frames, data frames and Excel tables have similar advantages, each column represents a variable, and each row represents a record

代码实例:读取txt文件
import pandas as pd
data1=pd.read_table("E:/学习资料/test1.txt",sep='\t',header=None,encoding="utf-8",names=['col1','col2'])

2.3 Connect to mysql library

2.3.1 Read and write mysql library

One: Create a connection object
host mysql server
port number type port
user username
passwd password
db database name
charset connection encoding, you need to specify the encoding method explicitly
Two: Get the cursor
execute() Execute a database query and command
fetchone() Get the next row of the result set
fetchmany() Get the size row of the result set
fetchall() Get all rows of the result set
close() Close the cursor

代码实例
import pymysql
import pandas as pd
创建一个连接对象,打开数据库连接
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='lh123',db='mysql',charset='utf-8')
sql="""SELECT *
from mysql.test1"""
cursor=execute(sal)
df1=pd.DateFrame(list(date1),columns=["id","col1","col2"])

Guess you like

Origin blog.csdn.net/HONGTester/article/details/108731905