Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
lesson7_7
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bf28723c
authored
Feb 17, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
5e830b75
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
289 additions
and
21 deletions
12.py
2.py
678.py
diy4.py
12.py
0 → 100644
View file @
bf28723c
a
=
'erhguherugoirier'
print
(
a
.
find
(
'g'
))
print
(
a
.
count
(
'e'
))
print
(
a
.
replace
(
'g'
,
'G'
,
1
))
\ No newline at end of file
2.py
0 → 100644
View file @
bf28723c
aa
=
int
(
input
(
"长:"
))
bb
=
int
(
input
(
"宽:"
))
S
=
aa
*
bb
C
=
(
aa
+
bb
)
*
2
print
(
'周长是'
,
C
,
'面积是'
,
S
)
678.py
0 → 100644
View file @
bf28723c
a
=
'acdefg'
b
=
list
(
a
)
b
.
insert
(
1
,
'b'
)
b
=
''
.
join
(
b
)
print
(
b
)
diy4.py
View file @
bf28723c
import
random
python3
# 私钥
首页
HTML
CSS
JAVASCRIPT
VUE
BOOTSTRAP
NODEJS
JQUERY
PYTHON
JAVA
C
C
++
C
# GO SQL LINUX 本地书签
key
=
"abcdefgh使用编程实现位移替换加密,制作密码机关真是太好玩了哈哈!"
Python
3
教程
# 要加密语句
Python3
教程
message
=
"诺依,周末一起去看动漫展吧!"
Python3
简介
Python3
环境搭建
# 请使用私钥key,对message进行加密
Python3
VScode
f
=
input
(
"gan:"
)
Python3
基础语法
e
=
""
Python3
基本数据类型
for
i
in
message
:
Python3
数据类型转换
a
=
i
Python3
推导式
b
=
random
.
choice
(
key
)
Python3
解释器
c
=
random
.
choice
(
key
)
Python3
注释
d
=
a
+
b
+
c
Python3
运算符
e
=
e
+
d
Python3
数字
(
Number
)
e
=
list
(
e
)
Python3
字符串
e
.
insert
(
len
(
e
),
f
)
Python3
列表
g
=
""
.
join
(
e
)
Python3
元组
print
(
g
)
Python3
字典
\ No newline at end of file
Python3
集合
Python3
编程第一步
Python3
条件控制
Python3
循环语句
Python3
迭代器与生成器
Python3
函数
Python3
数据结构
Python3
模块
Python3
输入和输出
Python3
File
Python3
OS
Python3
错误和异常
Python3
面向对象
Python3
命名空间
/
作用域
Python3
标准库概览
Python3
实例
Python
测验
Python3
高级教程
Python3
正则表达式
Python3
CGI
编程
Python3
MySQL
(
mysql
-
connector
)
Python3
MySQL
(
PyMySQL
)
Python3
网络编程
Python3
SMTP
发送邮件
Python3
多线程
Python3
XML
解析
Python3
JSON
Python3
日期和时间
Python3
内置函数
Python3
MongoDB
Python3
urllib
Python
uWSGI
安装配置
Python3
pip
Python
移除列表中重复的元素
Python3
列表
Python3
字典
Python3
元组
Python
的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号
(
)
,列表使用方括号
[
]
。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
实例
(
Python
3.0
+
)
>>>
tup1
=
(
'Google'
,
'Runoob'
,
1997
,
2000
)
>>>
tup2
=
(
1
,
2
,
3
,
4
,
5
)
>>>
tup3
=
"a"
,
"b"
,
"c"
,
"d"
# 不需要括号也可以
>>>
type
(
tup3
)
<
class
'
tuple
'>
创建空元组
tup1 = ()
元组中只包含一个元素时,需要在元素后面添加逗号 , ,否则括号会被当作运算符使用:
实例(Python 3.0+)
>>> tup1 = (50)
>>> type(tup1) # 不加逗号,类型为整型
<class '
int
'>
>>> tup1 = (50,)
>>> type(tup1) # 加上逗号,类型为元组
<class '
tuple
'>
元组与字符串类似,下标索引从 0 开始,可以进行截取,组合等。
访问元组
元组可以使用下标索引来访问元组中的值,如下实例:
实例(Python 3.0+)
#!/usr/bin/python3
tup1 = ('
Google
', '
Runoob
', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
以上实例输出结果:
tup1[0]: Google
tup2[1:5]: (2, 3, 4, 5)
修改元组
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:
实例(Python 3.0+)
#!/usr/bin/python3
tup1 = (12, 34.56)
tup2 = ('
abc
', '
xyz
')
# 以下修改元组元素操作是非法的。
# tup1[0] = 100
# 创建一个新的元组
tup3 = tup1 + tup2
print (tup3)
以上实例输出结果:
(12, 34.56, '
abc
', '
xyz
')
删除元组
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例:
实例(Python 3.0+)
#!/usr/bin/python3
tup = ('
Google
', '
Runoob
', 1997, 2000)
print (tup)
del tup
print ("删除后的元组 tup : ")
print (tup)
以上实例元组被删除后,输出变量会有异常信息,输出如下所示:
删除后的元组 tup :
Traceback (most recent call last):
File "test.py", line 8, in <module>
print (tup)
NameError: name '
tup
' is not defined
元组运算符
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
Python 表达式 结果 描述
len((1, 2, 3)) 3 计算元素个数
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) 连接
('
Hi
!
',) * 4 ('
Hi
!
', '
Hi
!
', '
Hi
!
', '
Hi
!
') 复制
3 in (1, 2, 3) True 元素是否存在
for x in (1, 2, 3):
print (x, end=" ") 1 2 3 迭代
元组索引,截取
因为元组也是一个序列,所以我们可以访问元组中的指定位置的元素,也可以截取索引中的一段元素,如下所示:
元组:
tup = ('
Google
', '
Runoob
', '
Taobao
', '
Wiki
', '
Weibo
','
Weixin
')
Python 表达式 结果 描述
tup[1] '
Runoob
' 读取第二个元素
tup[-2] '
Weibo
' 反向读取,读取倒数第二个元素
tup[1:] ('
Runoob
', '
Taobao
', '
Wiki
', '
Weibo
', '
Weixin
') 截取元素,从第二个开始后的所有元素。
tup[1:4] ('
Runoob
', '
Taobao
', '
Wiki
') 截取元素,从第二个开始到第四个元素(索引为 3)。
运行实例如下:
实例
>>> tup = ('
Google
', '
Runoob
', '
Taobao
', '
Wiki
', '
Weibo
','
Weixin
')
>>> tup[1]
'
Runoob
'
>>> tup[-2]
'
Weibo
'
>>> tup[1:]
('
Runoob
', '
Taobao
', '
Wiki
', '
Weibo
', '
Weixin
')
>>> tup[1:4]
('
Runoob
', '
Taobao
', '
Wiki
')
>>>
元组内置函数
Python元组包含了以下内置函数
序号 方法及描述 实例
1 len(tuple)
计算元组元素个数。
>>> tuple1 = ('
Google
', '
Runoob
', '
Taobao
')
>>> len(tuple1)
3
>>>
2 max(tuple)
返回元组中元素最大值。
>>> tuple2 = ('
5
', '
4
', '
8
')
>>> max(tuple2)
'
8
'
>>>
3 min(tuple)
返回元组中元素最小值。
>>> tuple2 = ('
5
', '
4
', '
8
')
>>> min(tuple2)
'
4
'
>>>
4 tuple(iterable)
将可迭代系列转换为元组。
>>> list1= ['
Google
', '
Taobao
', '
Runoob
', '
Baidu
']
>>> tuple1=tuple(list1)
>>> tuple1
('
Google
', '
Taobao
', '
Runoob
', '
Baidu
')
关于元组是不可变的
所谓元组的不可变指的是元组所指向的内存中的内容不可变。
>>> tup = ('r', 'u', '
n
', '
o
', '
o
', '
b
')
>>> tup[0] = '
g
' # 不支持修改元素
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '
tuple
' object does not support item assignment
>>> id(tup) # 查看内存地址
4440687904
>>> tup = (1,2,3)
>>> id(tup)
4441088800 # 内存地址不一样了
从以上实例可以看出,重新赋值的元组 tup,绑定到新的对象了,不是修改了原来的对象。
Python3 列表Python3 字典
11 篇笔记 写笔记
分类导航
HTML / CSS
JavaScript
服务端
数据库
数据分析
移动端
XML 教程
ASP.NET
Web Service
开发工具
网站建设
Advertisement
反馈/建议反馈/建议
在线实例
·HTML 实例
·CSS 实例
·JavaScript 实例
·Ajax 实例
·jQuery 实例
·XML 实例
·Java 实例
字符集&工具
· HTML 字符集设置
· HTML ASCII 字符集
· HTML ISO-8859-1
· PNG/JPEG 图片压缩
· HTML 拾色器
· JSON 格式化工具
最新更新
· HTML DOM matche...
· HTML DOM firstE...
· HTML DOM lastEl...
· Python 推导式
· JavaScript 静态...
· JavaScript 类继承
· JavaScript 类(c...
站点信息
· 意见反馈
· 免责声明
· 关于我们
· 文章归档
关注微信
Copyright © 2013-2022 菜鸟教程 runoob.com All Rights Reserved. 备案号:闽ICP备15012807号-1
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment