简介Python代码两大实际应用手册

开发 后端
经过长时间学习Python代码 ,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西。以下是相关内容的详细介绍。

本人很喜欢Python代码,在工作中也很喜欢总结关于Python代码的经验教训,下面就这个问题来详细说一般在定义类和函数头时,Python代码的实际应用方面的介绍,希望大家浏览以下文章会有所收获。

字符串——单引号和双引号的作用相同(与perl不同)

  1. perl -p -i -e 's/old/new/g' filename  


引号指示一个多行字符串,一般在定义类和函数头时做解释说明。
Python代码

  1. #!/usr/bin/python   
  2. #Filename:mymodule.py   
  3. class myModule:   
  4. """show   
  5.  
  6. this is only one simple example"""   
  7. pass   
  8.  
  9. p = myModule()   
  10. print p   
  11.  
  12. #!/usr/bin/python  
  13. #Filename:mymodule.py  
  14. class myModule:   
  15. """show   
  16.  
  17. this is only one simple example"""   
  18. pass   
  19.  
  20. p = myModule()   
  21. print p   
  22.  

python的变量:使用变量时只需要赋值,不需要声明或定义数据类型。

python内置的三种数据结构:

list、tple和dict。

一、list常用的几种方法:

  1. append,count,extend,index,insert,pop,remove,reverse,sort  

展示list用法的简单例子:

Python代码

  1. #!/usr/bin/python   
  2. #Filename:using_list.py   
  3.  
  4. shoplist =['apple','mango','carrot','banana']   
  5.  
  6. print 'I have',len(shoplist),'items to purchase.'   
  7.  
  8. print 'These items are:'   
  9. for item in shoplist:   
  10. print item,   
  11.  
  12. print '\nI also have to buy rice.'   
  13. shoplist.append('rice')   
  14. print 'My shopping list is now',shoplist   
  15.  
  16. print 'I will sort my list now'   
  17. shoplist.sort()   
  18. print 'Sorted shopping list is ',shoplist   
  19.  
  20. print 'The first item I will buy is ',shoplist[0]   
  21. olditem = shoplist[0]   
  22. del shoplist[0]   
  23. print 'I bought the',olditem   
  24. print 'My shopping list is now',shoplist   
  25.  
  26. #!/usr/bin/python  
  27. #Filename:using_list.py  
  28.  
  29. shoplist =['apple','mango','carrot','banana']  
  30.  
  31. print 'I have',len(shoplist),'items to purchase.'  
  32.  
  33. print 'These items are:'  
  34. for item in shoplist:  
  35. print item,  
  36.  
  37. print '\nI also have to buy rice.'  
  38. shoplist.append('rice')  
  39. print 'My shopping list is now',shoplist  
  40.  
  41. print 'I will sort my list now'  
  42. shoplist.sort()  
  43. print 'Sorted shopping list is ',shoplist  
  44.  
  45. print 'The first item I will buy is ',shoplist[0]  
  46. olditem = shoplist[0]  
  47. del shoplist[0]  
  48. print 'I bought the',olditem  
  49. print 'My shopping list is now',shoplist  
  50.  

二、tuple与list十分相似,只是tuple和字符串一样是不可变序列。元素间用逗号分隔,为了便于识别一般会在tuple起始和结束位置加括号。
元组最通常的用法是用在打印语句中。

Python代码

  1. #!/usr/bin/python   
  2. #Filename:print_tuple.py   
  3.  
  4. age = 26   
  5. name = 'SongYang'   
  6.  
  7. print '%s is %d years old.' %(name,age)   
  8. print '''''%s loves that girl who he is missing.   
  9.  
  10. Why is %s playing with that python?''' % (name,name)   
  11.  
  12. #!/usr/bin/python  
  13. #Filename:print_tuple.py  
  14.  
  15. age = 26 
  16. name = 'SongYang' 
  17.  
  18. print '%s is %d years old.' %(name,age)  
  19. print '''%s loves that girl who he is missing.  
  20.  
  21. Why is %s playing with that python?''' % (name,name)   
  22.  
  23.  
  24.  

 

Python代码有很多值得学习的地方,这里我们主要介绍Python代码 ,包括介绍实际应用方面的介绍。

【编辑推荐】

  1. Python逻辑操作中的三大应用方案
  2. Python字符串在实际中的操作手册
  3. Python环境在进行初始化后的效果
  4. Python编程语言如何保存搜索引擎结果
  5. Python脚本在游戏中寻找自己的知音
责任编辑:佚名 来源: 腾讯科技
相关推荐

2010-03-17 16:36:10

Java信号量模型

2010-03-19 15:16:11

Python代码

2010-04-01 09:34:06

Oracle函数

2010-09-14 17:27:12

DIV CSS定位

2010-03-16 09:20:25

Python时间

2010-03-10 14:18:36

Python数组

2011-08-09 13:22:31

iPhoneSqlite数据库

2010-04-08 18:33:46

Oracle VARR

2010-03-05 13:48:24

Python for

2010-01-14 16:44:39

VB.NET Mont

2017-09-13 15:37:53

2019-01-10 08:41:50

生物识别身份验证指纹

2011-07-01 10:42:51

IIS解析漏洞

2009-11-30 16:55:10

微软合作Novell

2012-02-01 09:59:05

TitaniumPhoneGapiOS

2010-03-31 17:40:15

Oracle SELE

2010-09-17 16:18:43

Java内存溢出

2022-01-10 22:06:41

机器人AI人工智能

2011-08-10 08:55:28

项目失败

2010-07-15 14:25:06

Perl时间函数
点赞
收藏

51CTO技术栈公众号