python字符串比较

python小数据池,代码块知识

穿精又带淫゛_ 提交于 2020-01-11 01:47:37
一、什么是代码块? 根据官网提示我们可以获知: A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file ( a file given as standard input to the interpreter or specified as a command line argument to the interpreter ) is a code block. A script command ( a command specified on the interpreter command lin with the '-c' option ) is a code block. The string argument passed to the built-in function eval() and exec() is a code

python基础之小数据池

ε祈祈猫儿з 提交于 2020-01-10 13:48:43
一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id()内置函数去查询一个数据的内存地址: name = '太白' print(id(name)) # 1585831283968 那么 is 是什么? == 又是什么? == 是比较的两边的数值是否相等,而 is 是比较的两边的内存地址是否相等。 如果内存地址相等,那么这两边其实是指向同一个内存地址。 可以说如果内存地址相同,那么值肯定相同,但是如果值相同,内存地址不一定相同。 二,代码块。 根据官网提示我们可以获知: 根据提示我们从官方文档找到了这样的说法: A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a

python基础二

给你一囗甜甜゛ 提交于 2020-01-10 11:05:27
一. 格式化输出 现有一练习需求,问用户的姓名、年龄、工作、爱好 ,然后打印成以下格式 ------------ info of 太白金星 ----------- Name : 太白金星 Age : 22 job : Teacher Hobbie : girl ------------- end ---------------- 你怎么实现呢?你会发现,用字符拼接的方式还难实现这种格式的输出,所以一起来学一下新姿势 只需要把要打印的格式先准备好, 由于里面的 一些信息是需要用户输入的,你没办法预设知道,因此可以先放置个占位符,再把字符串里的占位符与外部的变量做个映射关系就好啦 name = input ( "Name:" ) age = input ( "Age:" ) job = input ( "Job:" ) hobbie = input ( "Hobbie:" ) info = ''' ------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name Name : %s #代表 name Age : %s #代表 age job : %s #代表 job Hobbie: %s #代表 hobbie ------------- end ----------------- ''' %( name ,

Python去掉字符串中空格的方法

扶醉桌前 提交于 2020-01-08 10:26:06
、去空格及特殊符号 复制代码 代码如下: s.strip().lstrip().rstrip(',') 2、复制字符串 复制代码 代码如下: #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 3、连接字符串 复制代码 代码如下: #strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print sStr1 4、查找字符 复制代码 代码如下: #strchr(sStr1,sStr2) # < 0 为未找到 sStr1 = 'strchr' sStr2 = 's' nPos = sStr1.index(sStr2) print nPos 5、比较字符串 复制代码 代码如下: #strcmp(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'strch' print cmp(sStr1,sStr2) 6、扫描字符串是否包含指定的字符 复制代码 代码如下: #strspn(sStr1,sStr2) sStr1 = '12345678' sStr2 = '456' #sStr1 and chars both in sStr1 and sStr2 print len

这26个为什么,让初学者理解Python更简单!

柔情痞子 提交于 2020-01-07 04:13:50
为什么Python使用缩进来分组语句? 为什么简单的算术运算得到奇怪的结果? 为什么浮点计算不准确? 为什么Python字符串是不可变的? 为什么必须在方法定义和调用中显式使用“self”? 为什么不能在表达式中赋值? 为什么Python对某些功能(例如list.index())使用方法来实现,而其他功能(例如len(List))使用函数实现? 为什么 join()是一个字符串方法而不是列表或元组方法? 异常有多快? 为什么Python中没有switch或case语句? 难道不能在解释器中模拟线程,而非得依赖特定于操作系统的线程实现吗? 为什么lambda表达式不能包含语句? 可以将Python编译为机器代码,C或其他语言吗? Python如何管理内存? 为什么CPython不使用更传统的垃圾回收方案? CPython退出时为什么不释放所有内存? 为什么有单独的元组和列表数据类型? 列表是如何在CPython中实现的? 字典是如何在CPython中实现的? 为什么字典key必须是不可变的? 为什么 list.sort() 没有返回排序列表? 如何在Python中指定和实施接口规范? 为什么没有goto? 为什么原始字符串(r-strings)不能以反斜杠结尾? 为什么Python没有属性赋值的“with”语句? 为什么 if/while/def/class语句需要冒号?

python基础之小数据池

对着背影说爱祢 提交于 2020-01-06 03:48:03
一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id()内置函数去查询一个数据的内存地址: name = '太白' print(id(name)) # 1585831283968 那么 is 是什么? == 又是什么? == 是比较的两边的数值是否相等,而 is 是比较的两边的内存地址是否相等。 如果内存地址相等,那么这两边其实是指向同一个内存地址。 可以说如果内存地址相同,那么值肯定相同,但是如果值相同,内存地址不一定相同。 二,代码块。 根据官网提示我们可以获知: 根据提示我们从官方文档找到了这样的说法: A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a

Python-一些实用的函数

梦想与她 提交于 2020-01-06 01:02:14
一,返回值为bool类型的函数 1.any()函数 any(iterable)->bool 当迭代器中有一个是Ture,则返回Ture;若interable=NUll,则返回False. >>> any([1,0]) True >>> any([0,0]) False >>> any([]) False >>> any([1,0,0]) True 注:在Python中, False相当于:0,[], (), {}, 0.0 , " ", ' ' . 应用:在一颗二叉树中,找出每一层中的最大元素(leetcode515)。 Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3,9] #类节点的定义class node(self) : def __init__(self,data) self.val=data self.right=NULL self.left=NULLclass Solution(object): def largestValues(self, root): maxlist=[] row=[root] while any(row): maxlist.append(max[node.val for node in row]) row=[kid for node in row for kid in node.left,node.right)

