python基础2

  • 格式化输出

  使用%占位符,str类型用%s,int类型用%d

  eg:

1 info = """
2     name:%s
3     age:%d
4     hobby:%s
5 """
6 name = input("请输入名字:").strip()
7 age = int(input("请输入年龄:"))
8 hobby = input("请输入爱好:").strip()
9 print(info%(name,age,hobby))

  在格式化输出当中需要输出%,需要使用%%

print("我的名字叫%s,我的学习进度为3%%"%("lihao"))
  • 编码

  人们最早使用ASCII码的编码方式,1个字节用8个比特来表示,ASCII码所包含的信息由数字、大小写字母和特殊字符组成,ASCII设计之初只有7位,发明者预留了1位以保证后序扩展。Unicode也叫万国码,最早使用1个字节来表示所有英文、特殊字符和数字,一个汉字使用2字节来表示,也就是16位,但光是中文汉字就有九万多字,于是发明出了用四字节表示中文的的32位Unicode,前128个字符依然沿用ASCII码序列,但其占的资源太多。UTF-8是Unicode的升级版,用3字节表示中文,它的编码方式是英文(数字、符号)占8位、欧洲文字占16位、亚洲文字占24位。

  • 字符串类型操作str

  索引:s = “HelloWorld”,元素的索引从0开始,取出第一个元素s[0],还有一种方法是从后往前数,最后一个元素的索引值为-1,从后往前依次减少。

  切片:s[切片起始位置(包含),切片结束位置(不包含),移动步数]

  s[:] -> “HelloWorld”  s[0:5]->"Hello"  s[5:0:-1]->"Wolle"  s[::-1]->"dlroWolleH"  s[0:5:2]->"Hlo"

  常用方法:

 1 s.capitalize() #返回值首字母大写,其余字母全变为小写
 2 s.title()   #s = "ale3x lN2ame" 返回值"Ale3X Ln2Ame"
 3 s.upper()   #返回值字母全大写
 4 s.lower()
 5 s.strip()  #缺省去除首尾的空格,参数为可迭代对象,去除首尾含有的参数中的元素
 6 s.split("l")    #将字符串按参数分离开并返回一个包含字符串的列表,缺省参数为空格,可添加一个int类型参数表明拆分次数
 7 s.center(20)    #传入int类型参数,返回长度为参数的字符串,原字符串居中表示,可添加一个str类型长度为1的参数来控制填充物,缺省为空格
 8 s.startswith("al") #返回布尔值,判断是否以参数开头
 9 s.endswith("me")
#以下三个函数都可添加起始位置和结束位置的索引值来划定范围
10 s.find("l")     #查找字符串中是否含有此元素,有则返回参数索引,没有则返回-1
11 s.index("l")    #同查找,有返索引值,无则报错
12 s.count("l")    #计算字符串中参数的个数
13 s.isalpha()
14 s.isdigit()
15 s.isalnum()
16 s.swapcase() #大小字母反转
17 s.replace('l','hel',1) #用参数2替换参数1,参数3为替换次数,缺省则全部替换
  • 列表list

  l = ['hello','world','你好','世界']

  增:

>>> l.append('!')
>>> l
['hello', 'world', '你好', '世界', '!']
>>> l.insert(0,'eric')
>>> l
['eric', 'hello', 'world', '你好', '世界', '!']
>>> s = 'asdl'
>>> l.extend(s)
>>> l
['eric', 'hello', 'world', '你好', '世界', '!', 'a', 's', 'd', 'l']
>>> l2 = ['Lin','Jin','Wang']
>>> l.extend(l2)
>>> l
['eric', 'hello', 'world', '你好', '世界', '!', 'a', 's', 'd', 'l', 'Lin', 'Jin', 'Wang']

  改:

>>> l[5] = 23333
>>> l
['eric', 'hello', 'world', '你好', '世界', 23333, 'a', 's', 'd', 'l', 'Lin', 'Jin', 'Wang']
>>> l[5:10] = [1,2]
>>> l
['eric', 'hello', 'world', '你好', '世界', 1, 2, 'Lin', 'Jin', 'Wang']
>>> 

  删:

