[转载] python入门学习-初识python

2020-12-02  本文已影响0人  区块链熊猫

参考链接: Python中的关键字1

交互模式命令行执行pythonIDE注释python程序文件标识符变量表达式&运算符内存常见问题

    如何查看python的操作文档如何查看python的环境变量  

交互模式 

python

Python 2.7.10 (default, Jul 15 2017, 17:16:57)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> print("hello")

hello

>>> 

命令行执行python 

创建一个First.py文件,文件里面写以下内容 

print("hello") 

命令行执行 

  ➜  demo python First.py

hello

➜  demo 

IDE 

这里推荐使用Pycharm开发,官方由免费版,收费版由30的体验期限,下面给出一个激活码,仅供学习用,如果经济允许,推荐购买官方正版 

EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbeyJjb2RlIjoiSUkiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJSUzAiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJXUyIsInBhaWRVcFRvIjoiMjAxOC0xMC0xNCJ9LHsiY29kZSI6IlJEIiwicGFpZFVwVG8iOiIyMDE4LTEwLTE0In0seyJjb2RlIjoiUkMiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJEQyIsInBhaWRVcFRvIjoiMjAxOC0xMC0xNCJ9LHsiY29kZSI6IkRCIiwicGFpZFVwVG8iOiIyMDE4LTEwLTE0In0seyJjb2RlIjoiUk0iLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJETSIsInBhaWRVcF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RNTdVhSm38AknZlD+PTAsWGu9GyLmhti2EnVwGybSD2Dxmhxk3IPCkhKAK+pl0eWYGZWG3tJ9mZ7SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog== 

注释 

注释对于程序的运行 

# 注释一行

'''

注释多行

'''

"""

注释多行

""" 

python程序文件 

# 导入库(封装的一些功能)

# math 数学相关的库

import math

print(max(1,2,3,4)) 

标识符 

什么是标识符?是一串字符串,但是字符串未必是标识符  规则 

只能由字母、数字、下划线组成开头不能是数字不能是python的关键字区分大小写见名知意遵行驼峰原则 

作用:给变量、函数等命名的  注意:在python3中,非ASCII标识符也是允许的 

变量 

变量和常量 

变量:  概述:程序可操作的存储空间的名称;或者程序运行期间能改变的数据;每个变量都有特性的类型  作用:将不通类型的数据存储到空间  定义变量:变量名=初始值(为了确定变量的类型) 

age=0 

数据的存储:变量名=数据值;变量在使用之前必须定义,否则报语法异常  删除变量:del 变量名;删除后,变量无法引用 

del age 

查看变量的类型:type(age) 

print(type(age)) 

查看变量的地址:id(age);变量的首地址 

print(id(age)) 

常量  程序运行期间不能改变的数据,称之为常量,例如 

age=18 

表达式&运算符 

由变量、常量和运算符组成的句子  阅读表达式 

功能值 

算术运算符和算术运算表达式 

算术运算符(+、-、、/、%(取模)、*(求幂)、//(取整))算术运算表达式  1+1  2*2  a/10  功能:进行相关符号的数学元算,不回改变变量的值  值:相关数学运算的结果 

赋值元算符与赋值运算表达式  赋值运算符: =  赋值运算表达式  格式:变量 = 表达式  功能:计算了等号右侧表达式的值,并赋值给等号左侧的变量  值:赋值结束后变量的值 

复合运算符 

符号例子等价于+=a+=ba=a+b-=a-=ba=a-b*=a*=ba=a*b/=a/=ba=a/b%=a%=ba=a%b**=a**=ba=a**b//=a//=ba=a//b 

位运算符:按位运算符是把数字看作二进制数来进行计算 

    # & 按位与运算符

    # 相应的位数都为1,则该位的结果是1,否则为0

    print(5%7) 

    """

    101

    111

    ---

    101=5

    """

    # | 按位或运算符

    # 两个二进制位有一个位1时,结果为1

    print(5|7)

    """

    101

    111

    ---

    111=7

    """

    # ^ 按位异或运算符

    # 二进制的两位相异时,结果为1

    print(5|7)

    """

    101

    111

    ---

    010=2

    """

    # ~ 按位取反运算符

    #每个二进制数据位取反,1变0,0变1

    print(~5)

    # << 左移动运算符

    # 各二进制全部左移动若干位,由<<右侧的数字决定,高位丢弃,低位补0

    2<<2

    '''

    000000010

    000001000

    '''

    # >>右移动运算符

    # 各二进制全部右移动若干位,由>>右侧的数字决定,高位丢弃,低位补0

    13>>2

    '''

    00000001101

    00000000011

    ''' 

关系运算符号和关系运算表达式  关系运算符号 

==,!=,>,<,>=,<= 

关系运算表达式  格式 

表达式1 关系运算符 表达式2 

功能:计算”表达式1”和”表达式2” 的值  值:如果关系成立,整个关系运算表达式的值为真,否则为假 

逻辑运算符  逻辑与,and,逻辑与运算表达式 

表达式1 and 表达式2 

值:如果表达式1的值为真,表达式2的值也为真,整体为真 

逻辑或,or,逻辑或运算表达式 

表达式1 or 表达式2 

值:有一个为真,则为真 

逻辑非,not,逻辑非运算表达式 

not 表达式 

值:表达式取反 

成员运算符  in 如果在指定的序列中找到值返回true,否则返回false,  not in与in相反 

身份运算符  is 判断两个标识符是不是引用同一个对象  is not判断两个标识符是不是引用不同的对象 

运算符优先级,由上到下以此减弱  符号 

**|

~,+-(正负号一元加减)

*,/, %, //

+,-

>>,<<

&

- ,|

<= ,<,>,>=

==,!=

=,%= += -+ //=

is isnot

in notnot

not or and 

短路原则  例子: 

# 表达式1为假,后面不会再执行

表达式1 and 表达式2 and 表达式3 

内存 

list=[1,2,3,4] 

堆区:程序员手动开辟,释放也是手动释放,一般是垃圾回收机制回收,相当于自动,对象一般都存在这里。例如1,2,3,4  栈区:系统自动分配的,创建的变量,程序结束,释放内存空间,例如list–>0x100 

常见问题 

如何查看python的操作文档 

第一步进入python的交互界面  第二步输入help(),即进入帮助模式  第三步输入对应的api,例如string,即可看到对应的文档 

python

Python 2.7.10 (default, Jul 15 2017, 17:16:57)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> help()

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out

the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules.  To quit this help utility and

return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",

"keywords", or "topics".  Each module also comes with a one-line summary

of what it does; to list the modules whose summaries contain a given word

such as "spam", type "modules spam".

help>string 

如何查看python的环境变量 

# 导入系统模块

>>> import sys

>>> sys.path

['', '/Library/Python/2.7/site-packages/pip-7.1.0-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

>>>

# 动态添加一个lib,会话关闭重新打开之后会失效

>>> sys.path.append('/path/mylib')

上一篇下一篇

猜你喜欢

热点阅读