前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python文件类型,变量及字符串

Python文件类型,变量及字符串

作者头像
py3study
发布2020-01-08 17:46:26
6320
发布2020-01-08 17:46:26
举报
文章被收录于专栏:python3

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)变量的命名:

  1.  变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。
  2. 变量名的第一个字符不能是数字,而必须是字母或下划线。
  3. Python区分大小写。
  4. 不能将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 匿名函数。

blob.png
blob.png

练习:

写一个四则运算器:

要求从键盘读取数字。

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]:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档