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和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以