==开始于2020年6月15日==
方法str.title()以首字母大写的方式显示每个单词
name = "ada lovelace"
print(name.title()) # Ada Lovelace
方法str.upper()和str.lower()
name = "Ada Lovelace"
print(name.upper()) # ADA LOVELACE
print(name.lower()) # ada lovelace
print(name) # Ada Lovelace
删除字符串前后空白,str.strip()和str.rstrip()、str.lstrip()
str = ' python '
print(str.strip()) # 'python'
print(str.lstrip()) # 'python '
print(str.rstrip()) # ' python'
python之禅 交互模式下import this
The Zen of Python, by Tim Peters
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.
- Special cases aren't special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one-- and preferably only one --obvious way to do it.
- Although that way may not be obvious at first unless you're Dutch.
- Now is better than never.
- Although never is often better than *right* now.
- If the implementation is hard to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea -- let's do more of those!
删除列表中的元素的方法
- .del()根据索引删除元素,无返回值。
- .pop()可加索引,无索引时默认移除最后一个元素,并返回该元素。
- .remove()根据值删除第一个出现的元素,无索引,并有返回值。
列表排序
- .sort()永久正向排序,无返回
- .sort(reverse=Ture)永久反向排序,无返回
- sorted(list)临时正向排序,不改变原列表排序,有返回。
- sorted(list,reverse=Ture)临时反向排序,不改变原来列表排序,有返回。
- .reverse()永久反转列表元素的排列顺序,再次反转可恢复原来顺序,无返回。
对数字列表执行简单的统计计算
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45
使用任意数量的关键字实参(创建元组,*创建字典)
def build_profile(first, last, **user_info):
"""创建一个字典,其中包含我们知道的有关用户的一切"""
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)