Python学习日记1

.title() 以首字母大写的方式显示每个单词
.upper()全部大写
.lower()全部小写

删除空白:
使用方法
末尾的空白.rstrip()
开头的空白.lstrip()
两边的空白.strip()

数中的下划线:书写很大的数时,可使用下划线将其中的数字分组。
universe_age = 14_000_000_000
print(universe_age)
打印出14000000000。不会打印下划线

注释:
与C++不同,Python的注释用(#)标识,#后面的内容会被解释器忽略

Python 之禅:
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!

以上话是Tim Peters所提出,有兴趣的可以翻译一下,本人英语一般,只能说自己读懂意思,不敢乱翻译,最后误人子弟。

列表:
有点像c++中的数组,仍然是从下标0开始。但有趣的是Python接受-1.返回值为最后一个列表元素:

bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles[-1])
返回值为specialized。
这种语法很有用,因为我们经常需要在不知道列表长度的情况下访问最后的元素,
同样,这种约定也适用其他负数索引,如-2返回的是redline。
(从这点来看简直比C++方便了不知道多少)

在列表中添加元素:
1.在列表末尾添加元素
可以将元素附加(append)到列表
2.在列表中插入元素
可以使用insert(索引位置,值)
(这个是真的方便)

从列表中删除元素
1.使用del语句删除元素
例子:motorcycles = [‘a’,‘b’,‘c’]
del motorcycles[0]
2.使用pop()删除元素
此方法删除列表末尾元素,并让你能够接着使用它,将其从活跃列表中删除,并将其加入非活跃成员列表中
ps:实际上也可以使用pop()来删除列表中任意位置的元素。
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
first_owned = bicycles.pop(0)
print(f"the first bicycle i owned was a {first_owned.title()}.")
3.根据值删除元素
方法remove()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值