>>> s = l.pop(0)
>>> print(s,l)
eric ['hello', 'world', '你好', '世界', 1, 2, 'Lin', 'Jin', 'Wang']
>>> l.remove(1)
>>> l
['hello', 'world', '你好', '世界', 2, 'Lin', 'Jin', 'Wang']
>>> del l[4:6]
>>> l
['hello', 'world', '你好', '世界', 'Jin', 'Wang']
>>> 

  查:

>>> num = l.index('你好')
>>> num
2

通用方法:

>>> len(l)
6
>>> l.count("Jin")
1
>>> for i in l:
    print(i)

hello
world
你好
世界
Jin
Wang
>>> for i in range(0,10,2):
    print(i)

0
2
4
6
8
  • 元祖tuple

  不可修改,可哈希

  count和index

  • 字典

  不可修改数据类型:int、bool、str、tuple 可哈希

  可修改数据类型:list、dict 不可哈希

dic = {'name':'Lin',23:['H',True],False:(5,7),('weight','81'):75}

  增:

>>> dic['gender']='male' #没有键值则添加,有则覆盖
>>> dic
{'name': 'Lin', 23: ['H', True], False: (5, 7), ('weight', '81'): 75, 'gender': 'male'}
>>> dic.setdefault('hobby','eat') #返回值为值value
'eat'
>>> dic
{'name': 'Lin', 23: ['H', True], False: (5, 7), ('weight', '81'): 75, 'gender': 'male', 'hobby': 'eat'}
>>> dic.setdefault('name','He')#没有key则添加,有不修改原value,返回值为value
'Lin'
>>> dic
{'name': 'Lin', 23: ['H', True], False: (5, 7), ('weight', '81'): 75, 'gender': 'male', 'hobby': 'eat'}

  删:

>>> dic.pop(23) #把key为参数的键值对删除
['H', True]
>>> dic
{'name': 'Lin', False: (5, 7), ('weight', '81'): 75, 'gender': 'male', 'hobby': 'eat'}
>>> dic.pop('hello','不存在') #key不存在,返回第二个参数,不写报错
'不存在'
>>> dic.popitem() #3.5及以下版本随机删除一对键值对(误),3.6及以上删除最后一个
('hobby', 'eat')
>>> dic
{'name': 'Lin', False: (5, 7), ('weight', '81'): 75, 'gender': 'male'}
>>> del dic[False]
>>> dic
{'name': 'Lin', ('weight', '81'): 75, 'gender': 'male'}
>>> del dic #删除字典
>>> dic.clear() #清空字典

  改:

>>> dic['gender']='female'
>>> dic
{'name': 'Lin', ('weight', '81'): 75, 'gender': 'female'}
>>> dic['gender']='female'
>>> dic
{'name': 'Lin', ('weight', '81'): 75, 'gender': 'female'}
>>> dic2 = {'name':'Wang','height':178}
>>> dic.update(dic2)
>>> dic
{'name': 'Wang', ('weight', '81'): 75, 'gender': 'female', 'height': 178}

  查:

>>> dic.keys()
dict_keys(['name', ('weight', '81'), 'gender', 'height'])
>>> dic.values()
dict_values(['Wang', 75, 'female', 178])
>>> dic.items()
dict_items([('name', 'Wang'), (('weight', '81'), 75), ('gender', 'female'), ('height', 178)])
>>> type(dic.keys())
<class 'dict_keys'>
>>> type(dic.values())
<class 'dict_values'>
>>> type(dic.items())
<class 'dict_items'>
>>> dic.get('name')
Wang
>>> dic.get('h','wrong')
wrong

补充:

a,b = 1,2 # a=1 b=2
a,b = b,a # a=2 b=1
a,b = 'as' # a='a' b='s'
a,b = [3,4] # a=3 b=4
a,b = {1:2,3:4} #a=1 b=3
>>> for i in dic:
    print(i)

name
('weight', '81')
gender
height
>>> for i in dic.items():
    print(i)

('name', 'Wang')
(('weight', '81'), 75)
('gender', 'female')
('height', 178)
>>> for k,v in dic.items():
    print(k,v)

name Wang
('weight', '81') 75
gender female
height 178

 

你可能感兴趣的:(python基础2)