1.注释:
#单行注释
'''多行注释'''
2.换行: \
total = applePrice + \
orangePrice +\
milkPrice
3.变量:
#111 赋值给了 a
a=111
- 变量名规则:字母、数字、下划线组成【数字不可以开头、不能关键字重用】
- 变量名必须以字母或下划线开头,但以下划线开头的变量在Python中有特殊含义
- 变量名中不能有空格以及标点符号(括号、引号、逗号、斜线、反斜线、冒号、句号、问号等等)
- 不能使用关键字作变量名,可以导入keyword模块后使用print(keyword.kwlist)查看所有Python关键字
- 变量名对英文字母的大小写敏感,例如student和Student是不同的变量
- 不建议使用系统内置的模块名、类型名或函数名以及已导入的模块名及其成员名作变量名,这将会改变其类型和含义,可以通过dir(builtin)查看所有内置模块、类型和函数
Python中可以使用 id 命令查看变量的地址。
a=123
print(a)
print(id(a))
可以把一个变量a赋值给另一个变量b,这个操作实际上是把变量b指向变量a所指向的数据
a=123
print(a)
print(id(a))
b=a
print(b)
print(id(b))
关键字:
import keyword
len(keyword.kwlist)# 33
print(keyword.kwlist)#查看
dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。
dir()
4.常量
常量就是不能变的变量
PI = 3.14159265359
5.运算符
- 数学运算符
a=21;b=4
c=a+b
if c>5:
print(c)
d=a-b
if d<2:
print(d)
else:
print(100)
e=a*b
print(e)
f=a/b #除法是数学上的除法
print(f)
g=a%b
print(g)
h=a//b #整除
print(h)
#温度转换
f=float(input('请输入温度:'))
c=(f-32)/18
print(c)
- 关系运算符:==, !=, >, <, >=, <=
- 逻辑运算符:and,or,not
- 位运算符:与&,或|,异或^,取反~,左移<<, 右移>>
- [~取反就是如果是00111,则变为11000 (按位取反)]
- 身份运算符用于比较两个对象的内存地址。常用的有两个身份运算符:is 和 is not
m=2;n=3
#m=010 n=011
print(m&n) #2 [相同位的两个数字都为1,则为1;若有一个不为1,则为0。]
print(m|n) #3 [相同位只要一个为1即为1。]
print(m^n) #1 [相同位不同则为1,相同则为0。]
print(m<<2) #8 [若左移一位,则其数值变为 a*2]
print(m>>1) #1 [右移1位即除以2;,并且取整。]
a=20
b=20
print(id(a))
print(id(b))
print(a is b) #True
- 成员运算符用于测试给定值是否为序列中的成员,例如字符串,列表或元组,两个:in 和 not in
'y' in 'python' #True
- 成员函数
count():出现次数
clear():清除列表元素
extend():添加多个元素
append():添加一个元素
index():参数值第一次出现的下标
remove():搜索列表并删除第一个与参数相同的值
6.数据类型
print(123)
2**3 #幂次方 8
print('fgb')
print(1,56)
0.4-0.1 == 0.3 #应尽量避免直接比较两个实数是否相等
abs(0.4-0.1 - 0.3) < 1e-6 #这里1e-6表示10的-6次方
print(1>2)
a=True
print(not(a))
print(12 + 23) #35
print('why '+'not') #拼接字符串 why not
print(3*4+7) #19
a='lucky '
b='day'
print(a*3+b) #重复 lucky lucky lucky day
7.字符串切片
索引方式是包括开头,不包括结尾
使用in操作符判断某个子字符是否在字符串中
使用max()和min()方法获取字符串中编码极值对应的字符
data = '乌云追着人走'
print(data[0]) #乌
print(data[-1])#走
data[0:3]#乌云追
data[::-1] #'走人着追云乌'
len(data) #6
'走' in data #True
'dd' in data #False
max(data) #追
min(data) #乌
使用*操作符对字符串进行重复
(data+' ')*3 #乌云追着人走 乌云追着人走 乌云追着人走
8.数据处理
a=str(1234.54) #'1234.54'
type(a) #str
b=eval(a) #1234.54
type(b)#float
- 使用split()方法将字符串按照指定的字符串分隔,返回数组
"I am a good man".split(',')
#['I', 'am', 'a', 'good', 'man']
- 使用join()方法连接字符串数组为一个完整的字符串
a="I am a good man".split(' ')
'.'.join(a)
#'I.am.a.good.man'
- 使用count()方法获取某个字符在字符串中出现的次数
'vdvsddsfddddfd'.count('d')
- 使用index()方法获取某个字符在字符串中首次出现的位置的索引
data='qwertyuiop'
data.index('y')#5
- 使用strip()方法去除字符串头尾的特定字符,默认去除空格和换行
l=' python \n'
l.strip() #'python'
9.内置函数
- abs() :绝对值
- len()
- max()
- min()
- id() :查看变量的地址
- type() :查看变量的类型
- eval() :执行字符串语句
- replace() :实现字符串替换
- strip() :去除字符串首尾指定信息。
- lstrip():去除字符串左边指定信息。
- rstrip():去除字符串右边指定信息。
- isalnum(): 是否为字母或数字
- isalpha(): 检测字符串是否只由字母组成(含汉字)。
- isdigit() :检测字符串是否只由数字组成。
- isspace() :检测是否为空白符
- isupper() :是否为大写字母
- islower() :是否为小写字母
- swapcase():大小写互换
- title():首字母大写
- replace(‘old’,‘new’):字符串替换相关替换old为new
- replace(‘old’,‘new’,Times):替换指定次数的old为new
"*s*x*t*".strip("*") #'s*x*t'
"*s*x*t*".lstrip("*")#'s*x*t*'
"*s*x*t*".rstrip("*")#'*s*x*t'
" sxt ".strip() #'sxt'
a="why"
a.center(10,"*")#'***why****'
a.center(10)#' why '
a.ljust(10,"*")#'why*******'
a.rjust(10,"*")#'*******why'
10.random
- random.uniform(a,b) 【生成指定范围的随机浮点数,a<=n<=b】
- random.randint(a,b) 【生成指定范围的随机整数,a<=n<=b】
- random.seed() 【利用随机数种子,使得每次生成的随机数相同】
细致描述
import random
random.seed(10) #每次产生种子,使随机数不变
print(random.random())
11.占位符
print("{} {}".format("hello", "world")) # 不指定位置,按默认顺序
print("{0} {1}".format("hello", "world")) # 指定位置
print("{1} {0} {1}".format("hello", "world")) # 指定位置,可以重用
print('{name},{sex},{age}'.format(age=32,sex='男',name='张三')) # 使用关键字参数指定名字
hello world
hello world
world hello world
张三,男,32``
12.填充与对齐
填充常跟对齐一起使用
< (默认)左对齐
> 右对齐
^ 中间对齐
后面带宽度
:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充
print("12345678")
print('{:>8}'.format('abc'))
print('{:<8}'.format('abc'))
print('{:^8}'.format('abc'))
print('{:-^8}'.format('abc'))
13.类型与精度
f表示浮点
.2f 小数点后2位
.0f 取整
e表示科学计数
%表示百分制
,表示金融数字格式
b表示二进制
d表示十进制(默认)
o表示八进制
x表示十六进制
print('{:.2f}'.format(3.14159))
print('{:.0f}'.format(3.9))
print('{:e}'.format(3.14159))
print('{:%}'.format(0.75))
print('{:,}'.format(100000000))
print('{:b}'.format(56))
x='Pi',3.1415926
print('{0}的值是:{1:-^18.7f}'.format(*x)) # 因为x是一个地址,*x = 'Pi',3.1415926
print('{0}的值是:{1:^18.7f}'.format('Pi',3.1415926))
3.14
4
3.141590e+00
75.000000%
100,000,000
111000
Pi的值是:----3.1415926-----
Pi的值是: 3.1415926
sep参数,用于指定分隔符,默认是空格分割 end参数,用于指定行末的字符,默认是换行符,可以改为空格或者空字符,在循环中就可以在同一行里继续输出了
print(1,2,3,4)
print(1,2,3,4,sep='\t')
for i in range(5):
print(i,end=' ')
print("Learn python in %s"%('mooc'))