Python列表,元组,字典,序列,引用
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列表,元组,字典,序列,引用的更多相关文章
- python 列表和字典的引用与复制(copy)
列表或字典的引用: 引用针对变量的时候,传递引用后,对引用后的对象的值进行改变是不会影响到原值的:而列表不一样如: spam =42 cheese = spam spam =100 print(spa ...
- python3笔记十八:python列表元组字典集合文件操作
一:学习内容 列表元组字典集合文件操作 二:列表元组字典集合文件操作 代码: import pickle #数据持久性模块 #封装的方法def OptionData(data,path): # ...
- 【277】◀▶ Python 列表/元组/字典说明
目录: 前言 一.访问列表中的值 二.更新列表 三.删除列表元素 四.Python 列表脚本操作符 五.Python 列表函数 & 方法 参考:Python 列表(List)使用说明 列表截取 ...
- Python 列表/元组/字典总结
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...
- Python列表,元组,字典,字符串方法笔记
01. 列表 1.1 列表的定义 List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组 专门用于存储 一串 信息 列表用 [] 定义,数据 之间使用 , 分隔 列 ...
- Python 列表,元组,字典
0)字符串切片 py_str = 'python' >>>py_str[0] #取第一个字符串,返回值为"p",超出范围会报错 >>>py_st ...
- python 列表 元组 字典 集合
列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...
- Python 列表&元组&字典&集合
列表(list) 有序性,可存储任意类型的值 通过偏移存取,支持索引来读取元素,第一个索引为0 ,倒数第一个索引为-1 可变性 ,支持切片.合并.删除等操作 可通过索引来向指定位置插入元素 可通过po ...
- Python列表,元组,字典,集合详细操作
菜鸟学Python第五天 数据类型常用操作及内置方法 列表(list) ======================================基本使用====================== ...
随机推荐
- Cocos2d-x游戏移植到Android平台
1.所需环境支持:Android SDK.NDK.Eclipse.Cygwin.(本人所用系统WIN7-64位) (1)Cygwin的下载安装: Cygwin是Windows下的Linux模拟环境,用 ...
- php中读取文件内容的几种方法
1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件中读取最多 length 个字节.该函数在读取完最多 ...
- 147. Insertion Sort List
Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...
- 260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- cnblogs.com的用户体验
用户体验: 作为博客园的用户,我们觉得博客园的用户体验还是很不错的.但是我们觉得主界面有些混乱,一登录进去太多东西,完全不明白怎么分的类. 评价cnblogs.com的用户体验 1.你是什么样的用户, ...
- kafka的推和拉的问题
之前学习过这一问题,但是面试又被问道了.再次记录下 推还是拉? Kafka最初考虑的问题是,customer应该从brokes拉取消息还是brokers将消息推送到consumer,也就是pull还p ...
- Mac下的利器们介绍
先说说一些快捷键吧,从windows下过来还不很习惯: ctrl + 开关 关机等提示 ctrl+shift+开关 关闭显示器 cmd+option+v 相当于剪贴 cmd+tab,对于最小化了的窗口 ...
- 《苹果开发之Cocoa编程》键-值编码和键-值观察
一.KVC 键-值编码(Key - Value Coding, KVC)是通过变量名的读取和设置变量值的一种方法,将字符串的变量名作为key来引用.NSObject定义了两个方法(KVC方法)用于变量 ...
- 黑马程序员——JAVA基础之构造函数,构造代码块
------- android培训.java培训.期待与您交流! ---------- 构造函数特点: 1. 函数名与类名相同 2. 不用定义返回值类型 3. 不可以写return语句 构造函数 ...
- 磁盘分区、格式化、挂载[转自vbird]
磁盘分区.格式化.挂载磁盘分区 新增分区 查询分区 删除分区磁盘格式化 mkfs mke2fs磁盘挂载与卸载 mount umount 磁盘的分区.格式化.挂 ...