设为首页 收藏本站
查看: 1298|回复: 0

[经验分享] Python文件类型,变量及字符串

[复制链接]

尚未签到

发表于 2018-8-10 10:50:07 | 显示全部楼层 |阅读模式
  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]:>  Out[3]: 7891024
  In [4]: a = 456

  In [5]:>  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)逻辑运算符:
DSC0000.jpg

  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(&quot;Please input: &quot;)
  num2 = input(&quot;Please input: &quot;)
  print &quot;%s + %s = %s&quot; % (num1,num2,num1+num2)
  print &quot;%s -  %s = %s&quot; % (num1,num2,num1-num2)
  print &quot;%s * %s = %s&quot; % (num1,num2,num1*num2)
  print &quot;%s / %s = %s&quot; % (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 = &quot;hello\nworld&quot;
  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:&quot; abc deFGh&*ijkl opq mnrst((uvwxyz &quot;
  字符串2:&quot; ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ &quot;
  使用字符串的各种方法转换成如下方式
  ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba
  In [25]: str1 = &quot; abc deFGh&*ijkl opq mnrst((uvwxyz &quot;
  In [26]: str2 = &quot; ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ &quot;
  In [27]: print filter(lambda x:x.isalpha(),str2+str1[::-1].lower())
  ABCDEFGHIJMNOPQKLRSTUVWXYZzyxwvutsrnmqpolkjihgfedcba
  In [28]:

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549580-1-1.html 上篇帖子: Python中函数定义及参数实例 下篇帖子: 一位大牛整理的Python资源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表