Python语言文件类型,变量及字符串
小标 2018-09-20 来源 : 阅读 1379 评论 0

摘要:本文主要向大家介绍了Python语言文件类型,变量及字符串,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言文件类型,变量及字符串,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

1. 文件类型:

(1)源代码:

    vim test.py

    #!/usr/bin/python

    print 'hello world!'

运行方法1:

    [root@localhost python]# python test.py

    hello world!

    [root@localhost python]#

运行方法2:

    [root@localhost python]# chmod +x test.py

    [root@localhost python]# ./test.py

    hello world!

    [root@localhost python]#

(2)字节代码:

python源文件编译后为扩展名字为.pyc

删除源码,编译后的二进制文件可以独立执行。

编译方法:

    vim 2.py

    #!/usr/bin/python

    import py_compile

    py_compile.compile('test.py')

    [root@localhost python]# python 2.py

    [root@localhost python]# ls

    2.py  test.py  test.pyc

    [root@localhost python]#

    [root@localhost python]# python test.pyc

    hello world!

    [root@localhost python]#

(3)优化的代码:

    python -O -m py_compile test.py

    [root@localhost python]# python -O -m py_compile test.py

    [root@localhost python]# ls

    2.py  test.py  test.pyc  test.pyo

    [root@localhost python]# python test.pyo

    hello world!

    [root@localhost python]#

2.Python的变量

变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变结构。

python下变量是对一个数据的引用

(1)变量的命名:

 变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。

变量名的第一个字符不能是数字,而必须是字母或下划线。

Python区分大小写。

不能将Python关键字用作变量名。

  例如: a   a1   _a

(2)变量的赋值:

是变量的声明和定义的过程。

    a = 123

    In [1]: a = 123

    In [2]: a

    Out[2]: 123

    In [3]: id(a)

    Out[3]: 7891024

    In [4]: a = 456

    In [5]: id(a)

    Out[5]: 19127624

    In [6]:

(3)运算符和表达式:

    赋值运算符

    算术运算符

    关系运算符

    逻辑运算符

    

表达式:

    将不同的数据(包括变量、函数)用运算符号按一定的规则连接起来的一种式子。

1)赋值运算符

    In [68]: a = 3

    In [69]: a

    Out[69]: 3

    In [70]: a+=3

    In [71]: a

    Out[71]: 6

    In [72]: a-=4

    In [73]: a

    Out[73]: 2

    In [76]: a*=3

    In [77]: a

    Out[77]: 6

    In [78]: a/=2

    In [79]: a

    Out[79]: 3

    In [80]: a%=3

    In [81]: a

    Out[81]: 0

    In [82]:

2)算术运算符

    In [82]: 1 + 2

    Out[82]: 3

    In [83]: 2 - 1

    Out[83]: 1

    In [84]: 2 * 2

    Out[84]: 4

    In [85]: 6 / 2

    Out[85]: 3

    In [86]: 6 % 2

    Out[86]: 0

    In [88]: 3.999999 / 2

    Out[88]: 1.9999995

    In [89]: 3.999999 // 2

    Out[89]: 1.0

    In [90]: 3 ** 2

    Out[90]: 9

    In [91]:

3)关系运算符:

    In [91]: 1 > 2

    Out[91]: False

    In [92]: 2 < 3

    Out[92]: True

    In [93]: 2 >= 1

    Out[93]: True

    In [94]: 3 <= 56

    Out[94]: True

    In [95]: 3 == 3

    Out[95]: True

    In [96]: 2 != 34

    Out[96]: True

    In [97]:

4)逻辑运算符:


    In [97]: 1 < 2 and 2 > 0

    Out[97]: True

    In [98]: 1 == 1 and 2 < 1

    Out[98]: False

    In [99]: 1 == 1 or 2 < 1

    Out[99]: True

    In [100]: not 1 > 2

    Out[100]: True

5)各种运算符的优先级:

往右越高   上到下越高,

lambda 匿名函数。

练习:

写一个四则运算器:

要求从键盘读取数字。

input()与raw_input()

    查看帮助:help(input)

    raw_input()都当然成字符串处理

    %s 格式化字符串。

    [root@localhost python]# cat 4.py

    #!/usr/bin/python

    num1 = input("Please input: ")

    num2 = input("Please input: ")

    print "%s + %s = %s" % (num1,num2,num1+num2)

    print "%s -  %s = %s" % (num1,num2,num1-num2)

    print "%s * %s = %s" % (num1,num2,num1*num2)

    print "%s / %s = %s" % (num1,num2,num1/num2)

    [root@localhost python]# python 4.py

    Please input: 3

    Please input: 5

    3 + 5 = 8

    3 -  5 = -2

    3 * 5 = 15

    3 / 5 = 0

    [root@localhost python]#


