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开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