前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 学习笔记 3 -- 数据

python 学习笔记 3 -- 数据

作者头像
py3study
发布2020-01-07 20:52:47
2710
发布2020-01-07 20:52:47
举报
文章被收录于专栏:python3python3

5.引用

当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 引用 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。 eg.

  1. # -*- coding: utf-8 -*-
  2. shoplist = ['apple', 'mango', 'carrot', 'banana']  
  3. print "we copy the shoplist to mylist directly \"with mylist = shoplist\" "
  4. mylist = shoplist                         # 直接使用等于,此时mylist与shoplist只是指向一个对象的两个名字
  5. print "\tNow the shoplist is : %s"% shoplist  
  6. print "\tNow the mylist is : %s"% mylist  
  7. print "delete the first item of shoplist"
  8. del shoplist[0]                           # 此时删除shoplist 或者mylist 中的元素效果是一样的,都会对那个列表对象直接进行操作
  9. print "\tNow the shoplist is : %s"% shoplist  
  10. print "\tNow the mylist is : %s"% mylist  
  11. print "\nThis time we use slice to cope the shoplist \"with mylist = shoplist[:]\" "
  12. mylist = shoplist[:]                      # 使用一个完整的切片复制,此时mylist只是与shoplist有一样的内容的两个对象!
  13. print "delete the first item of mylist"
  14. del mylist[0]                             # 此时删除mylist中的元素不会对shoplist中的元素造成影响!
  15. print "\tNow the shoplist is : %s"% shoplist  
  16. print "\tNow the mylist is : %s"% mylist  

运行的结果如下:

  1. long@zhouyl:~/python_test$ python cite.py   
  2. we copy the shoplist to mylist directly "with mylist = shoplist"   
  3.     Now the shoplist is : ['apple', 'mango', 'carrot', 'banana']  
  4.     Now the mylist is : ['apple', 'mango', 'carrot', 'banana']  
  5. delete the first item of shoplist  
  6.     Now the shoplist is : ['mango', 'carrot', 'banana']  
  7.     Now the mylist is : ['mango', 'carrot', 'banana']  
  8. This time we use slice to cope the shoplist "with mylist = shoplist[:]"   
  9. delete the first item of mylist  
  10.     Now the shoplist is : ['mango', 'carrot', 'banana']  
  11.     Now the mylist is : ['carrot', 'banana']  

6.字符串的方法

字符串也是对象,同样具有方法。这些方法可以完成包括检验一部分字符串和去除空格在内的各种工作。

eg.

  1. # -*- coding: utf-8 -*-
  2. name = 'longerzone' # 先定义一个字符串
  3. if name.startswith('lon'):                         # startwith 用来测试字符串是否以给定字符串开始。
  4. print 'Yes, the string starts with "lon"'
  5. if 'z' in name:                                    # in操作符用来检验一个给定字符串是否为另一个字符串的一部分
  6. print 'Yes, it contains the string "z"'
  7. if name.find('zon') != -1:                         # find方法用来找出给定字符串在另一个字符串中的位置,或者返回-1以表示找不到子字符串。
  8. print 'Yes, it contains the string "zon"'
  9. delimiter = '_*_'
  10. mylist = ['Brazil', 'Russia', 'India', 'China']  
  11. print delimiter.join(mylist)                       # str类也有以一个作为分隔符的字符串join序列的项目的整洁的方法,它返回一个生成的大字符串。

这里的运行结果如下:

  1. <span style="font-size:18px;">long@zhouyl:~/python_test$ python string.py  
  2. Yes, the string starts with "lon"  
  3. Yes, it contains the string "z"  
  4. Yes, it contains the string "zon"  
  5. Brazil_*_Russia_*_India_*_China  
  6. </span>  

注:本文主要以例子的形式介绍了几种python的数据结构,只能作为简单了解,想跟熟悉使用还需您自己好好练习,多阅读好代码!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 5.引用
  • 6.字符串的方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档