python基础_1
来源:互联网 发布:刀锋洗眼 知乎 编辑:程序博客网 时间:2024/06/06 00:22
列表,元组,集合,字典
列表——[] 可读可写,可以对列里元素修改
元组——() 只读,不可以对列里元素修改
1.列表
in:
#列表students = ["jack","robin","lina",3]print (students[-2])#这儿索引为负数-2,表示倒数第二个元素
out:
lina
in:
students[3] = "python"print (students[3])print (students)
out:
python['jack', 'robin', 'lina', 'python']
in:
len(students)#求列表长度
out:
4
in:
students[1:3]#按区间(切片)查找元素
out:
['robin', 'lina']
in:
students = ["jack1","jack2","robin1","robin2","lina1","lina2",3]students[1:5:2]#按区间(切片)查找元素,并设定步长为2
out:
['jack2', 'robin2']
in:
students.append('helloworld')#添加元素students
out:
['jack1', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3, 'helloworld']
in:
students.pop(7)#删除元素——按索引students
out:
['jack1', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3]
in:
students.insert(1,'helloworld2')#插入元素——按索引students
out:
['jack1', 'helloworld2', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3]
in:
scores = [10,20,30]students.insert(1,scores)students
out:
['jack1', [10, 20, 30], [10, 20, 30], [10, 20, 30], [10, 20, 30], [10, 20, 30], [10, 20, 30], [10, 20, 30], 'helloworld2', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3]
in:
l=studentsl
2.元组
in:
students = ("jack1","jack2","robin1","robin2","lina1","lina2",3)print(students[2])
out:
robin1
in:
students.append(score)#添加元素students
in:
teachers = ("jack1","jack2","robin1","robin2","lina1","lina2",scores)teachers
out:
('jack1', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', [10, 20, 30])
3.集合
主要有2个功能: 1.进行集合操作 2.消除重复元素 集合的格式是:set(元素)
in:
a = set("abcnmaaaaggsng")print('a=',a)b = set("cdfm")print('b=',b)#交集x = a & bprint('x=',x)#并集y = a|bprint('y=',y)#差集z = a - bprint('z=',z)#去除重复元素new = set(a)print('z=',z)
out:
a= {'g', 'b', 'm', 's', 'a', 'c', 'n'}b= {'d', 'm', 'f', 'c'}x= {'m', 'c'}y= {'g', 'b', 'm', 'd', 's', 'f', 'a', 'c', 'n'}z= {'g', 'b', 'n', 's', 'a'}z= {'g', 'b', 'n', 's', 'a'}
in:
strs = 'abc'strs[1]
out:
'b'
in:
L = {'a','b','c','c'}L
out:
{'a', 'b', 'c'}
in:
set_L = set(L)list_L = list(set_L)list_L
out:
['b', 'a', 'c']
in:
tuple_L = tuple(set_L)tuple_L
out:
('b', 'a', 'c')
in:
list(tuple_L)
out:
['b', 'a', 'c']
4.字典
python中字典也叫关联数组,用{}括起来。每个元素是一个k-v对
in:
#字典zidian = {"name":"robin","home":"yunnan"}print(zidian["home"])#添加字典里的项目zidian["like"] = "music"print(zidian["name"])print (zidian["like"])
out:
yunnanrobinmusic
in:
zidian
out:
{'home': 'yunnan', 'like': 'music', 'name': 'robin'}
5.常用python关键字
in:
#常用python关键字#查看一下有哪些关键字import keywordprint(keyword.kwlist)
out:
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
6.python运算符与表达式
算术运算符
in:
#"*":两个数相乘或字符串重复a = 4 * 7print (a)b = "hello "*3print (b)
out:
28hello hello hello
in:
#"**":求幂运算a2 = 2 ** 3 #相当于2的三次方(幂),就是2*2*2print (a2)
out:
8
in:
#"//":除法运算,返回其商的整数部分,舍掉余数a3 = 11 // 3 print (a3)a4 = 11.0 // 3 print (a4)
out:
33.0
in:
#"%":除法运算,返回其商的余数部分,舍掉商a5 = 11 % 3 print (a5)
out:
2
range
in:
range(0,10,2)alist = range(10)[x*x for x in alist]
out:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
in:
[x*x for x in alist if x%3]
out:
[1, 4, 16, 25, 49, 64]
截图
阅读全文
0 0
- Python基础学习_1
- Python基础_1
- python基础_1
- python基础_1
- hibernate基础_1
- 多项式基础_1.2
- JAVA学习_1基础
- 数学基础_1: 级数
- 汇编基础代码_1
- servlet基础_1
- Andoid基础学习_1
- JAVA基础知识点_1
- c++_1: 基础问题
- ReactJs基础_1
- 数据库_1: MySQL:基础
- C基础_1
- 基础_1 2016.4.16
- 计算几何基础_1
- Shader学习(一)
- 【微信公众号开发】自我学习第七章:公众号自定义菜单栏
- 调试操作系统-bochs使用入门
- Java元组Tuple介绍与使用
- hellochart详细讲解(二)饼状图
- python基础_1
- Codeforces Beta Round #7 D. Palindrome Degree —— hash
- 浅谈Three.js源码-render之WebGLShader.js
- NuttX实时操作系统
- jboss容器,web整站https环境下使用(spring mvc)redirect重定向到http协议问题
- 剑指offer | 训练题51:正则表达式匹配
- MOS管
- 屏幕记录机BB FlashBack v5.26.0发布丨附下载
- 110. Balanced Binary Tree验证平衡二叉树