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. FDR

    声明: 网上摘抄 False discovery rate (FDR) control is a statistical method used in multiple hypothesis test ...

  2. #include #import @class 的一些用法区别

    从网上查了一些资料,整理了一下,发现很多都说的比较详尽,下面摘录自网络 说一下#import同class之间的区别 在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明 ...

  3. VMware中三种网络连接的区别

    1.概述 大家在安装完虚拟机后,默认安装了如下图的两块虚拟网卡——VMnet1和VMnet8,其中VMnet1是host网卡,用于host方式连接网络:VMnet8是NAT网卡,用于NAT方式连接网络 ...

  4. Codeforces Round #277 (Div. 2) A B C 水 模拟 贪心

    A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...

  5. bzoj 1012 维护一个单调数列

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  6. xctest错误问题解决

    xctest xctest.h file not found(null): Framework not found XCTest 在FrameWork Search Path里增加以下内容$(PLAT ...

  7. 5 款傻瓜式手机 APP 开发工具

    http://www.oschina.net/news/21552/5-app-creators

  8. Codeforces Flipping game 动态规划基础

    题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...

  9. checkbox 点击全选

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 跟上节奏 大数据时代十大必备IT技能(转)

    新的想法诞生新的技术,从而造出许多新词,云计算.大数据.BYOD.社交媒体……在互联网时代,各种新词层出不穷,让人应接不暇.这些新的技术,这些新兴应用和对应的IT发展趋势,使得IT人必须了解甚至掌握最 ...