We continue to update this article
Input and processing
1 |
INPUT = A ( "the Hello World:" ) |
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) |
- sep
inserted between the output string string specified, the default is a space
Example:
1 |
print("a","b",sep="%") |
- 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="%") |
- file
to enter text into the file-like object, it may be a file, a data flow, etc., default sys.stdout
Example:
1 |
fi=open('zht.out','w') |
- 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 |
fi=open('zht.out','w') |
Library file
Call the method
1 |
Import Math # direct call |
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 |
a=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 |
= A . 1 |
1 |
= a [ 0 , . 1 , 2 ] |
Loop structure
for loop
Example:
1 |
A = List (Range ( 0 , . 5 )) |
while loop
Example:
1 |
i=1 |
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 |
a="abcd" |
General operation
1. Connect
1 |
a="abc" |
2.重复
1 |
a="abc" |
3.监测
1 |
a="abc" |
4.取长度
1 |
a="abc" |
5.字符编码
强制使用中文:
1 |
#-*- coding:UTF-8 -*- |
例:
1 |
a=input() |
List
定义
1 |
a=[] |
特点
1.不固定长度,可以删减
2.成员类型不限制
3.可以嵌套列表
注:与vector类似
用法
1.访问
类比字符串,可以正序反序访问,取子列表
例:
1 |
a=[1,2,"a"] |
2.运算
支持加法(拼接)和乘法(重复)操作
例:
1 |
a=[1,2] |
3.常用函数
1 |
a=[1,2] |
tuple
和list基本相同,不能增删改成员
字典
注:类似于C++中map,但是在没有建立映射关系时不可访问所对应键值!
可以嵌套
例:
1 |
a={"a":1,"b":2,"c":3} |
模块化编程
函数
官方定义:完成特定功能的语句组
实际:对重复次数过多的某个功能进行的封装,以及为了程序主提简洁性将一些功能外放
基本操作
例:
1 |
def (name): |
传进去的参数为局部变量,在函数内部可以任意修改,赋值,对全局无影响,用return返回函数计算出的值
可以跨文件调用
例:
1 |
import circle |
命名规则
必须以下划线和字母开头,函数名区分大小写
Python提供的函数为内键函数,自己定义的函数不可以和内键函数重名,也不可以是保留字(如list,int)。
例:
1 |
p=1 |
递归调用
1 |
def cul(x): |
递归最大深度:2955层
模块
保存在外存储器的文件称为一个模块,可以被调用
import 模块名 进行调用