数字: int 整数 作用:年龄、等级、薪资、身份证、QQ号 float 浮点型 作用:带小数
*****以后看到self当到没看见 ---------------------------------------------------
字符串: 作用:名字,性别,国籍,地址等描述信息
定义:在引号、双引号、三引号内,由一串字符组成
name=‘elroy‘ 只能存一个值
常用操作:
移除空白strip:
name=input("username:")
print(name.strip())
效果: username: eeee eeee
----------------------------------
切分split(切成一个列表list):
user_info=‘meng/xing/long/elroy‘ print(user_info.split("/"))
效果: [‘meng‘, ‘xing‘, ‘long‘, ‘elroy‘]
----------------------------------
长度len: name=‘elroy‘ print(len(name))
效果: 5
----------------------------------
索引
----------------------------------
切片(指定范围时含头不含尾):
name=‘elroyy‘
print(name[1:4])
效果: lro
========================================
字符串其他方法:
name=‘elroy‘
------------------------------
判断以什么开头startswith:
print(name.startswith(‘el‘))
效果:True
------------------------------
判断以什么结尾endswith:
print(name.endswith(‘roy‘))
效果:True
------------------------------
字符串替换replace:
name=‘elroy say :i have you one tesla,my name is elroy‘
print(name.replace(‘elroy‘,‘heiheihei‘))
效果: heiheihei say :i have you one tesla,my name is heiheihei
----------------------------------------------------------
字符串增加format:
三种: 1.print("{}{}{}".format(‘elroy‘,‘20‘,‘IT‘))
效果:elroy20IT 2.print("{0}{0}{1}".format(‘elroy‘,‘20‘,‘IT‘))
效果:elroyelroy20 *3.print("{name}{age}{sex}".format(name=‘elroy‘,sex=‘20‘,age=‘IT‘))
效果:elroyIT20
--------------------------------------------------------------------------------------
判断输入是否为数字isdigit:
age=‘123123‘
print(age.isdigit())
效果: True
------------------------------------
大写转小写:
name=‘Elroy‘ print(name.lower())
效果: elroy
----------------------------
低优先级的字符串处理:
name=‘elroy123‘
判断是否由字符串和数字组成isalnum:
print(name.isalnum())
效果:True
--------------------------------
判断是否由字符串组成isalpha:
name=‘elroy‘
print(name.isalpha())
效果:True
================================================
数字判断:
num=b‘3‘ #Bytes num1=u‘3‘ #unicode
python3中默认是unicode num2=‘四‘ #中文数字 num3=‘IV‘ #罗马数字
------------------------------------------------
bytes,unicode能被isdigit判断 unicode、中文数字,罗马数字能被isnumberic判断
======================================================
列表(可以是任意数据类型)字符串和列表是有序的:
常用操作: 索引 切片
------------------------------------------------
*追加append:
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] my_friends.append(‘heiheihei‘) print(my_friends)
效果: [‘elroy‘, ‘meng‘, ‘xing‘, ‘long‘, ‘heiheihei‘ -----------------------------------------------
*追加insert:
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] my_friends.insert(2,‘heiheihei‘) print(my_friends)
效果: [‘elroy‘, ‘meng‘, ‘heiheihei‘, ‘xing‘, ‘long‘]
-----------------------------------------------
*删除(按照下标删除)pop:
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] my_friends.append(‘heiheihei‘) my_friends.pop(0)
print(my_friends)
效果: [‘meng‘, ‘xing‘, ‘long‘, ‘heiheihei‘]
---------------------------------------
*删除(按照字符串删除)remove:
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] print(my_friends) my_friends.remove(‘meng‘)
print(my_friends)
效果: [‘elroy‘, ‘meng‘, ‘xing‘, ‘long‘] [‘elroy‘, ‘xing‘, ‘long‘]
-----------------------------------------
*长度len: my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘]
print(len(my_friends))
效果: 4
-----------------------------------------
循环
包含in
其他操作(掌握)清空clear:
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] my_friends.clear()
print(my_friends) 效果: []
复制copy: my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] my_friends.clear()
print(my_friends) l=my_friends.copy() print(l)
效果: [] []
------------------------------------------
统计相同字符串count:
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘] print(my_friends.count(‘long‘))
效果: 0
------------------------------------------
全部添加extend:
my_friends.extend([‘meng‘,‘xing‘]) print(my_friends)
效果: [‘meng‘, ‘xing‘]
-------------------------------------------
倒序reverse:
my_friends.reverse() print(my_friends)
效果: [‘long‘, ‘xing‘, ‘meng‘, ‘elroy‘]
-------------------------------------------
my_friends=[‘elroy‘,‘meng‘,‘xing‘,‘long‘]
print(my_friends.index("elroy"))
效果: 0 ---------------------------------------------
元组(不可变的列表一般用作只读):indes看下标位置,count查看相同个数 age=(11,22,33,44,55) 查看下标index: print(age.index(33)) 2 查看有几个33count: print(age.count(33)) 1
字典的Key必须是不可变类型,也称为可hash类型,无序
--------------------------------------------
*从字典里抬一个值出来pop:
info={‘name‘:‘elroy‘,‘age‘:18,"sex":"ss"}
print(info.pop(‘name‘))
效果: elroy
--------------------------------------------
*从字典查看一个V,get,get与普通取值的区别在于没有值会返回None:
print(info.get(‘name‘))
效果: elroy
--------------------------------------------
无序弹K/V popitem:
info={‘name‘:‘elroy‘,‘age‘:18,"sex":"ss"}
print(info.popitem())
效果: (‘sex‘, ‘ss‘)
--------------------------------------------
弹所有K和V keys、values info={‘name‘:‘elroy‘,‘age‘:18,"sex":"ss"}
print(info.keys()) print(info.values())
效果: dict_keys([‘name‘, ‘age‘, ‘sex‘]) dict_values([‘elroy‘, 18, ‘ss‘])
-------------------------------------------
info={‘name‘:‘elroy‘,‘age‘:18,"sex":"ss"} for key,value in info.items():
print(key,value) 效果: name elroy age 18 sex ss
--------------------------------------------
清空clear:
info={‘name‘:‘elroy‘,‘age‘:18,"sex":"ss"} info.clear()
print(info)
效果: {}
---------------------------------------------
info={‘name‘:‘elroy‘,‘age‘:18,"sex":"ss"} dic=info.fromkeys([‘name‘,‘age‘,‘sex‘],22222)
print(dic)
效果: {‘name‘: 22222, ‘age‘: 22222, ‘sex‘: 22222}
-----------------------------------------------
goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] goods[0].setdefault(‘AA‘,[]).append(‘11‘) print(goods) 效果: [{‘name‘: ‘电脑‘, ‘price‘: 1999, ‘AA‘: [‘11‘]}, {‘name‘: ‘鼠标‘, ‘price‘: 10}, {‘name‘: ‘游艇‘, ‘price‘: 20}, {‘name‘: ‘美女‘, ‘price‘: 998}]
集合: 集合去重和做关系运算 集合无序 作用:去重、关系运算(不可变类型)(集合内可以有多个元素,但是每个元素必须是不可变类型,即不可Hash)
-----------------------------------------
去重:集合内的元素唯一 s={1,‘a‘,1,1,1,1,1,1}
print(s) 效果: {1, ‘a‘}
--------------------------------------------
关系运算: s1={1,2,3,4} s2={3,4,5,6} 交集: print(s1 & s2) {3,4} 并集: print(s1 | s2) 差集: print (s1 - s2) print (s2 - s1) 对称差集: print(s1 ^ s2)
---------------------------
不重要的: s1={1,2,3,4} s2={1,2} 父集: print(s1 >= s2) 子集: print(s1 <= s2)