自学python之旅(二)

自学python的第二次学习。
今天是关于简单数据处理和一部分列表知识。
在我看来,列表和Java语言中的数组没有太大差别,也比较易懂。关于差一错误是我很难理解的,涉及到本质的东西让我头疼,等有机会研究一下。

print(0.1+0.1);
a=3*0.1;
print(a);
#计算机将十进制转化为二进制进行运算,然后再转成十进制输出,所以会有误差。
import decimal
b=decimal.Decimal('3');
c=decimal.Decimal('0.1');
print(b*c);
#引用decimal函数进行小数误差解决。
age=23;
message="Happy "+str(age)+"rd Birthday!"
print(message);
# 类型错误:如果直接输入“happy"+age+"rd brithdaay!"则会出现错误,系统可以识别int值23,但无法解读,可能是23,也可能是2或3.
# 调用函数str()让非字符串转化为字符串。
d=8/3;
f=8//3;
e=8%3;
print(d);
print(f);
print(e);
#使用除法除不尽则为小数,//为整除运算符,%为求余运算符。

#关于列表
bicycles = ['trek','cannonade','redline','specialized'];
message = "My first bicycle was a "+bicycles[1].title()+".";
print(message);
#差一错误:元素索引从0开始。即1到100的索引是0到99.开发者为了便于处理器对于程序在内存中寻址而特意这么做的。
print(bicycles[-1]);
#在未知列表长度的时候可以直接访问最后的元素。一次类推,可以访问-2或者-3。
print(bicycles);
bicycles[0]='car';
print(bicycles);
#修改列表中任意元素值。
bicycles.append('train');
print(bicycles);
#append在列表尾添加元素。
#在不知列表元素个数的时候,先创建空列表,然后往里添加。
bicycles.insert(5,'airplane');
print(bicycles);
#insert可在任何位置插入元素,并添加到该元素前。5处无元素,即直接在最后
del bicycles[5];
print(bicycles);
#del删除任意位置元素,并且永久删除。
popped_bicycles = bicycles.pop();
print(bicycles);
print(popped_bicycles);
#pop(弹出)方法删除列表尾元素,但仍可以使用。即将其元素移出另外储存。
first_bicycle = bicycles.pop(1);
print("The first bicycle I owned was a " + first_bicycle.title() + ".");
#弹出任意位置列表元素,并且注意该元素已不再列表中。
bicycles.remove('car');
print(bicycles);
delete = 'redline';
bicycles.remove(delete);
print(bicycles);
#remove在不知元素位置时移出指定元素。在移出中也可另外储存。注意:只能移除第一个该元素,若元素有多个,则可以通过循环执行。

关于python禅,我觉得总结的挺好,希望自己牢记在心。

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值