Python基础

python官网:http://www.python.orgpython官方文档:https://docs.python.org/2/ or https://docs.python.org/3/python第三方插件:https://pypi.python.org/pypiUbuntu下python的第三方插件包的默认路径:/usr/local/lib/python2.7/dist...
摘要由CSDN通过智能技术生成

python官网:http://www.python.org

python官方文档:https://docs.python.org/2/  or  https://docs.python.org/3/

python第三方插件:https://pypi.python.org/pypi

Ubuntu下python的第三方插件包的默认路径:/usr/local/lib/python2.7/dist-packages/

 

1.交互模式(python编译器python.exe)下,符号“_”是特殊标识符,保留了表达式的最后一个计算结果。

  如:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

 

2.标识符

  (1)有字母、数字和下划线组成,但不能以数字开头;

  (2)区分大小写;

  (3)以单下划线开头(如:_foo)的,表示不能直接访问的类属性,需通过类提供的接口进行访问,不能“from xxx import * ”导入;

  (4)以双下划线开头(如:__foo)的,表示类的私有成员;

  (5)以双下划线开头和结尾的(如:__init__),代表特殊方法专用的标识;

 

3.多行语句

  (1)可用“\”将一行的语句氛围多行显示,如:      

total = item_one + \ 
        item_two + \
        item_three

 

  (2)若语句中包含[],{},()就不需要使用多行连接符,如:

 days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']

 

 

 

4.注释

  (1)用“#”开头,python没有块注释;

 

5.同一行显示多条语句:用“;”分隔;

 

6.多个变量赋值

 

Python允许你同时为多个变量赋值。如:

a = b = c = 1

以上实例,创建一个整型对象,值为1,三个变量被分配到相同的内存空间上。

您也可以为多个对象指定多个变量。如:

a, b, c = 1, 2, "john"

以上实例,两个整型对象1和2的分配给变量a和b,字符串对象"john"分配给变量c。

 

7.数字

  (1)不可改变;

  (2)删除可用del语句;

  (3)支持复数形式;

 

8.字符串

  (1)打印字符串,既可以用双引号,也可用单引号;

  (2)字符串中有特殊字符,如“\n”需要打印的,只需在字符串引号前加“r”;

           如:

>>> print r'C:\some\name'  # note the r before the quote
C:\some\name

  (3)字符串连接,除了“+”,还可以单独字符串并排;

           如:

>>> 'Py' 'thon'
'Python'

  (4)打印字符串中,有的特殊符号,可用反斜杠进行转义;

  (5)打印多行,需用 """ '''

          注:在第一个3引号后加“\”,可略去开头的空行;

          如:

print """
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
"""

produces the following output (note that the initial newline is not included):

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

 

print """\ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """

produces the following output (note that the initial newline is not included):

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

  (6)字符串可用“+”和“*”连接;

          如:

>>> 3 * 'un' + 'ium'
'unununium'

  (7)两个或多个字符串写在一起,会一起打印出来;

           如:

>>> 'Py' 'thon'
'Python'

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值