Python基础学习-->二 基础语法

北战南征 提交于 2020-01-03 23:10:29
二.基础语法   2.1 编码   默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串。 当然你也可以为源码文件指定不同的编码:    #-*- conding:utf-8 -*-   2.2 标识符 第一个字符必须是字母表中字母或下划线'_'。 标识符的其他的部分有字母、数字和下划线组成。 标识符对大小写敏感。   在Python 3中,非-ASCII 标识符也是允许的了。   2.3 Python保留字     保留字即关键字,我们不能把它们用作任何标识符名称。Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字:   import keyword   print(keyword.kwlist) ['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',

第四天Python学习记录

北慕城南 提交于 2020-01-03 07:21:13
不加引号的字符串被认为是变量 字符串只能与字符串拼接,不能与数字拼接 输入姓名、年龄、工作、家乡,能够格式化输出,里面用到%s 占位符,%s表示字符串,%d表示数字,%f表示浮点数 ,%s是万能的, 运算包括 算数运算 加减乘除等 比较运算 大于小于 赋值运算 和逻辑运算 and or not 单分支 满足条件才执行 双分支 哪个满足执行那个 网站登录用到双分支 多分支语句 猜年龄游戏 匹配成绩小程序 while循环 打印0到100 ,只打印偶数 , 来源: https://www.cnblogs.com/xudachen/p/8278100.html

Python学习笔记(二)

淺唱寂寞╮ 提交于 2020-01-02 17:35:23
三.基本数据类型 Python 中的变量不需要声明。 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建 。 在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型。 等号(=)用来给变量赋值。 等号(=)运算符左边是一个变量名,等号(=)运算符右边是存储在变量中的值。例如: 实例(Python 3.0+) #!/usr/bin/python3 counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = " runoob " # 字符串 print ( counter ) print ( miles ) print ( name ) Python赋值比较特殊的是支持以下形式 : a,b,c=1,"111",10 Python3 中有六个标准的数据类型: Number(数字) int、float、bool、complex(复数),python3 没有Long,int就是长整型。 注意: 在 Python2 中是没有布尔型的,它用数字 0 表示 False,用 1 表示 True。到 Python3 中,把 True 和 False 定义成关键字了,但它们的值还是 1 和 0,它们可以和数字相加。 String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary