1.列表

# Filename: using_list.py
# This is my shopping list
shoplist=["apple", "mango", "carrot", "banana"]
print ("I have", len(shoplist), "items to purchase.")
print ("These items are:"),
for item in shoplist:
print (item),
print ("\nI also have to buy rice.")
shoplist.append("rice")
print ("My shopping list is now", shoplist) print ("I will sort my list now")
shoplist.sort()
print ("The first item I will buy is", shoplist[0])
olditem=shoplist[0]
del shoplist[0]
print ("I bought the", olditem)
print ("My shopping list is now", shoplist)

2.元组

# !/usr/bin/python
# Filename: using_tuple.py zoo=("wolf", "elephant", "penguin")
print ("number of animals in the zoo is", len(zoo)) new_zoo=("monkey", "dolphin", zoo)
print ("number of animals in the new zoo is", len(new_zoo))
print ("all animals in new zoo are", new_zoo)
print ("animals brought from old zoo are", new_zoo[2])
print ("last animal brought from old zoo is", new_zoo[2][2])

3.元组打印

#!/usr/bin/python
# Filename: print_tuple.py
age = 22
name = 'Swaroop'
print ('%s is %d years old' % (name, age))
print ('Why is %s playing with that python?' % name)

4.字典

#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ook
ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}
print ("Swaroop's address is %s" % ab['Swaroop'])
# Adding a key/value pair
ab['Guido'] = 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
print ('\nThere are %d contacts in the address-book\n' % len(ab))
for name, address in ab.items():
print ('Contact %s at %s' % (name, address))
if 'Guido' in ab: # OR ab.has_key('Guido')
print ("\nGuido's address is %s" % ab['Guido'])

5.序列

#!/usr/bin/python
# Filename: seq.py
shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print ('Item 0 is', shoplist[0])
print ('Item 1 is', shoplist[1])
print ('Item 2 is', shoplist[2])
print ('Item 3 is', shoplist[3])
print ('Item -1 is', shoplist[-1])
print ('Item -2 is', shoplist[-2])
# Slicing on a list
print ('Item 1 to 3 is', shoplist[1:3])
print ('Item 2 to end is', shoplist[2:])
print ('Item 1 to -1 is', shoplist[1:-1])
print ('Item start to end is', shoplist[:])
# Slicing on a string
name = 'swaroop'
print ('characters 1 to 3 is', name[1:3])
print ('characters 2 to end is', name[2:])

6.引用

#!/usr/bin/python
# Filename: reference.py
print ('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object!
del shoplist[0]
print ('shoplist is', shoplist)
print ('mylist is', mylist)
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object
print ('Copy by making a full slice')
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print ('shoplist is', shoplist)
print ('mylist is', mylist)
# notice that now the two lists are different

7.字符串

#!/usr/bin/python
# Filename: str_methods.py
name = 'Swaroop' # This is a string object
if name.startswith('Swa'):
print ('Yes, the string starts with "Swa"')
if 'a' in name:
print ('Yes, it contains the string "a"')
if name.find('war') != -1:
print ('Yes, it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print (delimiter.join(mylist))

Python列表,元组,字典,序列,引用的更多相关文章

  1. python 列表和字典的引用与复制(copy)

    列表或字典的引用: 引用针对变量的时候,传递引用后,对引用后的对象的值进行改变是不会影响到原值的:而列表不一样如: spam =42 cheese = spam spam =100 print(spa ...

  2. python3笔记十八:python列表元组字典集合文件操作

    一:学习内容 列表元组字典集合文件操作 二:列表元组字典集合文件操作 代码: import pickle  #数据持久性模块 #封装的方法def OptionData(data,path):    # ...

  3. 【277】◀▶ Python 列表/元组/字典说明

    目录: 前言 一.访问列表中的值 二.更新列表 三.删除列表元素 四.Python 列表脚本操作符 五.Python 列表函数 & 方法 参考:Python 列表(List)使用说明 列表截取 ...

  4. Python 列表/元组/字典总结

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...

  5. Python列表,元组,字典,字符串方法笔记

    01. 列表 1.1 列表的定义 List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组 专门用于存储 一串 信息 列表用 [] 定义,数据 之间使用 , 分隔 列 ...

  6. Python 列表,元组,字典

    0)字符串切片 py_str = 'python' >>>py_str[0] #取第一个字符串,返回值为"p",超出范围会报错 >>>py_st ...

  7. python 列表 元组 字典 集合

    列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...

  8. Python 列表&元组&字典&集合

    列表(list) 有序性,可存储任意类型的值 通过偏移存取,支持索引来读取元素,第一个索引为0 ,倒数第一个索引为-1 可变性 ,支持切片.合并.删除等操作 可通过索引来向指定位置插入元素 可通过po ...

  9. Python列表,元组,字典,集合详细操作

    菜鸟学Python第五天 数据类型常用操作及内置方法 列表(list) ======================================基本使用====================== ...

随机推荐

  1. 解决:“MediaPlayer error (1, -2147483648)”问题

    如果你使用VideoView播放过MP4视频,你可能碰到过类似下面的问题: MediaPlayer   error (1, -2147483648) 如果你查阅文档,会发现1其实代表MEDIA_ERR ...

  2. leetcode 141. Linked List Cycle ----- java

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  3. Crawler4j学习笔记

    Crawler4j概述 crawler4j是一款基于Java的轻量级单机开源爬虫框架,最大的一个特点就是简单.另外也支持多线程.支持代理.可以过滤重复URL 基本上从加载jar到工程里面 通过修改示例 ...

  4. URAL Mosaic(并查集)(欧拉回路)

    Mosaic Time limit: 0.25 secondMemory limit: 64 MB There's no doubt that one of the most important an ...

  5. 附录二 C语言标准库

    上章回顾 数组和指针相同与不同 通过指针访问数组和通过数组访问指针 指针在什么时候可以加减运算 函数指针的申明和调用 函数数组和数组函数 git@github.com:Kevin-Dfg/Data-S ...

  6. javascript往textarea追加内容

    <html> <body> <textarea id="content"></textarea> <script> va ...

  7. windows7旗舰版激活密钥永久版免费分享

    windows7之家不仅提供精品Win7教程 给大家,加上这个windows7激活密匙还帮大家解决windows7系统激活问题,包括win7旗舰版 windows7安装版这些. 用的是Windows7 ...

  8. 自然语言处理2.1——NLTK文本语料库

    1.获取文本语料库 NLTK库中包含了大量的语料库,下面一一介绍几个: (1)古腾堡语料库:NLTK包含古腾堡项目电子文本档案的一小部分文本.该项目目前大约有36000本免费的电子图书. >&g ...

  9. unity 合并skinnedMeshRenderer中遇到的一个大坑

    将多个skinnedMeshRenderer合并成一个skinnedMeshRenderer,主要涉及的mesh合并.骨骼列表合并.重定向顶点骨骼索引.其中重定向顶点骨骼索引只是通过加偏值即可完成,所 ...

  10. OpenJudge计算概论-求一元二次方程的根【含复数根的计算、浮点数与0的大小比较】

    /*====================================================================== 求一元二次方程的根 总时间限制: 1000ms 内存限 ...