s22day2笔记

Posted zzp1991

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了s22day2笔记相关的知识,希望对你有一定的参考价值。

内容大纲:1、字符串   2、格式化输出,运算符  3、列表   4、列表的嵌套   5、tuple元组range     6、字典的增删改查     7、字典的嵌套及数据类型补充

一、字符串

# s9 = s[-1:-5:-1]
# print(s9)
#切片:s[起始索引:结束索引+1:步长]

s = ‘alexWUsir‘
# ** capitalize 首字母大写其余字母小写
# s1 = s.capitalize()
# print(s1)

# ** 大小写翻转
# s2 = s.swapcase()
# print(s2)

# *title 非字母隔开的每个单词的首字母大写
# s = ‘alex wusir*taibai2ritian‘
# s3 = s.title()
# print(s3)

# center
# *center 设置总长度,并居中
# s4 = s.center(30,)
# s4 = s.center(30,‘*‘)
# print(s4)

#*** upper() lower()
# s5 = s.upper()
# print(s5)
# code = ‘AwEqr‘

 

s = ‘ alexWUsir\t‘
# *** strip 默认去除字符串前后的空格,换行符,制表符。
# 去除的元素可自定制。
#lstrip(),rstrip()
# s7 = s.strip()
# print(s7)

 

# *** replace 替换
# s = ‘alex 分甘共苦老地方 alex 风刀霜剑卡乐芙‘
# s8 = s.replace(‘alex‘, ‘SB‘)
# s8 = s.replace(‘alex‘, ‘SB‘,1)
# print(s8)

 

#***split 分隔,str ---> list
# s1 = ‘alex wusir taibai ritian‘
# s2 = ‘alex,wusir,taibai,ritian‘
# s3 = ‘alexwusirtaibairitian‘
# s4 = ‘alexwusirtaibairitian‘
# l1 = s1.split() # 默认以空格分隔
# l2 = s2.split(‘,‘)
# l3 = s3.split(‘a‘)
# l4 = s4.split(‘a‘,2)
# print(l4)

 

s = ‘alexwausir‘
# ***find index 通过元素找索引
# find 找不到返回-1
# index找不到会报错
# s11 = s.find(‘e‘)
# s12 = s.find(‘ex‘)
# print(s11,type(s11))
# print(s12,type(s12))
# s11 = s.find(‘a‘,1)
# print(s11)

 

# format
# s = "我叫{},今年{},爱好{}"
#第一种用法
# s12 = s.format(‘太白‘, ‘25‘, ‘女‘)
# print(s12)
#第二种用法
# s1 = "我叫{0},今年{1},爱好{2},我依然叫{0}"
# s13 = s1.format(‘太白‘, ‘25‘, ‘女‘)
# print(s13)
#第三种用法
# s3 = "我叫{name},今年{age},爱好{hobby}"
# s14 = s3.format(age=25,hobby=‘girl‘,name=‘太白‘)
# print(s14)

 

# 公共方法:
#len 测量个数
# print(len(s))
# #count
# print(s.count(‘a‘))

# name = ‘123‘
# print(name.isalnum()) #字符串由字母或数字组成
# print(name.isalpha()) #字符串只由字母组成
# print(name.isdigit()) #字符串只由数字组成

 

‘‘‘
str ---> int 字符串全部由数字组成 int(‘123‘)
int ---> str str(1)


int ---> bool 0---> False 非0 ---> True bool(1)
# print(bool(100))
# print(bool(-1))
# print(bool(0))
bool --> int True 1 False 0
print(int(True))
print(int(False))

str ---> bool 非空 True 空字符串 False
print(bool(‘‘))
print(bool(‘fdsaf‘))
bool ---> str
# print(str(True),type(str(True)))

str ---> list split
list --> str join

‘‘‘

二、格式化输出,运算符

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#格式化输出 % 占位符 s d
# name = input(‘请输入名字:‘)
# age = input(‘请输入年龄:‘)
# job = input(‘请输入职业:‘)
# hobbie = input(‘请输入爱好:‘)
# msg = ‘‘‘------------ info of %s -----------
# Name : %s
# Age : %d
# job : %s
# Hobbie: %s
# ------------- end -----------------
# ‘‘‘ % (name, name, int(age), job, hobbie)
# print(msg)

# print(‘我叫%s,今年%d,学习进度2%%‘ % (‘老男孩‘, 10000))

#and or not

# 1,前后都是比较运算
# 优先级:() > not > and > or
# print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)

# 2, 前后都是数值
‘‘‘
0 是False 非0 都是True
x or y if x is True,return x
‘‘‘
# print(3 or 4)
# print(1 or 4)
# print(0 or 4)
# print(-1 or 4)

# print(3 and 5)

# print(3 or 4 and 5)

choice = input(‘>>>‘).strip()
if not choice:
pass

 

三、列表

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 按照索引取值,与元素本身的数据类型一致
# 按照切片取值,取出来的是小列表。
# li = [1, (2, 3), 2, 3, 4, 5, 6,]
# l1 = li[:3]
# print(l1)
# print(li[0],type(li[0]))
# print(li[1],type(li[1]))
# l2 = li[:]
# print(l2)

l = [‘WuSir‘, ‘alex‘, ‘OldBoy‘, ‘barry‘]
# 增
# 追加 append()
# l.append(‘景女神‘)
# l.append(1)
# l.append([1, 2, 3])
# print(l)

# insert
# l.insert(1, ‘葫芦‘)
# print(l)

# extend
# l.extend(‘abc‘)
# l.extend([1, ‘2122‘, 56])
# print(l)
# 删
# 按照索引删除pop
# ret = l.pop(1)
# print(ret)

# 按照元素去删除 remove
# l.remove(‘alex‘)
# print(l)

#clear 清空列表
# l.clear()
# print(l)

# del l
# print(l)

# del l[1]
# print(l)

# del l[:3]
# print(l)
‘‘‘
1 删除列表。
2,按照索引删除。
3.可以按照切片去删除。
‘‘‘
# 改
# 按照索引改
# l[0] = ‘日天‘
# print(l)
# l[2] = ‘老男孩‘
# print(l)

# 按照切片去改
# l[:3] = ‘abcdgfdljgkfdgjlfdlgjfdsaklfdjskladsfj‘
# l[:3] = ‘a‘
# l[:3] = [11, 22, 33, 44]
#加步长 必须一一对应
# l[:3:2] = ‘ab‘
# print(l)
# 查
#按照索引,切片(步长)
#for 循环遍历。
# for i in l:
# print(i)


# 其他方法
# print(len(l)) # 总个数
# print(l.count(‘WuSir‘)) # 某个元素出现的次数
# l1 = [1, 9, 8, 4, 3, 2, 6,]
# l1.sort() # 正序排序
# print(l1)
# l1.sort(reverse=True) # 倒叙排序
# print(l1)
# l1.reverse() # 翻转
# print(l1)

 

四、列表的嵌套

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# l1 = [1, 2, ‘alex‘, [‘WuSir‘, ‘taibai‘, 99], 6]
# 1,将alex该成Alex
# l1[2] = l1[2].capitalize()

#2,将‘WuSir‘变成全部大写。
# l1[3][0] = l1[3][0].upper()
# print(l1)
# 3,99让其通过数字加1的方式变成 ‘100‘.
# l1[3][-1] = str(l1[3][-1] + 1)
# print(l1)

 

五、06 tuple元组range

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#元组 只读列表。
# tu = (1, 2, 33, ‘alex‘)
# print(tu[1])
# print(tu[:-1])
# for i in tu:
# print(i)
# tu1 = (1, 2, [1, 3,], 4)
# tu1[2].append(666)
# print(tu1)

#range

# print(range(100))
# for i in range(100):
# print(i)
# for i in range(1,11):
# print(i)

# for i in range(2, 101, 2):
# print(i)

# for i in range(100,0,-1):
# print(i)

# l1 = [1, 2, 3, 4, 5, ‘alex‘]
# for i in range(len(l1)):
# print(i)

 

六、字典的增删改查

#!/usr/bin/env python
# -*- coding:utf-8 -*-

dic = {
‘name‘: ‘老男孩‘,
‘age‘: 10000,
‘sex‘: ‘男‘,
}
# 增
# dic[‘hobby‘] = ‘old_girl‘ # 无责增加,有责修改。
# dic[‘name‘] = ‘alex‘
# print(dic)

# dic.setdefault(‘high‘) # 有key则不修改,无责添加。
# dic.setdefault(‘high‘, 169)
# dic.setdefault(‘name‘, ‘wusir‘)
# print(dic)
# 删
# print(dic.pop(‘name‘)) # 返回值
# # dic.pop(‘name1‘,None)
# print(dic)

#popitem 随机删除 有返回值
# print(dic.popitem())
# print(dic)

#clear
# dic.clear()
# print(dic)

# del dic
# print(dic)
# del dic[‘name‘]
# print(dic)
‘‘‘
1,删除整个字典
2,按照键去删除键值对
‘‘‘
# 改
# dic[‘name‘] = ‘alex‘ # 无责增加,有责修改。
# print(dic)

# dic = {"name": "jin", "age": 18,"sex": "male"}
# dic2 = {"name": "alex", "weight": 75}
# dic2.update(dic) # 将dic里面的键值对覆盖并更新到dic2中,dic不变 。
# print(dic) # {‘name‘: ‘jin‘, ‘age‘: 18, ‘sex‘: ‘male‘}
# print(dic2) # {‘name‘: ‘jin‘, ‘weight‘: 75, ‘age‘: 18, ‘sex‘: ‘male‘}

# 查
# print(dic[‘name‘])
# print(dic[‘name1‘])
# print(dic.get(‘name‘))
# print(dic.get(‘name1‘,‘没有此键...‘))
# print(dic.keys()) # 类似于列表的一个容器,没有索引
# for i in dic.keys():
# print(i)
# for i in dic:
# print(i)

print(dic.values())
# print(dic.items())
# for i in dic.items():
# print(i)

# 分别赋值
# a, b = 1 ,3
# a, b = [22, 33]
# print(a,b)
# for k, v in dic.items():
# print(k, v)

 

七、字典的嵌套及数据类型的补充

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# dic = {‘name_list‘:[‘高猛‘, ‘于其‘,],
# 1:{
# ‘alex‘: ‘李杰‘,
# ‘high‘: ‘175‘,
# }
# }
# [‘高猛‘, ‘于其‘,] 追加一个元素‘wusir‘,
# 2, {‘alex‘: ‘李杰‘,‘high‘: ‘175‘ } 增加一个键值对 ‘sex‘: man,
# dic[‘name_list‘].append(‘wusir‘)
# print(dic)
# print(dic[1])
# dic[1][‘sex‘] = ‘man‘
# print(dic)

# l1 = [11, 22, 33, 44, 55]
# 将索引为奇数对应的元素删除。
# del l1[1::2]
# print(l1)
# 再循环一个列表时,不要改变列表的大小,这样会影响结果。
# for i in range(len(l1)):
# print(i)
# print(l1)
# if i % 2 == 1:
# del l1[i]
# print(i)
# print(l1)
# print(l1)

# for i in range(len(l1)-1,-1,-1):
# if i % 2 == 1:
# del l1[i]
# print(l1)


# dictionary changed size during iteration
# 再循环一个字典时,不要改变字典的大小,这样会影响结果。
# dic = {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘:‘v3‘, ‘name‘:‘alex‘}
# for k in dic:
# if ‘k‘ in k:
# dic.pop(k)
# print(dic)
# s1 = ‘abc‘
# print(‘a‘ in s1)
# l1 = []
# for i in dic:
# if ‘k‘ in i:
# l1.append(i)
# print(l1)
# for i in l1:
# del dic[i]
# print(dic)

# dic = dict.fromkeys(‘abc‘,666)
# print(dic)

# dic = dict.fromkeys(‘abc‘,[])
# dic[‘a‘].append(666)
# print(dic)

 

以上是关于s22day2笔记的主要内容,如果未能解决你的问题,请参考以下文章

python day2 笔记

day2,笔记内容

python学习笔记 Day2

Day2 Java学习笔记

Python学习笔记_day2

python学习笔记(day2)