Python在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
python临时测试代码 发布于:2024-07-25 16:15 武侠文字游戏 发布于:2024-07-24 16:15 超级大乐透选号器终极版 发布于:2024-07-23 23:22 json 绘制图 发布于:2024-07-19 21:13 JointQuant WorkFrame Debug 发布于:2024-07-19 11:32 JointQuant FrameWork 发布于:2024-07-19 10:24 电影评分推荐 发布于:2024-07-18 13:45 sfdg wserfg 发布于:2024-07-18 09:57 JointQuant格式 发布于:2024-07-17 16:37 π生成代码 发布于:2024-07-16 14:52 rsa p高位攻击 发布于:2024-07-15 23:02 汇率测试脚本 发布于:2024-07-15 15:22 1. 基础代码“hello,world”输出 2. 循环结构+字符串切片重组应用 3. 条件循环(以加法为例)应用 4. for循环+if语句对字符串进行判定 5. 次数循环一到二十的质数寻找与判断 发布于:2024-07-15 13:38 python代码测试 发布于:2024-07-10 09:25 import os # 定义要对比的照片文件路径 file1 = "path/to/file1.jpg" file2 = "path/to/file2.jpg" # 获取文件大小 file_size1 = os.path.getsize(file1) file_size2 = os.path.getsize(file2) # 获取文件修改时间 modification_time1 = os.path.getmtime(file1) modification_time2 = os.path.getmtime(file2) # 比较文件大小和修改时间 if file_size1 == file_size2 and modification_time1 == modification_time2: print("文件属性相同") else: print("文件属性不同") 发布于:2024-07-09 11:07 毒死手机上啥时候 发布于:2024-07-07 16:48 input practice 发布于:2024-07-06 22:59 print practice 发布于:2024-07-06 22:32 水某人超绝技术左右 发布于:2024-06-29 15:51 画一个爱心 发布于:2024-06-28 11:33 身高体重计算 发布于:2024-06-23 19:42 停车撒打算萨达 发布于:2024-06-20 21:37 多线程模拟银行操做 发布于:2024-06-19 12:31 在一组数据中请求等于目标数的组合 发布于:2024-06-19 09:18 就是一个自用功能 发布于:2024-06-18 16:44 模糊加密登录 发布于:2024-06-18 14:49 大乐透选号器 发布于:2024-06-18 03:28 以1个小时为频率生成cpu、内存、tcp的csv随机数据 发布于:2024-06-16 11:07 实验5 python 发布于:2024-06-12 20:50 python 获取a标签 发布于:2024-06-12 00:27 判断两个数差是否为偶数 发布于:2024-06-10 18:25 pip install pandas 发布于:2024-06-10 18:11 调试程序代码 发布于:2024-06-08 16:30 成品入库时间生成 发布于:2024-07-13 09:32 TensorFlow使用测试 发布于:2024-06-07 11:11 mobaxterm密码解密 发布于:2024-06-06 11:22 找年龄大的 发布于:2024-06-05 17:05 哈哈哈,真操蛋 发布于:2024-06-05 00:15 机器人代码 发布于:2024-06-04 13:43 Jaden wu Menu 发布于:2024-06-03 19:42 文字替换术 发布于:2024-06-07 02:07 abs取绝对值 发布于:2024-06-02 09:42 计算解冻一定时间后的温度 发布于:2024-06-01 17:25 购物清单列表 发布于:2024-06-01 10:23 有效前沿及夏普比率 发布于:2024-06-01 05:44 BMI计算 发布于:2024-05-30 17:32 字符串逆序输出 发布于:2024-05-29 17:34 人员信息输出 发布于:2024-05-29 17:34 指定月份平均访客量 发布于:2024-05-29 17:34 列表中位数 发布于:2024-05-29 17:33 [更多]
显示目录

字符串



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

Python 字符串

除了数字,Python也能操作字符串。字符串有几种表达方式,可以使用单引号或双引号括起来:

>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

Python中使用反斜杠转义引号和其它特殊字符来准确地表示。

如果字符串包含有单引号但不含双引号,则字符串会用双引号括起来,否则用单引号括起来。对于这样的输入字符串,print() 函数会产生更易读的输出。

跨行的字面字符串可用以下几种方法表示。使用续行符,即在每行最后一个字符后使用反斜线来说明下一行是上一行逻辑上的延续:

以下使用 \n 来添加新行:

>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.'  # \n 意味着新行
>>> s  # 不使用 print(), \n 包含在输出中
'First line.\nSecond line.'
>>> print(s)  # 使用 print(), \n 输出一个新行
First line.
Second line.

以下使用 反斜线(\) 来续行:

hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."

print(hello)

注意,其中的换行符仍然要使用 \n 表示——反斜杠后的换行符被丢弃了。以上例子将如下输出:

This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is significant.

