Python notes (manual top)

We continue to update this article

Input and processing

1
2
3
4
5
INPUT = A ( "the Hello World:" )				 
B = int (INPUT ()) #b to: int, a single line is read into a single integer
B = a float (INPUT ()) #b to: a float, floating point single single-line read
a, b Map = (int, INPUT () Split ().) # A, B: int, a single line is read two integers
A List = (. Map (int, INPUT () Split ())) #a is: from 0 to n- -1, single read variable integer, from zero into an array

Output and processing

Note: The following are using the escape syntax%

Basic Operations

1
print("%s %d %f"%(a,b,c))

The line of output string, integer, floating point, separated by spaces

Format parameters

d, i signed decimal integer
octal unsigned o
u unsigned decimal
x unsigned hexadecimal (lowercase)
X-Hex (uppercase) unsigned
e scientific notation floating point (lowercase)
a floating point number in scientific notation E (upper case)
f, F. decimal floating
g if the index precision value is greater than or less than -4 and e is the same, otherwise the same as f
g if the index is greater than -4 precision value or less than the same, and E, F same as the case of the other
C single character (accepted single-character string or integer)
R & lt string (python using repr convert any object)
S string (str using python convert any object)

Complete Parameter

1
print(objects,sep,end,file,flush)
  1. sep
    inserted between the output string string specified, the default is a space

Example:

1
print("a","b",sep="%")
  1. end
    in the end of the output string plus specified, the default is a newline (n-), if the write cancel wrap '

Example:

1
print("a",end="%")
  1. file
    to enter text into the file-like object, it may be a file, a data flow, etc., default sys.stdout

Example:

1
2
fi=open('zht.out','w')
print("a",file=fi)
  1. flush
    flush True or False, the default is Flase, it will immediately indicate whether the output statement to the object input parameter file pointed to

Example:

1
2
3
fi=open('zht.out','w')
print("a",file=fi)
print("a",file=fi,flush=True)

Library file

Call the method

1
2
3
4
5
6
7
Import Math					 # direct call
Print (Math.ceil ( 2.5 ))
from Math Import ceil # accurate call
Print (ceil ( 2.5 ))
from Math Import ceil AS the CE
Print (the CE ( 2.5 )) # rename function
from Math Import *

Common database and table functions and usage

To be filled pit

Design structure

Sequence Structure

slightly

Select structure

Boolean value

Flase, None, 0, the empty sequence, an empty string, an empty dictionary is false, others are true

The if statement

Example:

1
2
3
4
5
6
a=1
b=2
if a>b and b==2:
print(1)
elif 1<=a<=1:
print(1)

NOTE: elif the else may be replaced, deleted latter condition, if may be nested between

Operator:

and,or,not,==,>,<,!=,is,is not,in,not in

Example:

1
2
3
4
= A . 1
B = . 1
IF A IS B:
Print ( . 1 ) # A, B refer to the same object
1
2
3
4
= a [ 0 , . 1 , 2 ] 
B = 2
IF B in a:
Print ( . 1 ) #b to in a the

Loop structure

for loop

Example:

1
2
3
4
5
A = List (Range ( 0 , . 5 )) 
for I in Range ( 0 , 10 ): the rear # is iterables
print ( "! TQL" ) # 0 to 9, the cycle
for I in A: the rear # a list
print ( "TQL!" ) # 0 to 4 cycles

while loop

Example:

1
2
3
4
5
6
7
8
i=1
while 1:
print("tql!")
i+=1
if i>=10:
break
else:
continue #0~10循环

data structure

String

Containing special symbols using the escape

Example:

1
print(""Hello World!"")

Strings are immutable objects can only access the positive and negative sequence, positive sequence 0 n--. 1, -1 reverse order -n

Slice visit:

1
2
3
4
a="abcd"
print(a[0:2])
print(a[:2])
print(a[0:])

General operation

1. Connect

1
2
3
a="abc"
b="cde"
print(a+b)

2.重复

1
2
a="abc"
print(3*a)

3.监测

1
2
a="abc"
print("a" in a)

4.取长度

1
2
a="abc"
print(len(a))

5.字符编码

强制使用中文:

1
2
#-*- coding:UTF-8 -*-
#coding=utf-8

例:

1
2
3
a=input()
print(eval(a)) #将字符串作为数值表达式
print(eval(str(1+2))) #将数值转换为字符串

List

定义

1
2
3
a=[]
b=[NONE]*100
c=[0 for i in range(0,100)]

特点

1.不固定长度,可以删减
2.成员类型不限制
3.可以嵌套列表

注:与vector类似

用法

1.访问

类比字符串,可以正序反序访问,取子列表

例:

1
2
3
4
a=[1,2,"a"]
print(a[0])
print(a[-1])
print(a[1:])

2.运算

支持加法(拼接)和乘法(重复)操作

例:

1
2
3
4
5
6
a=[1,2]
b=[3]
c=a+b
d=a*3
print(c)
print(d)

3.常用函数

1
2
3
4
5
6
7
a=[1,2]
a.append(3) #添加元素在末尾
a.insert(1,4) #在指定位置插入元素
print(a.pop()) #弹出元素,并从列表中删除(可以指定位置)
a.remove(x) #删除元素x(第一个匹配的)
a.sort() #排序,默认从小到大(只能排int)
print(a)

tuple

和list基本相同,不能增删改成员

字典

注:类似于C++中map,但是在没有建立映射关系时不可访问所对应键值!

可以嵌套

例:

1
2
3
4
5
6
a={"a":1,"b":2,"c":3}
a["a"]=3 #支持修改操作
del(a["b"]) #删除对应键值对
a["b"]=1 #增加新的键值对
print(a["a"]) #输出键值
print(a) #输出字典(按照键值添加的顺序)

模块化编程

函数

官方定义:完成特定功能的语句组

实际:对重复次数过多的某个功能进行的封装,以及为了程序主提简洁性将一些功能外放

基本操作

例:

1
2
3
4
def (name):
for i in range(1,10):
print("orz %s!"%name)
orz("Somebody")

传进去的参数为局部变量,在函数内部可以任意修改,赋值,对全局无影响,用return返回函数计算出的值

可以跨文件调用

例:

1
2
import circle
print(circle.aera(1))

命名规则

必须以下划线和字母开头,函数名区分大小写

Python提供的函数为内键函数,自己定义的函数不可以和内键函数重名,也不可以是保留字(如list,int)。

例:

1
2
3
4
5
6
7
p=1
def cul(x):
global p
p+=1
return x**p
print(cul(2))
print(p)

递归调用

1
2
3
4
5
6
def cul(x):
if (x==1):
return x
else:
return x*cul(x-1)
print(cul(10)) #计算10!

递归最大深度:2955层

模块

保存在外存储器的文件称为一个模块,可以被调用

import 模块名 进行调用

原文:大专栏  Python笔记(手动置顶)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11607000.html