笨方法学Python笔记(6)

    在上一个博客中,我们已经学到了习题31,接下去是习题32关于list的联系,感觉自己好像已经掌握,就想偷懒直接跳过去,往后扫了几眼教程,一路扫到了文末,结尾是这样的一句话:“你会编程。他们不会。这真他妈的酷。”。看得真叫人热血澎湃。所以自己为了能成为“真他妈酷”的人,还是乖乖学习,作者都这么牛逼了,自己是哪根葱,还不会走就想学着跑,要上天啊。于是默默地打开notepad,翻回习题32,扎扎实实,脚踏实地地去做。把这些记录下来也是为了让和我有一样想法的童鞋放弃这种走捷径的想法,努力让今天的自己比昨天的自己更好。可能也没人能看到这些,就当是自言自语,自说自话,自己监督自己吧。


习题32:循环和列表。乖乖贴代码。

the_count = [1,2,3,4,5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 'dimes', 3, 'quarters']

#this first kind of for-loop goes through a list
for number in the_count:
	print "This is count %d" %number
	
#same as above
for fruit in fruits:
	print "A fruit of type: %s" %fruit
	
#also we can go through mixed lists too
#notice we have to use %r since we don't know what's in items
for i in change:
	print "I got %r" %i
	
#we can also build lists, first start with an empty one 
elements = []

#then use the range function to do 0 to 5 counts
for i in range(0,6):
	print "Adding %d to the list." %i
	#append is a function that lists understand
	elements.append(i)
	
#noe we can print them out too
for i in elements:
	print "Element was:%d" %i
	


----------------------------------------------------------------------------------------------------------------------------------------------------------

加分习题1:

1. 注意一下 range 的用法。查一下 range 函数并理解它。

答:百度了一下,先贴引用的网址:http://blog.csdn.net/zhenyu5211314/article/details/19069567

以下内容是从该链接copy的:

函数原型:range(start, end, scan):

参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);

              end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

              scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

这个和matlab中的range有点像的,不过matlab中的是[]这种括号,也就是结束的那个数也能取到。在Python中是()括号,从零开始,end的数字是取不到的。还有就是在上面链接中贴了一个神奇的例子,如下

for i in range(5):  
    print i  
    i += 2  
    print i  
    print '一轮结束'

从结果中可以看出,尽管在代码块中对i进行了操作,但是在for语句中i仍然是从0开始,依次1、2、3、4,变化,并没有受到操作的影响。因此在设计的时候要考虑这一点,不然很容易出错。


2. 在第 22 行,你可以试试直接将 elements 赋值为 range(0,6),而无需使用 for 循环

答:确实是可以的。因为range(5)本身就是一个list,element定义是一个空的list,list之间是可以进行赋值操作的。如果将element=[0,2],然后element = range(5),结果也是一样,说明list之间是可以进行赋值操作的。

#-*- coding: utf-8 -*- 
element = []
element = range(5)
print element
print range(5)


3. 在 Python 文档中找到关于列表的内容,仔细阅读以下,除了 append 以外列表还支持哪些操作?

答:http://www.runoob.com/python/python-lists.html

在这个链接中可以看到关于对list操作的方法和一些函数,等到要用的时候可以再仔细研究。

=======================================================================================

习题 33: While 循环

今天时间比较充足,连着学习了一上午,继续贴代码:

i = 0
numbers = []

while i < 6:
	print "At the top i is %d" % i
	numbers.append(i)
	i = i+1
	print "Number now:", numbers
	print "At the bottom i is %d" % i
	
print "The numbers:"

for num in numbers:
	print num

书中说尽量少用while语句,因为容易死循环,能用for就用for循环。

加分习题1

1. 将这个 while 循环改成一个函数,将测试条件(i < 6)中的 6 换成一个变量。

答:这个简单。

def test_while(test_num):
	i = 0
	numbers = []

	while i < test_num:
		print "At the top i is %d" % i
		numbers.append(i)
		i = i+1
		print "Number now:", numbers
		print "At the bottom i is %d" % i
	
	print "The numbers:"

	for num in numbers:
		print num


test_while(7)



2. 使用这个函数重写你的脚本,并用不同的数字进行测试。

答:这个不贴图了,就是改变test_num的值就可以了。

3. 为函数添加另外一个参数,这个参数用来定义第 8 行的加值 + 1 ,这样你就可以让它任意加值了。

答:这个也简单,直接加个参数就行。

4. 再使用该函数重写一遍这个脚本。看看效果如何。

答:讲真,这道题,我没看懂、、、

5. 接下来使用 for-loop 和 range 把这个脚本再写一遍。你还需要中间的加值操作吗?如果你不去掉它,会有什么样的结果?

答:哦、、、知道了,用for和range试一遍。试了一下,发现i在循环的是是写死了必须按照range的安排一次改变,而不会按照循环中设置的改变走。这一点之前已经有所了解。

def test_while(test_num1,test_num2):
	i = 0
	numbers = []

	while i < test_num1:
		print "At the top i is %d" % i
		numbers.append(i)
		i = i+test_num2
		print "Number now:", numbers
		print "At the bottom i is %d" % i
	
	print "The numbers:"

	for num in numbers:
		print num

def test_for(test_num1,test_num2):
	i = 0
	numbers = []
	for i in range(test_num1):
		print i
		numbers.append(i)
		i = i + test_num2
		print "Number now:", numbers
		print "At the bottom i is %d" %i
	print "The numbers:", numbers
	

test_while(7,2)
print "\n"
test_for(7,2)


=======================================================================================总结:这两个习题主要是练习循环语句的使用,包括if和while两种循环方法。这两种方法都可以实现循环的效果,但是在具体使用的过程需要注意if中循环角标是不会因为循环体中的改变而改变,这一特点有好处也有不好,在日后使用的过程需要加以注意。而while方法有可能陷入死循环,因此在使用的时候也要注意。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值