或者,字符串可以被 """ (三个双引号)或者 ''' (三个单引号)括起来。使用三引号时,换行符不需要转义,它们会包含在字符串中。以下的例子使用了一个转义符,避免在最开始产生一个不需要的空行。

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

其输出如下:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

如果我们使用"原始"字符串,那么 \n 不会被转换成换行,行末的的反斜杠,以及源码中的换行符,都将作为数据包含在字符串内。例如:

hello = r"This is a rather long string containing\n\
several lines of text much as you would do in C."

print(hello)

将会输出:

This is a rather long string containing\n\
several lines of text much as you would do in C.

字符串可以使用 + 运算符串连接在一起,或者用 * 运算符重复:

>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
''

两个紧邻的字面字符串将自动被串连;上例的第一行也可以写成 word = 'Help' 'A' ;这样的操作只在两个字面值间有效,不能随意用于字符串表达式中:

>>> 'str' 'ing'                 
#  <- string="">>> 'str'.strip() + 'ing'  
#  <- string="">>> 'str'.strip() 'ing'    
#  <-  这样操作错误   File "", line 1, in ?
    'str'.strip() 'ing'
                      ^
SyntaxError: invalid syntax

字符串可以被索引;就像 C 语言一样,字符串的第一个字符的索引为 0。没有单独的字符类型;一个字符就是长度为一的字符串。就像Icon编程语言一样,子字符串可以使用分切符来指定:用冒号分隔的两个索引。

>>> word[4]
'A'
>>> word[0:2]
'Hl'
>>> word[2:4]
'ep'

默认的分切索引很有用:默认的第一个索引为零,第二个索引默认为字符串可以被分切的长度。

>>> word[:2]    # 前两个字符
'He'
>>> word[2:]    # 除了前两个字符之外,其后的所有字符
'lpA'

不同于C字符串的是,Python字符串不能被改变。向一个索引位置赋值会导致错误:

>>> word[0] = 'x'
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: 'str' object does not support slice assignment

然而,用组合内容的方法来创建新的字符串是简单高效的:

>>> 'x' + word[1:]
'xelpA'
>>> 'Splat' + word[4]
'SplatA'
在分切操作字符串时,有一个很有用的规律: s[:i] + s[i:] 等于 s.

>>> word[:2] + word[2:]
'HelpA'
>>> word[:3] + word[3:]
'HelpA'

对于有偏差的分切索引的处理方式也很优雅:一个过大的索引将被字符串的大小取代,上限值小于下限值将返回一个空字符串。

>>> word[1:100]
'elpA'
>>> word[10:]

>>> word[2:1]

在索引中可以使用负数,这将会从右往左计数。例如:

>>> word[-1]     # 最后一个字符
'A'
>>> word[-2]     # 倒数第二个字符
'p'
>>> word[-2:]    # 最后两个字符
'pA'
>>> word[:-2]    # 除了最后两个字符之外,其前面的所有字符
'Hel'
但要注意, -0 和 0 完全一样,所以 -0 不会从右开始计数!

>>> word[-0]     # (既然 -0 等于 0)
'H'

超出范围的负数索引会被截去多余部分,但不要尝试在一个单元素索引(非分切索引)里使用:

>>> word[-100:]
'HelpA'
>>> word[-10]    # 错误
Traceback (most recent call last):
  File "", line 1, in ?
IndexError: string index out of range

有一个方法可以让您记住分切索引的工作方式,想像索引是指向字符之间,第一个字符左边的数字是 0。接着,有n个字符的字符串最后一个字符的右边是索引n,例如:

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

第一行的数字 0...5 给出了字符串中索引的位置;第二行给出了相应的负数索引。分切部分从 i 到 j 分别由在边缘被标记为 i 和 j 的全部字符组成。

对于非负数分切部分,如果索引都在有效范围内,分切部分的长度就是索

Python 条件控制


if 语句

Python中if语句的一般形式如下所示:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句,如果 "condition_1" 为False,将判断 "condition_2",如果"condition_2" 为 True 将执行 "statement_block_2" 块语句,如果 "condition_2" 为False,将执行"statement_block_3"块语句。

Python中用elif代替了else if,所以if语句的关键字为:if – elif – else。

注意:

  • 1、每个条件后面要使用冒号(:),表示接下来是满足条件后要执行的语句块。
  • 2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
  • 3、在Python中没有switch – case语句。

实例

以下实例演示了狗的年龄计算判断:

age = int(input("Age of the dog: "))
print()
if age < 0:  print("This can hardly be true!") elif age == 1:  
print("about 14 human years") elif age == 2:  
print("about 22 human years") elif age > 2:
  human = 22 + (age -2)*5
   print("Human years: ", human)

### 
input('press Return>')

将以上脚本保存在dog.py文件中,并执行该脚本:

python dog.py
Age of the dog: 1

about 14 human years

以下为if中常用的操作运算符:

操作符 描述
< 小于
<= 小于或等于
> 大于
>= 大于或等于
== 等于,比较对象是否相等
!= 不等于

实例

# 程序演示了 == 操作符
# 使用数字
print(5 == 6)
# 使用变量
x = 5
y = 8
print(x == y)

以上实例输出结果:

False
False

high_low.py文件:

#!/usr/bin/python3 
# 该实例演示了数字猜谜游戏
number = 7
guess = -1
print("Guess the number!")
while guess != number:
    guess = int(input("Is it... "))

    if guess == number:
        print("Hooray! You guessed it right!")
    elif guess < number:         print("It's bigger...")     elif guess > number:
        print("It's not so big.")

引的差值。例如, word[1:3] 的长度是2。

内置的函数 len() 用于返回一个字符串的长度:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
由JSRUN为你提供的Python在线运行、在线编译工具
        JSRUN提供的Python 在线运行,Python 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout