基本概念

  迭代(iteration):如果给定一个list或tuple,我们可以通过for循环来遍历,这种遍历我们称为迭代(iteration)

  可变:value改变,id不变,可变类型是不可hash类型

  不可变:value改变,id就变,不可变类型是可hash类型

字符串

  字符串是可迭代,不可变的

  example_str='life is short you need python'

  常用操作:index()、len()、切片、成员运算、strip()、split()、join()、循环遍历、replace()、count()、upper()、lower()、enumerate()、isdigit()、isalpha()



example_str='life|is|short you|need|python'
example_bank=' today is Monday '
example_other='***good morning***'
#index()/rindex():获取索引值(适用于可遍历对象如列表,字符串,元祖)
print(example_str.index('p'))
#len():求长度()
print(len(example_str))
#[::]:切片
print(example_str[::]) #取所有字符串
print(example_str[0:10:])#从左向右切,(默认步长为1)
print(example_str[0:10:1])#从左向右切,步长为1
print(example_str[0:10:2])#从左向右切,步长为2
print(example_str[::-1])#从右向左切,步长为1
print(example_str[-1:-8:-2])#从右向左切,步长为2
#in/not in:成员运算(适用于可迭代对象如列表,字符串,元祖)
life in example_str
live not in example_str
#strip()/lstrip()/rstrip():移除空白
print(example_bank.strip())#strip()默认移除字符串前后的空白
print(example_other.strip('*'))#移除字符串前后的*
#split():切分
example_split=example_str.split('|')#按'|'对字符串进行切分并存在列表里
print(example_split)
#join():连接
example_join=('*').join(example_split)#按'*'将字符串连接在一起,可迭代对象必须都是字符串
print(example_join)
#for str in example_str:循环遍历(适用于可迭代对象如列表、字符串和元祖)
for s in example_str:
print(s)
#replace():替换
print(example_str.replace('short','long'))#字符串替换,replace(para1,para2)中需指定两个参数
#count():计数
print(example_str.count('s'))#统计字符串中's'出现的个数
#upper:大写
print(example_str.upper())#将字符串中所有字符都变成大写
#lower:小写
print(example_str.lower())#将字符串中所有字符都变成小写
#enumerate():枚举(适用于可迭代对象如列表、字符串和元祖)
for index,s in enumerate(example_str): #同时获取索引和值
print(index,s)
#isdigit(): 可以判断bytes和unicode类型,检测字符串是否只由数字组成
age=input('Please input your age:')
print(type(age))

  了解:format()、startswith()、endswith()、find()、center()、expandtabs()、capitalize()

#format():格式化
print('{0}{1}{0}'.format('love','you'))
print('{name}{age}{nature}'.format(nature='cute',name='luoli',age=18))
#startswith():判断开头
print(example_str.startswith('life'))
#endswith():p判断结尾
print(example_str.endswith('python'))
#find()/rfind():查找字符串
print(example_str.find('s',0,6))#找不到则返回-1
print(example_str.index('s',0,6))#找不到会报错
#center()/ljust()/rjust()/zfill():填充
name='luoli'
print(name.center(20,'-')
print(name.rjust(20,'*'))
print(name.ljust(20,'+'))
print(name.zfill(10))
#expandtabs(tabsize=8):指定转换字符串中的 tab 符号('\t')转为空格的字符数
name='luoli\tcute'
print(name.expandtabs(8))
#capitalize()/swapcase()/title():首字母大写/大小写翻转/每个单词的首字母大写
name='hello,luoli'
print(name.capitalize())
print(name.swapcase())
print(name.title())

列表

  列表是可迭代,可变的

  example_list=['Monday','Tuesday','Friday','Sunday']

  常用操作:切片、长度、遍历、查找元素、添加元素、删除元素、排序

example_list=['Monday','Tuesday','Wednesday ','Thursday','Friday']
#[::]:切片
print(example_list[2:4])
#len():长度
print(len(example_list))
#遍历
for i in example_list:
print(i)
#查找元素
print('Monday' in example_list) #in /not in
print(example_list.index('Tuesday',0,4)) #index()
print(example_list.count('Tuesday')) #count()
#添加元素
example_append=['Saturday','Sunday'] #append()
example_list.append(example_append)
print(example_list)
example_extend=['Saturday','Sunday'] #extend():可以将另一个集合中的元素逐一添加到列表中
example_list.extend(example_extend)
print(example_list)
example_list.insert(1,'hello') #insert():可以在指定位置添加元素
print(example_list)
#删除元素
del example_list[0] #del():根据下标进行删除
print(example_list)
example_list.pop() #pop():删除最后一个元素
print(example_list)
example_list.remove('Tuesday') #remove():根据元素的值进行删除
print(example_list)
#排序
a=[1,4,3,2,5] #sort():按特定顺序重新排列,默认由小到大
a.sort()
print(a)
a.sort(reverse=True) #将list逆置
print(a)

元祖

  元祖是可迭代,不可变的

  example_tuple=('apple','peach','cherry','orange')

  元祖和列表非常相似,但是元祖不可变,一旦初始化就不能进行修改

example_tuple=('apple','peach','cherry','pear','orange')
#index()/count()
print(example_tuple.index('apple'))
print(example_tuple.count('peach'))
#切片
print(example_tuple[0:4:2])
#长度
print(len(example_tuple))
#循环
for i in example_tuple:
print(i)
#成员运算
print('apple' in example_tuple) 

字典

  字典是可迭代,可变的

  example_dict={'name':'luoli','age':18,'sex':'female'}

  默认情况下,dict迭代的是key。如果要迭代value,可以用for value in d.values(),如果要同时迭代key 和value,可以用for k,v in d.items()

example_dic={'apple':18,'beach':20,'pear':30,'cherry':40,'orange':50}
#按key取值,可存可取
print(example_dic['apple'])
#遍历
for i,s in example_dic.items():
print(i,example_dic[i])
#成员运算
print('apple' in example_dic)
#添加元素
a={'x':2,'y':3}
a['z']=5 #在使用dic['key']=value时,这个'key'在字典中,如果不存在就会新增这个元素,如果'key'存在,新value就会覆盖原value
for i,s in a.items():
print(i,a[i])
a.setdefault('y',5) #key存在,则不赋值,key不存在,则在字典中新增key和value
for i,s in a.items():
print(i,a[i])
#删除元素
b={'m':12,'n':18}
del b['m'] #del()删除指定的元素
for i in b:
print(i)
b.clear() #清空整个字典
print(b)

集合

  集合是可迭代,可变的

  example_set={'a','b','c','d','e'}

  集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,主要用于去重和关系运算

example_set1={'a','b','f'}
example_set2={'a','b','c','d','e'}
#长度
print(len(example_set1))
#成员运算
print('a' in example_set1)
print('d' not in example_set1)
#合集 |
print(example_set1|example_set2)
#交集 &
print(example_set1&example_set2)
#差集 -
print(example_set1-example_set2)
#对称差集
print(example_set1^example_set2)
#==
print(example_set1==example_set2)
#父集 > >=
print(example_set1>example_set2)
#子集 < <=
print(example_set1<example_set2)

练习

(1).test_list=[1,2,3,4,5,6,7,5,3,6,67,43,12,0,34,23]
a.去除列表中的重复元素
b.将去重后的列表进行由小到大排序
test_list=[1,2,3,4,5,6,7,5,3,6,67,43,12,0,34,23]
s=set(test_list)
li=list(s)
li.sort()
print(li)
(2).部门员工管理
a.能够查询部门是否有该员工
b.能够增加员工名称
c.能够删除员工名称
d.能够修改员工名称
msg='''
1. 添加名⽚
2. 删除名⽚
3. 修改名⽚
4. 查询名⽚
5. 退出系统
'''
list=[]
no_quit=True
while no_quit:
print(msg)
#print(list)
user_choice=input('Please chioce the number:')
if user_choice.isdigit():
choice=int(user_choice)
if choice == 1:
add_name=input('Pleaase enter the username:')
list.append(add_name)
elif choice == 2:
move_name=input('Please enter the username:')
if move_name in list:
list.remove(move_name)
else:
print('The username is not exist!')
elif choice == 3:
old_name=input('Please enter the old name:')
if old_name in list:
i=list.index(old_name)
new_name=input('Please input the new name:')
list[i]=new_name
print('change succeed!')
elif choice == 4:
query_name=input('Please enter the search name:')
if query_name in list:
print(list)
else:
print('Search failed!')
elif choice == 5:
no_quit=False
continue
else:
print('Error input! Please choose again')
else:
print('Illegal choice!')

第二章 Python数据类型详解的更多相关文章

  1. Python数据类型详解——元组

    Python数据类型详解--元组 有时候我们的列表数据不想被别人修改时该怎么办? 此时,就可以使用元组来存放,元祖又称为只读列表,不能修改 定义方式:与列表类似,将列表的[]换成()即可. 特性: 1 ...

  2. Python数据类型详解——字典

    Python数据类型详解--字典 引子 已经学习了列表,现在有个需求--把公司每个员工的姓名.年龄.职务.工资存到列表里,你怎么存? staff_list = [ ["Kwan", ...

  3. python 数据类型详解

    python数据类型详解 参考网址:http://www.cnblogs.com/linjiqin/p/3608541.html 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8 ...

  4. python数据类型详解(全面)

    python数据类型详解 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8.字典9.日期 1.字符串1.1.如何在Python中使用字符串a.使用单引号(')用单引号括起来表示字 ...

  5. 第二章 IP协议详解

    第二章 IP协议详解 2.1 IP服务的特点 它为上层协议提供了无状态,无连接,不可靠的服务 名称 简介 优点 缺点 对付缺点的方法 无状态 IP通信双方不同步传输数据的状态信息 无须为保持通信的状态 ...

  6. 转 python数据类型详解

    python数据类型详解 目录 1.字符串 2.布尔类型 3.整数 4.浮点数 5.数字 6.列表 7.元组 8.字典 9.日期 1.字符串 1.1.如何在Python中使用字符串 a.使用单引号(' ...

  7. Python数据类型详解——列表

    Python数据类型详解--列表 在"Python之基本数据类型概览"一节中,大概介绍了列表的基本用法,本节我们详细学一下列表. 如何定义列表:在[]内以英文里输入法的逗号,,按照 ...

  8. Cobalt Strike系列教程第二章:Beacon详解

    上周更新了Cobalt Strike系列教程第一章:简介与安装,文章发布后,深受大家的喜爱,遂将该系列教程的其他章节与大家分享,提升更多实用技能! 第二章:Beacon详解 一.Beacon命令 大家 ...

  9. 1.python数据类型详解

    python数据类型分类 1).数值型:整数型(int).浮点型(float).布尔型(bool 取值:True.False) 2).容器类型 : 字符串型(str).列表(list).元祖(tupl ...

随机推荐

  1. 2018秋招blibli算法工程师

    我给出代码如下:和之前做数塔(dp的入门题目)的思路一致 dp[i][j]为走到坐标(i,j)的最小减速(只有向右走和向上走两种情况) #include<stdio.h> #include ...

  2. -ms-,-moz-,-webkit-,-o-含义

    transform:rotate(30deg); //统一标识语句 -ms-transform:rotate(30deg); //-ms代表ie内核识别码 -moz-transform:rotate( ...

  3. mysql出错ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    其他的贴会教你 1.键盘上win+r 2.输入cmd 3.输入net  start mysql 但是还是没用 你可以试试 1.右击开始菜单 2.点击windows PowerShell(i) 3.输入 ...

  4. 16种C语言编译警告(Warning)类型的解决方法

    当编译程序发现程序中某个地方有疑问,可能有问题时就会给出一个警告信息.警告信息可能意味着程序中隐含的大错误,也可能确实没有问题.对于警告的正确处理方式应该是:尽可能地消除之.对于编译程序给出的每个警告 ...

  5. PHP 中 call_user_func 的使用

    call_user_func函数类似于一种特别的调用函数的方法,使用方法如下 第一种情况: function set_max($a,$b) { if($a>$b) echo $a; else e ...

  6. 《代码敲不队》第八次团队作业:Alpha冲刺 第一天

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 代码敲不队 作业学习目标 掌握软件编码实现的工程要求. 第一天 日期:2019/6/15 团队项目 ...

  7. Hadoop(2)_机器信息分布表

    1.分布式环境搭建 采用4台安装Linux环境的机器来构建一个小规模的分布式集群. 图1 集群的架构 其中有一台机器是Master节点,即名称节点,另外三台是Slaver节点,即数据节点.这四台机器彼 ...

  8. spring的几个重要类和接口

    1.datasource接口是javax.sql包下的接口,不是spring,是javax.sql下的 datasource接口有个重要的方法getConnection()方法 Connection ...

  9. JAVA循环迭代中删除或添加集合数据报java.util.ConcurrentModificationException错误

    1.写出下面的输出结果 public class test{ public static void main(String [] args) List<String> list = new ...

  10. arp与免费arp的差别,arp老化

    免费arp:应用场景: case1:PC通过DHCP申请地址.在获取到IP地址后,会发送免费ARP,目的用于探測同一网段时候存在同样的IP地址终端,防止IP冲突. case2:PC的MAC地址发生变化 ...