码迷,mamicode.com
首页 > 编程语言 > 详细

python代码练习(每天一小时)

时间:2017-11-28 01:24:33      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:splay   closed   div   列表   col   display   max   odi   sed   

技术分享图片
 1 #-*-  coding :utf-8  -*-
 2 print("#1.函数")
 3 def my_abs(x):
 4     if x>=0:
 5         return x;
 6     else:
 7         return -x;
 8 print(my_abs(-10))
 9 
10 #递归函数
11 print("#2.递归函数")
12 def fact(n):
13     if n==1:
14         return 1;
15     return n*fact(n-1);
16 
17 print(fact(4))
18 
19 #3.切片:取元素
20 print("#3.切片")
21 L = list(range(100))
22 print(L[10:20]);
23 
24 #4.迭代:在Python中,迭代是通过for ... in来完成的
25 print("#4.迭代")
26 
27 e={a:1,b:2,c:3,d:4}
28 for key in e:
29     print(key)
30 print(------------------)
31 for e in abcd:
32     print(e)
33 
34 #5.列表生成式:列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。
35 print("#5.列表生成式")
36 
37 L=[]
38 for x in range(2,10):
39     L.append(x*x)
40 print(L)
41 
42 #6.生成器
43 print("#6.生成器")
44 L=[x*x for x in range(10)]
45 print(L)
46 print("---斐波拉契数列------")
47 def fb(max):
48     n,a,b=0,0,1
49     while  n<max:
50         print(b)
51         a,b=b,a+b
52         n=n+1
53     return done
54 print(fb(5))
55 
56 #7.迭代器
57 print("#7.迭代器")
58 from collections import Iterable
59 print(isinstance([], Iterable))
View Code

运行效果:

#1.函数
10
#2.递归函数
24
#3.切片
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
#4.迭代
a
b
c
d
------------------
a
b
c
d
#5.列表生成式
[4, 9, 16, 25, 36, 49, 64, 81]
#6.生成器
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
---斐波拉契数列------
1
1
2
3
5
done
#7.迭代器
True

 

python代码练习(每天一小时)

标签:splay   closed   div   列表   col   display   max   odi   sed   

原文地址:http://www.cnblogs.com/chenyanlong/p/7906777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!