Python 3.9部分教程 计算 列表 字符串 切片等
off999 2024-09-21 21:03 22 浏览 0 评论
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5
1.6 # division always returns a floating point number 除法总是返回一个浮点数
>>> 17 / 3
5.666666666666667 # classic division returns a float 经典除法返回一个浮点数
>>>
>>> 17 // 3
5 # floor division discards the fractional part 向下取整丢弃小数部分
>>> 17 % 3
2 # the % operator returns the remainder of the
division %操作符返回除法的余数
>>> 5 * 3 + 2
17 # floored quotient * divisor + remainder 商*除数+余数
>>> 5 ** 2 # 5 squared 5的平方
25
>>> 2 ** 7 # 2 to the power of 7 2的7次方
128
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
>>> n # try to access an undefined variable 未定义变量
Traceback (most recent call last):
File "<stdin>", line 1, in <module> 第一行
NameError: name 'n' is not defined 未定义n
>>> 4 * 3.75 - 1 浮点运算
14.0
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 4)
113.0625
>>> round(_,3)
113.062
>>>'spam eggs' # single quotes 单引号
'spam eggs'
>>> 'doesn\'t'
"doesn't" # use \' to escape the single quote... 使用 \' 转义符 把单引号变双引号 …
>>> "doesn't"
"doesn't" # ...or use double quotes instead 或者使用双引号
>>> '"Yes," they said.' 放在单引号内
'"Yes," they said.'
>>> "\"Yes,\" they said." 使用\' 转义符 把双引号变单引号
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.') #输出单引号内的内容
"Isn't," they said.
# \n means newline \ n表示换行符
>>> s = 'First line.\nSecond line.'
>>> s
'First line.\nSecond line.' # without print(), \n is included in the output 没有print() \n包含在输出中
>>> print(s)
First line.
Second line # with print(), \n produces a new line 使用print(), \n产生换行作用
>>> print('C:\some\name')
C:\some
ame # here \n means newline! 这里\n默认会表示换行
>>> print(r'C:\some\name')
C:\some\name # note the r before the quote 注意前面加 r 可以让特殊符号失去作用
''' ''' 或 """ """ \ 可以跨越多行输入
print(''' ''') 或者 print(""" """)
>>> # 3 times 'un', followed by 'ium' 3乘以un,然后加ium
>>> 3 * 'un' + 'ium'
'unununium'
>>> 'Py' 'thon'
'Python'
>>> '大''家''好'
'大家好'
>>> text = ('Put several strings within parentheses '
'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.
如果您要将变量或变量与文字串联,请使用 +
>>> prefix = 'Py'
>>> prefix + 'thon'
'Python'
>>> word = 'Python'
>>> word[0]
'P' # character in position 0 索引第一个字符默认是 0
>>> word[5]
'n' # character in position 5 位置5的字符是n 因第一个字符是0
>>> word[-1]
'n' # last character 最后一个字符 由于 -0 与 0 相同 负指数从 -1 开始
>>> word[-2] # second-last character 倒数第二字符
'o'
>>> word[-6]
'P
>>> word[0:2]
'Py' # characters from position 0 (included) to 2 (excluded) 还可以切片 从0到2但不包括2的所有字符
>>> word[2:5]
'tho # characters from position 2 (included) to 5 (excluded) 从位置2到5但不包括5的所有字符
>>> word[:2]
'Py' # character from the beginning to position 2 (excluded) :前没数索引位置默认是0也就是最开头 从开始到位置2但不包括位置2全部字符
>>> word[4:]
'on' # characters from position 4 (included) to the end :后没数默认是从......一直到最后 从位置4到最后的全部字符
>>> word[-2:]
'on' # characters from the second-last (included) to the end 从倒数第2到最后的全部字符
>>> word[:2] + word[2:]
'Python' 开始到第2位置但不包含2 + 从第2位置到最后
>>> word[:4] + word[4:]
'Python' 开始到第4位置但不包含4 + 从第4位置到最后
# the word only has 6 characters 太大的索引将导致错误 word只有6个字符
>>> word[42]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of rang 索引错误字符串索引超出范围
但是切片就会优雅地处理范围外切片索引
>>> word[4:42]
'on'
>>> word[42:]
''
Python字符串无法更改 它们是不变的。
>>> word[0] = 'J'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
但是可以创建一个新的字符串
>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'
索引位置
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
内置功能 len() 返回字符串长度
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
列表
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
列表也可以索引和切片
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
列表还支持串联等操作
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
与不变的字符串不同,列表是一种可变类型,即可以更改其内容
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here 这里的第四个是错误输入
>>> 4 ** 3
64 # the cube of 4 is 64, not 65! 4的三次方是64不是65
>>> cubes[3] = 64 # replace the wrong value 替换65
>>> cubes
[1, 8, 27, 64, 125]
在列表末尾添加新项目
>>> cubes.append(216) # add the cube of 6 加入6的3次方
>>> cubes.append(7 ** 3) # and the cube of 7 加入7的3次方
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
分配到切片也是可能的,这甚至可以更改列表的大小或完全清除它
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values 替换一些值
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them 现在删除它们
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list 通过将所有元素替换为空列表来清除列表
>>> letters[:] = []
>>> letters
[]
内置功能len()也适用于列表
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
可以嵌套列表(创建包含其他列表的列表)
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0] # x第一个列表a
['a', 'b', 'c']
>>> x[0][1] # x第一个列表 a里的第2个元素
'b'
>>> # Fibonacci series: 斐波纳契数列
... # the sum of two elements defines the next
... a, b = 0, 1 # a b分别赋值0和1
>>> while a < 10:
... print(a) # 注意缩进TAB
... a, b = b, a+b # a赋值等于b ,b赋值等于a+b
...
0
1
1
2
3
5
8
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536
>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
任何非零整数值都是真实的 零是假的 任何长度为非零的都是真的, 空序列是假的
(小于)、(大于)、(等于)、(小于或等于)、(大于或等于)和(不等于)
< > == <= >= !=
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 56
if语句
>>> if x < 0: # 如果
... x = 0 # 注意缩进
... print('Negative changed to zero')
... elif x == 0: 否则
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
for语句
>>> # Measure some strings: 测量一些字符串
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
相关推荐
- python入门到脱坑经典案例—清空列表
-
在Python中,清空列表是一个基础但重要的操作。clear()方法是最直接的方式,但还有其他方法也可以实现相同效果。以下是详细说明:1.使用clear()方法(Python3.3+推荐)...
- python中元组,列表,字典,集合删除项目方式的归纳
-
九三,君子终日乾乾,夕惕若,厉无咎。在使用python过程中会经常遇到这四种集合数据类型,今天就对这四种集合数据类型中删除项目的操作做个总结性的归纳。列表(List)是一种有序和可更改的集合。允许重复...
- Linux 下海量文件删除方法效率对比,最慢的竟然是 rm
-
Linux下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、findwithdelete、rsync、Python、Perl.首先建立50万个文件$testfor...
- 数据结构与算法——链式存储(链表)的插入及删除,
-
持续分享嵌入式技术,操作系统,算法,c语言/python等,欢迎小友关注支持上篇文章我们讲述了链表的基本概念及一些查找遍历的方法,本篇我们主要将一下链表的插入删除操作,以及采用堆栈方式如何创建链表。链...
- Python自动化:openpyxl写入数据,插入删除行列等基础操作
-
importopenpyxlwb=openpyxl.load_workbook("example1.xlsx")sh=wb['Sheet1']写入数据#...
- 在Linux下软件的安装与卸载(linux里的程序的安装与卸载命令)
-
通过apt安装/协助软件apt是AdvancedPackagingTool,是Linux下的一款安装包管理工具可以在终端中方便的安装/卸载/更新软件包命令使用格式:安装软件:sudoapt...
- Python 批量卸载关联包 pip-autoremove
-
pip工具在安装扩展包的时候会自动安装依赖的关联包,但是卸载时只删除单个包,无法卸载关联的包。pip-autoremove就是为了解决卸载关联包的问题。安装方法通过下面的命令安装:pipinsta...
- 用Python在Word文档中插入和删除文本框
-
在当今自动化办公需求日益增长的背景下,通过编程手段动态管理Word文档中的文本框元素已成为提升工作效率的关键技术路径。文本框作为文档排版中灵活的内容容器,既能承载多模态信息(如文字、图像),又可实现独...
- Python 从列表中删除值的多种实用方法详解
-
#Python从列表中删除值的多种实用方法详解在Python编程中,列表(List)是一种常用的数据结构,具有动态可变的特性。当我们需要从列表中删除元素时,根据不同的场景(如按值删除、按索引删除、...
- Python 中的前缀删除操作全指南(python删除前导0)
-
1.字符串前缀删除1.1使用内置方法Python提供了几种内置方法来处理字符串前缀的删除:#1.使用removeprefix()方法(Python3.9+)text="...
- 每天学点Python知识:如何删除空白
-
在Python中,删除空白可以分为几种不同的情况,常见的是针对字符串或列表中空白字符的处理。一、删除字符串中的空白1.删除字符串两端的空白(空格、\t、\n等)使用.strip()方法:s...
- Linux系统自带Python2&yum的卸载及重装
-
写在前面事情的起因是我昨天在测试Linux安装Python3的shell脚本时,需要卸载Python3重新安装一遍。但是通过如下命令卸载python3时,少写了个3,不小心将系统自带的python2也...
- 如何使用Python将多个excel文件数据快速汇总?
-
在数据分析和处理的过程中,Excel文件是我们经常会遇到的数据格式之一。本文将通过一个具体的示例,展示如何使用Python和Pandas库来读取、合并和处理多个Excel文件的数据,并最终生成一个包含...
- 【第三弹】用Python实现Excel的vlookup功能
-
今天继续用pandas实现Excel的vlookup功能,假设我们的2个表长成这样:我们希望把Sheet2的部门匹在Sheet1的最后一列。话不多说,先上代码:importpandasaspd...
- python中pandas读取excel单列及连续多列数据
-
案例:想获取test.xls中C列、H列以后(当H列后列数未知时)的所有数据。importpandasaspdfile_name=r'D:\test.xls'#表格绝对...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python人脸识别 (54)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)