Python学习记录——数据类型和基本表达式
图书推荐:《Python风格指南》
一.变量命名规则
1.字母数字下划线组成
2.不能以数字开头,不能含有特殊字符和空格
3.不能以保留字命名
4.不能以中文命名
5.定义的变量名应该有意义
6.驼峰式命名、下划线分割单词
7.变量名区分大小写
二.条件表达式
1.if <表达式> :
elif <表达式> :
else :
2.while <表达式> :
3.for i in range(循环次数):
4.break跳出当前循环,如果有两层,只能跳出内层
5.continue结束本次循环,开始下一次循环的条件判断
6.条件表达式末尾要加“:”,while、if、else、elif、for都是如此,但break和continue不用
三.注释
1.# 单行注释
2.'''多行注释''' (三个引号赋值给变量,可按引号内内容原样打印,多用于打印多行,如msg='''内容''',print(msg))
3.""" 多行注释 """
4.注释快捷键Ctrl+/
四.逻辑运算
and or not 遵循短路原则
五.字符格式化输出(占位符的使用)
%s s = string
%d d = digit 整数
%f f = float 浮点数,约等于小数
六.列表(可更改),元组(不可更改)
1.列表用[],元组用()
2.查:
(1)索引(下标) ,都是从0开始 a[2]
(2)切片 a[起始:结束(不包含该位置元素):步长]
(3)count 查某个元素的出现次数
(4).index 根据内容找其对应的位置
3.增:
(1)a.append() 追加
(2)a.insert(index, "内容")
(3)a.extend 扩展
4.改:
(1)a[index] = "新值"
(2)a[start:end] = [a,b,c]
5.删:
(1)remove("内容")
(2).pop(index) 有返回值
(3)del a a已经不存在 del a[index] 删一个元素
(4)a.clear() 清空,但是a还在
6.排序:
sort ()
reverse()
.sort(reverse=Ture) 从大到小排列
.sort() 从小到大排列
七.字典
1.字典用{},无序,键唯一
2.格式:dic={‘键’:‘值’,‘键’:‘值’}
3.查:
通过键查找
4.增:
.setdefault(‘键’,‘值’) 键存在,不改动,返回字典中相应的键对应的值;键不存在,在字典中中增加新的键值对,并返回相应的值
dic['键']=值
5.改:
old.update(new) 如果old中有new的键,更新,否则创建新的键并补充值
dic['键']=新值
6.删:
(1).clear() 清空字典
(2)del dic['键'] 删除字典中指定键值对
(3).pop('键') 删除字典中指定键值对,并返回该键值对的值
(4)del dic 删除整个字典
7.初始化:
dic.fromkeys(['host1','host2','host3'],'test')
8.遍历:
for i in dic:
print(i,dic[i])
八.字符串
1.重复输出字符串
print('内容' * 重复次数)
2. [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表
print('helloworld'[2:])
3.关键字 in 返回True或者False
print(123 in [23,45,123])
print('el' in 'hello')
4.格式字符串
print('zhou is a good teacher')
print('%s is a good teacher'%'zhou')
5.字符串拼接
a='123'
b='abc'
d='44'
c=a+b+d
c= '分隔内容'.join([a,b,d])
6.关于转义
要想显示字符串'he's good',应写为'he\'s good'
要想显示\n而不让其变为换行符,则写为repr(\n)
7.字符串内置函数
(1)print(st.count('元素')) # 统计元素个数
(2)print(st.capitalize()) # 字符串首字母大写
(3)print(st.center(总字符数,'填充符号')) # 居中
(4)print(st.endswith('字符串')) # 判断是否以某个内容结尾
(5)print(st.startswith('字符串')) # 判断是否以某个内容开头,在文件处理中经常使用
(6)print(st.expandtabs(tabsize=个数)) #自定义TAB代表空格数
(7)print(st.find('字符串')) # 查找到第一个元素,并将索引值返回,找不到返回-1
(8)print(st.format(name='zhou',age=20)) # 格式化输出的另一种方式,字符串中要替换的内容在{}里面
(9)print(st.format_map({'name':'zhou','age':20})) #和上面一种方法的区别是参数是一个字典
(10)print(st.index('t')) #同find一样,但是找不到报错
(11)print('asd'.isalnum()) #判断是不是字母或字符串
(12)print('12632178'.isdecimal()) #判断是不是十进制
(13)print('1269999.uuuu'.isnumeric()) #判断是不是整数
(14)print('abc'.isidentifier()) #是不是变量名
(15)print('Abc'.islower()) #是不是小写
(16)print('ABC'.isupper()) #是不是大写
(17)print(' e'.isspace()) #是不是空格
(18)print('My Title'.istitle()) #是不是每个单词首字母大写
(19)print('My title'.lower()) #所有大写变小写
(20)print('My title'.upper()) #所有小写变大写
(21)print('My title'.swapcase()) #大写变小写,小写变大写
(22)print('My title'.ljust(字符串总数,'填充符号')) #左对齐
(23)print('My title'.rjust(字符串总数,'填充符号')) #右对齐
(24)print('\tMy title\n'.strip()) #去掉空格和换行符
(25)print('\tMy title\n'.lstrip()) #去掉左边空格和换行符
(26)print('\tMy title\n'.rstrip()) #去掉右边空格和换行符
(27)print('ok')
(28)print('My title title'.replace('被替换字符串','替换内容',替换次数)) #替换
(29)print('My title title'.rfind('查找内容')) #从右往左找字符串
(30)print('My title title'.split('分割符',分割次数)) #分割
(31)print('My title title'.title()) #改为标题格式

浙公网安备 33010602011771号