python

it2025-05-05  9

1.环境搭建

anaconda环境配置 在这里环境选择anaconda搭建,进入anaconda官网下载最新版本 https://www.anaconda.com/ 根据下面链接进行安装https://www.cnblogs.com/MrZhangxd/p/10726791.html

解释器 当我们编写python代码时,得到的时包含python代码的以.py为扩展名的文本文件,若要运行代码则需要python解释器去执行.py的文件 使用最广的python解释器是cpython 另python解释器还包含ipython,pypy,jython,ironpython

2.python初体验

print and input print

print(‘welcome to the python world’) welcome to the python world

input

name=input() michael

name ‘michael’

3.python基础讲解

python变量特性+命名规则 变量可以是数字还可以是任意数据类型 变量名必须是大小写英文,数字和“—”组合,不能是数字开头 a=1 变量a是整数 t_007=‘T007’ 变量t_007是字符串 Answer=True 变量Answer是布尔值true 命名规则 必须以字⺟或下划线开头,且只能是下划线、字⺟和数字的组合。 • 不能和语⾔保留字相同。 • 名字区分⼤⼩写。 • 模块中以下划线开头的名字视为私有。 • 以双下划线开头的类成员名字视为私有。 • 同时以双下划线开头和结尾的名字,通常是特殊成员。 • 单⼀下划线代表最后表达式的返回值

注释方法 注释即为#右面的文字可以是中文也可以是英文

print absolute value of an integer:

a=100 if a >=0: print(a) else: print(-a)

python中“:”作用 以:结尾时,下一个为缩进语句,缩进的语句为一个代码块

学会使用dir( )及和help( ) dir(sys) [‘breakpointhook’, ‘displayhook’, ‘doc’, ‘excepthook’, ‘interactivehook’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘stderr’, ‘stdin’, ‘stdout’, ‘_clear_type_cache’, ‘_current_frames’, ‘_debugmallocstats’, ‘_enablelegacywindowsfsencoding’, ‘_framework’, ‘_getframe’, ‘_git’, ‘_home’, ‘_xoptions’, ‘api_version’, ‘argv’, ‘base_exec_prefix’, ‘base_prefix’, ‘breakpointhook’, ‘builtin_module_names’, ‘byteorder’, ‘call_tracing’, ‘callstats’, ‘copyright’, ‘displayhook’, ‘dllhandle’, ‘dont_write_bytecode’, ‘exc_info’, ‘excepthook’, ‘exec_prefix’, ‘executable’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, ‘get_asyncgen_hooks’, ‘get_coroutine_origin_tracking_depth’, ‘get_coroutine_wrapper’, ‘getallocatedblocks’, ‘getcheckinterval’, ‘getdefaultencoding’, ‘getfilesystemencodeerrors’, ‘getfilesystemencoding’, ‘getprofile’, ‘getrecursionlimit’, ‘getrefcount’, ‘getsizeof’, ‘getswitchinterval’, ‘gettrace’, ‘getwindowsversion’, ‘hash_info’, ‘hexversion’, ‘implementation’, ‘int_info’, ‘intern’, ‘is_finalizing’, ‘last_traceback’, ‘last_type’, ‘last_value’, ‘maxsize’, ‘maxunicode’, ‘meta_path’, ‘modules’, ‘path’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’, ‘ps1’, ‘ps2’, ‘ps3’, ‘set_asyncgen_hooks’, ‘set_coroutine_origin_tracking_depth’, ‘set_coroutine_wrapper’, ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’, ‘setswitchinterval’, ‘settrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘version’, ‘version_info’, ‘warnoptions’, ‘winver’] dir()函数能返回由对象所定义的名称列表,如果这一对象是一个模块,则该列表会包括函数内所定义的函数、类与变量 help(help) Help on _Helper in module _sitebuiltins object:

class _Helper(builtins.object) | Define the builtin ‘help’. | | This is a wrapper around pydoc.help that provides a helpful message | when ‘help’ is typed at the Python interactive prompt. | | Calling help() at the Python prompt starts an interactive help session. | Calling help(thing) prints help for the python object ‘thing’. | | Methods defined here: | | call(self, *args, **kwds) | Call self as a function. | | repr(self) | Return repr(self).

Data descriptors defined here:dictdictionary for instance variables (if defined)weakreflist of weak references to the object (if defined)

使用help()可以获得对python对象的帮助

import使用

import sys print(‘the command line arguments are:’) the command line arguments are:

for i in sys.argv: … print(i) File “”, line 2 print(i) ^ IndentationError: expected an indented block

print(’\n\nthe PYTHONPATH is’,sys.path,’\n’)

the PYTHONPATH is [’’, ‘c:\python\python37.zip’, ‘c:\python\DLLs’, ‘c:\python\lib’, ‘c:\python’, ‘c:\python\lib\site-packages’] 通过import 语句导入sys模块,代码将告诉python我们将使用这以模块 pep8介绍 https://wenku.baidu.com/view/0d9535d8a300a6c30d229fc4.html该链接介绍了关于python编码的规范

4.python数值基本知识

python中数值类型,int,float,bool,e记法等 int函数可以将其他数据类型转换为整数

int(123.45) 123

float能将数字转换为浮点数

float(123) 123.0

bool为布尔型,None、0、空字符串、以及没有元素的容器对象都可视为 False,反之为 True

bool(1) True

bool(’’) False

e记法即为科学计数法,用幂的形式表示

a=12334345.345 print(’%E’%a) 1.233435E+07

算数运算符 +加,两个对象相加

1+1 2

-减,一个数减去另一个数

-1-1 -2

*两个数相乘

5*5 25

/两个数相除

7/8 0.875

%返回除法的余数

7%3 1

**返回x的y次幂

5**2 25

//返回商的整数部分

10//3 3

逻辑运算符 辑运算符or,x or y, 如果x为True则返回x,如果x为False返回y值。因为如果x为True那么or运算就不需要在运算了,因为一个为真则为真,所以返回x的值。如果x的值为假,那么or运算的结果取决于y,所以返回y的值 逻辑运算符and,x and y,如果x为True则返回y值。如果x为False则返回y值。如果x的值为True,and的运算不会结束,会继续看y的值,所以此时真与假取决于y的值,所以x如果为真,则返回y的值。如果x为假,那么and运算就会结束运算过程了,因为有一个为假则and为假,所以返回x的值。 按照从左向右,优先级高的先执行优先级高的规则,首先因为比较运算符优先级高于逻辑运算符,很简单,如果运算符低于了逻辑运算符优先级那还如何运算呢。and 优先级大于 or,not优先级大于and和or

print(1 > 2 and 3 or 4 and 3 < 2 or not 4 > 5) True

1》2为f 3<2为f not4>5为t f and 3为f f or f 为f f or t为t 最终输出true 成员运算符 in指在指定的序列中找到了,结果为true ,未找到,结果为false not in 与in 相反

l=(1,2,3) a=3 print(a in l) True

身份运算符 is用于判断2个标识符的引用是否一样,一样输出true,不一样输出false not is 与is 相反

a=(123) b=a print(b is a) True

运算符优先级 运算符优先级如下链接所示https://www.sojson.com/operation/python.html

最新回复(0)