3.Python的数值和字符串

数据类型:

    数值

    字符串

    列表

    元组

    字典

(1)数值类型:

    整型

        In [6]: a = 123

        In [7]: type(a)

        Out[7]: int

        In [8]:

    长整型

        In [8]: a = 199999999999999999999999999999

        In [9]: a

        Out[10]: 199999999999999999999999999999L

        In [11]: type(a)

        Out[12]: long

        In [13]:

     浮点型

        0.0, 12.0   -18.8   3e+7等

        科学计数法是浮点型

        In [11]: 3e+7

        Out[11]: 30000000.0

        In [12]: type(3e+7)

        Out[12]: float

        In [13]: 3.0/2

        Out[13]: 1.5

        In [14]: type(3.0/2)

        Out[14]: float

        In [15]:

    复数型

        python对复数提供内嵌支持,这是大部分软件没有的。

        In [8]: a = 3.14j

        In [9]: a

        Out[9]: 3.14j

        In [10]: type(a)

        Out[10]: complex

(2)字符串类型:

        In [12]: a = 'abc'

        In [13]: a

        Out[13]: 'abc'

        In [14]: type(a)

        Out[14]: str

        In [15]:

        三重引号还可以做注释:.

        In [28]: a = 'hello\nworld'

        In [29]: a

        Out[29]: 'hello\nworld'

        In [30]: a = "hello\nworld"

        In [31]: a

        Out[31]: 'hello\nworld'

        In [39]: a = '''hello\nworld'''

        In [40]: a

        Out[40]: 'hello\nworld'

        In [41]: print a

        hello

        world

        In [42]:

        In [43]: type(a)

        Out[44]: str

    序列索引:

        In [42]: a = 'abcde'

        In [43]: a[0]

        Out[43]: 'a'

        In [44]: a[1]

        Out[44]: 'b'

        In [45]: a[-1]

        Out[45]: 'e'

        In [46]: a[-2]

        Out[46]: 'd'

    序列切片:

        In [42]: a = 'abcde'

        In [43]: a[0]

        Out[43]: 'a'

        In [44]: a[1]

        Out[44]: 'b'

        In [45]: a[-1]

        Out[45]: 'e'

        In [46]: a[-2]

        Out[46]: 'd'

        In [47]: a[0:2]

        Out[47]: 'ab'

        In [48]: a[0:4]

        Out[48]: 'abcd'

        In [49]: a[0:3]

        Out[49]: 'abc'

        In [50]: a[1:3]

        Out[50]: 'bc'

        In [56]: a[0] + a[1]

        Out[56]: 'ab'

        In [57]: a[:2]

        Out[57]: 'ab'

        In [58]: a[:]

        Out[58]: 'abcde'

        In [59]: a[:-1]

        Out[59]: 'abcd'

        In [60]: a[::-1]

        Out[60]: 'edcba'

        In [61]: a[::1]

        Out[61]: 'abcde'

        In [62]: a[:3:1]

        Out[62]: 'abc'

        In [63]: a[::2]

        Out[63]: 'ace'

        In [64]: a

        Out[64]: 'abcde'

        In [65]: a[-4::-2]

        Out[65]: 'b'

        In [66]: a[-4:-2]

        Out[66]: 'bc'

        In [67]: a[-2:-4:-1]

        Out[67]: 'dc'

        In [68]:

练习:


1.将 “123” 转换成整数

    In [10]: a = int(123)

    In [11]: print a

    123

    In [12]:

2.将 “9999999999999999999” 转换成长整数

    In [18]:  a = long(9999999999999999999)

    In [19]: print a

    9999999999999999999

    In [20]: a

    Out[20]: 9999999999999999999L

    In [21]:

3.将 “3.1415926” 转换成一个浮点数

    In [21]:  a = float(3.1415926)

    In [22]: print a

    3.1415926

    In [23]:

4.将 123 转换成一个字符串

    In [23]:  a = str(123)

    In [24]: print a

    123

    In [25]:

5.现有以下字符串

字符串1:" abc deFGh&*ijkl opq mnrst((uvwxyz "

字符串2:" ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "

使用字符串的各种方法转换成如下方式

ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba

    In [25]: str1 = " abc deFGh&*ijkl opq mnrst((uvwxyz "

    In [26]: str2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "

    In [27]: print filter(lambda x:x.isalpha(),str2+str1[::-1].lower())

    ABCDEFGHIJMNOPQKLRSTUVWXYZzyxwvutsrnmqpolkjihgfedcba

    In [28]:

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程