python全天课视频(2)

2024-02-23 07:58
文章标签 python 视频 全天

本文主要是介绍python全天课视频(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

6、编码的规范

适当添加空行使代码布局更为优雅、合理
编写函数:
函数设计要尽量短小,嵌套层不宜过深
函数声明应该做到合理、简单、易于使用,函数名能够正确反映函数
大体功能,参数设计应简洁明了,参数个数不宜过度
函数参数设计应考虑向下兼容;
一个函数只做一件事,尽量保证函数语句粒度的一致性;
函数命名使用小写,比如:upper_letter(),analyze_log();

7、二进制、八进制和十六进制
>>> oct(9)
'0o11'
>>> 0o11
9
>>> 0o01
1
>>> bin(10)
'0b1010'
>>> hex(20)
'0x14'
>>> hex(30)
'0x1e'
>>> hex(15)
'0xf'
>>>

在这里插入图片描述

8、运算符
>>> 2/1
2.0
>>> 3 %2
1
>>> 2//1
2
>>> 1/2
0.5
>>> 1//2
0
>>> import math
>>> math.floor(1.9/2)
0
>>> math.ceil(1.9/2)
1
>>> math.round(0.5)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: module 'math' has no attribute 'round'
>>> round(0.5)
0
>>> round(0.51)
1
>>> round(0.511111)
1
>>> round(0.51111,2)
0.51

在这里插入图片描述

>>> def divmod_new(a,b):
...     c=a//b
...     d=a%b
...     return c,d
...
>>> divmod_new(5,2)
(2, 1)

在这里插入图片描述

>>> 2**4
16
>>> bin(1)
'0b1'
>>> bin(3)
'0b11'
>>> bin(3)[2:]
'11'
>>> bin(3)[2:].zfill(len(bin(3)))
'0011'
>>> bin(3)[2:].zfill(len(bin(8)))
'000011'
>>> bin(3)[2:].zfill(8)
'00000011'
>>> help("1".zfill)
Help on built-in function zfill:zfill(...) method of builtins.str instanceS.zfill(width) -> strPad a numeric string S with zeros on the left, to fill a field
of the specified width. The string S is never truncated.

在这里插入图片描述

>>> int(bin(3),base=16)
2833
>>> bin(3)
'0b11'
>>> int('15',base=16)
21
>>> int('15',base=8)
13

在这里插入图片描述

9、and\or\not
>>> True and True
True
>>> True or False
True
>>> True or True
True
>>> not True
False
>>> not False
True
10、在python里面哪些值是False
>>> 3&3
3
>>> 11
11
>>> 11 11File "<stdin>", line 111 11^
SyntaxError: invalid syntax
>>> 11&11
11
>>> 11&10
10
>>> 3&2
2
>>> 2|1
3
>>> 11|01File "<stdin>", line 111|01^
SyntaxError: invalid token
>>> 11|1
11
>>> 2^1
3
>>> ~2
-3
11、比较关系运算符
>>> 1>1
False
>>> 2>1
True
>>> 2>=1
True
>>> 2==2
True
>>> 2<=1
False
>>> 2!=1
True

在这里插入图片描述

12、赋值运算符
>>> a=1
>>> a++1
2
>>> a+=1
>>> a
2
>>> a=a+1
>>> a
3
>>> "+".join(["a"+"b"])
'ab'
>>> a//=1
>>> a
3

在这里插入图片描述

13、成员运算符
>>> "a" in "abc"
True
>>> "a" not in "abc"
False
>>> "a" not in ["a","b"]
False
>>> "a" not in {"a":1,"b":2}
False
>>> "a"  in {"a":1,"b":2}
True
>>> "a" in ("a","b")
True
>>> "a"  in set(["a","b"])
True

在这里插入图片描述

14、身份运算符
>>> 1 is 1
True
>>> 1000 is 1000
True
>>> 1001 is 1000
False
>>> id(1001)
1983555282032
>>> id(1000)
1983555282032
>>> a=1
>>> b=1
>>> a is b
True
>>> a=1000
>>> b=1000
>>> a is b #超过256之后数字的id就变了
False

在这里插入图片描述

15、operator包的应用
>>> import operator
>>> print(operator.add(1,1))
2
>>> print(operator.sub(2,1))
1
>>> print(operator.mul(2,3))
6
>>> print(operator.truediv(6,2))
3.0
>>> print(operator.contains("ab","a"))
True
>>> print(operator.pow(2,3))
8
>>> print(operator.ge(1,1))
True
>>> print(operator.ge(2,1))
True
>>> print(operator.le(1,2))
True
>>> print(operator.eq(1,1))
True
>>> print(operator.gt(2,1))
True
>>> print(operator.gt(1,2))
False
>>> print(operator.lt(1,2))
True
>>> print(operator.lt(2,1))
False

在这里插入图片描述

>>> eval("1+2")
3
>>> "print ('hi')"
"print ('hi')"
>>> s="print ('hi')"
>>> exec(s)
hi

在这里插入图片描述

16、标准输入、标准输出和错误输出

在这里插入图片描述
将标准文件改为文件输出:

>>> import sys
>>> print('divein!')
divein!
>>> saveout=sys.stdout
>>> fsock=open('out.log','w')
>>> sys.stdout=fsock
>>> print('This message will belogged instead of displayed')
>>> sys.stdout=saveout
>>> fsock.close()
17、sys.stdin与input
>>> import sys
>>> print('hello:',end='')
hello:>>> hi=sys.stdin.readline()[:-1]
women
>>> hi
'women'

在这里插入图片描述

18、重定向错误输出

在这里插入图片描述

19、表达式计算矩形的面积和周长
#coding=utf-8
length=5
breadth=2
area=length *breadth
print("面积是:",area)
print("周长是:",2*(length+breadth))

在这里插入图片描述

>>> import math
>>> math.pi
3.141592653589793
>>>

在这里插入图片描述

def cmp(a,b):if not isinstance(a,(int,float)) not  isinstance(b,(int,float)):raise TypeErrorif a>b:return 1elif a==b:return 0else:return -1
print(cmp(1,1))
print(cmp(2,1))
print(cmp(-1,1))

这篇关于python全天课视频(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/737981

相关文章

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图