参考书:《Python编程:从入门到实践》
还有其他
print(a,b,sep="|")
sep规定输出间的间隔
print(“Python\n\tniubi”)
\n换行,\t制表符
文件操作:
f=open('D:\main.java','w')
print('hello, world.',file=f)
f.close()
关闭文件流,以写入文件,或:
f=open('D:\main.java','w')
print('hello, world.',file=f,flush=true)
1.大小写调整:
name='ada lovelace'
print(name.title()) #每个单词首字母大写
print(name.upper()) #全大写
print(name.lower()) #全小写
2.字符串组合与分割
‘Ada’+' '+'Lovelace'
分割:
a='to be or not to be.'
a.split()
组合:
a='python'
b='hi'
a=a.join(b)
3.删除空格:
name=' Python '
print(name.rstrip()) #前
print(name.lstrip()) #后
print(name.rstrip())
4.str()强制类型转换
a='happy'
b='birthday'
c=17
message=a+b+str(c)
a=2
b=4
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a**b)
a=['nan','bei','dong','xi']
print(a[1].title)
修改
a[0]='zhong'
append尾加
a.append('hell')
insert插入
a.insert(0,'heaven')
del删除索引元素
del a[1]
pop删除末尾元素
poped_a=a.pop()
print(a)
print(poped_a)
pop()索引元素
poped_a=a.pop(3)
print(a)
print(poped_a)
remove()
a=['nan','bei','dong','xi']
a.remove('nan')
sort()永久性排序
books=['Python','cookbook','the reader','me before you']
books.sort()
print(books)
books.sort(reverse=ture)
print(books)
sorted()临时排序
books=['Python','cookbook','the reader','me before you']
print(sorted(books))
print(books)
列表反相
books.reverse()
确定列表长度
books_len=len(books)
反向索引
re_books=books[-1]
列表加与乘
list1=[1,2,3,4]
list2=[4.5.6.7]
list3=list1+list2
list4=list1*3
序列封包与序列解包
#序列封包
x=1,2,3
#x输出为元组
#序列解包
li = ["yjj",12,['yjj',3]]
a,b,c = li
a = ["",15,8,4,6]
c,b,*d = a
a,*b,c = range(10)
#* 承担剩余元素
遍历
books=['Python','cookbook','the reader','me before you']
for book in books:
print(book)
range()
for value in range(1,6):
print(value)
#1-5,后续可以添加间隔eg:range(1,20,2)
创建列表
list1=list(range(1,6))
min,max,sum 简单统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
digits_min=min(digits)
digits_max=max(digits)
digits_sum=sum(digits)
列表解析
#eg.1 1-10的平方数列
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
squares = [value**2 for value in range(1,11)]
print(squares)
切片
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(digits[0:3])
切片同样可以应用于循环
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
for digit_0 in digits[0:3]:
print(digit_0)
与列表类似,但不能直接修改,但是可以给变量赋值:
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
dimensions = (400, 50)
for dimension in dimensions:
print(dimension)
条件设置
#多个条件并行 和
age_0 >= 21 and age_1 >= 21
#多个条件并行 或
age_0 >= 21 or age_1 >= 21
#参数是否在特定序列
'pepperoni' in requested_toppings
列表处理
if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")
字典的定义与输出
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
print(alien_0['points'])
添加值
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
#{'color': 'green', 'points': 5, 'y_position': 25, 'x_position': 0}
新字典,修改值
alien_0 = {}
alien_0 = {'color': 'green'}
print("The alien is " + alien_0['color'] + ".")
alien_0['color'] = 'yellow'
print("The alien is now " + alien_0['color'] + ".")
#根据键值
例子,alien
alien_0 = {'x_position': 0, 'y_position': 25, 'speed': 'medium'}
print("Original x-position: " + str(alien_0['x_position']))
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
x\_increment = 3
alien_0['x_position'] = alien_0['x_position'] + x_increment
print("New x-position: " + str(alien_0['x_position']))
删除键-值对
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
del alien_0['points']
print(alien_0)
遍历字典
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():
print("\nKey: " + key)
print("Value: " + value)
#定义两个变量用来存贮键与值
嵌套:暂略6.4.2
手机扫一扫
移动阅读更方便
你可能感兴趣的文章