python 基础:各种数据类型的用途:
1.字符串:
1)大小写转换:
例:
pharse='he is very beautiful'
print(pharse.upper())
other='DJFSDF '
print(other.lower())
以上代码并没有将变量永久改为大/小写模式,若想永久改变:
pharse='he is very beautiful'.upper()
other='DJFSDF'.lower()
2)删除字符串末尾的空格:
例:
pharse=‘I like study python '
pharse=pharse.strip()
print(pharse)
3)分割字符串
例:
str="Teacher fu [ is my teacher] he is a good teacher.
print(str.split("[")[1].split("]")[0])
#以上代码中用 ”[" 和 “]” 将字符串分为三段,split("[")[1]中的[1]访问的是中括号中的内容,若为“0”,则访问的是“Teacher fu",若为”2“则返回错误,原因是超出范围,后面的也是同理。
2.数值类型:加减乘除等其他数学运算
3.列表:
1)在列表中增加或删除元素:
例:
增加:
list=[]
list.append('dog')
list.append('cat')
list.append('panada')
list.append('dargon')
list.append('tiger')
print(list)
删除:
第一种:
list.remove('tiger')
print(list)
第二种:
list.remove(1)
print(list)
第三种:
del list(1)
print(list)
第四种:
list.pop(1)
print(list)
3)列表重新排列:
例:
反向排列(不按大小)
mylist=[1,8,9,4,5,6]
mylist.reverse()
print(mylist)
正向排列
扫描二维码关注公众号,回复:
5040292 查看本文章
mylist=[1,2,5,9,4,7]
mylist.sort()
print(mylist)
保存原列表的正向排列:
mylist=[1,5,7,8,6]
mylist.sorted()
print(mylist)
pharse('interesting')#也可对字符类型排列
pharse.sorted()
print(pharse)
4.字典:
1)增加/修改一个键值对
dict = {'Name': 'Mr.Ren', 'Age': 19, 'Class': 'third'}
dict['Age'] = 18 # 更新
dict['School'] = "RUNOOB" # 添加
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
2)利用键查找值
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
五、python中有用的工具:
1)type
type可以确定对象属于哪种类型,实现方法:type(对象)
例:type(100) 返回值为:int类型
2)dir
dir会返回一个内置方法与属性的列表,列出特定数据类型能做的所有事情,深入了解每一种python数据类型的内置方法
例:
print(dir('hos,dsds,dsad'))
#返回值为:['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
使用其中的split方法:
'hos,dsds,dsad'.split(',')
#返回值为:['hos','dsds','dsad']
3)help
这一方法返回对象、方法或模块的文档
例:
animals='cat,dog,horse'
help(animals.split)
#返回值为:Return a list of the words in S, using sep as the
# delimiter string. If maxsplit is given, at most maxsplit
#splits are done. If sep is not specified or is None, any
#whitespace string is a separator and empty strings are
#removed from the result.
#百度翻译结果:返回s中的单词列表,使用sep作为
#分隔符字符串。如果给定maxsplit,则最多为maxsplit
#分割完成。如果未指定SEP或SEP为“无”,则任何
#白字符串是分隔符,空字符串是
#从结果中删除。