心海恋歌 发表于 2018-8-13 09:17:25

python的类型 变量 数值和字符串

  文件类型一
  源代码
  -Python源代码文件以“py”为扩展名,由Python程序解释,不需要编译
  ># mkdir python
  # cd python/
  # vim 1.py      //写一个代码
  >#! /usr/bin/python
  >print 'hellow world'
  ># python 1.py   //执行
  hellow world
  文件类型二
  字节代码
  -Python源代码文件经编译后,生成的扩展名为“pyc”的文件
  -编译方法:
  >importpy_compile
  py_compile.compile('hello.py')
  ># vim 2.py
  >#! /usr/bin/python
  >importpy_compile
  py_compile.compile('./1.py')
  # python 2.py
  # ls
  1.py1.pyc2.py
  # file 1.pyc
  1.pyc: python 2.7 byte-compiled
  # cat 1.py
  #! /usr/bin/python
  >print 'hellow world'
  # cat 1.pyc
  »顁@s    dGHdS(s
  hellow worldN((((s./1.py<module>s   //二进制的乱码文件
  #
  # python 1.pyc                     //用Python查看
  hellow world
  #
  -优化代码
  -经过优化的源码文件,扩展名为“pyo”
  -Python -O -m py_compile hello.py
  ># python -O -m py_compile 1.py
  # ls
  1.py1.pyc1.pyo2.py
  # python 1.pyo
  hellow world
  #
  python 的变量
  --变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变
  --Python下的变量是对一个数据的引用
  --变量的命名由字母 数字 下划线组成 不能以数字开头 不可以使用关键字

  --变量的赋值是变量的声明和定义的过程a = 1   >  ># vim 3.py
  #! /usr/bin/python
  num1 = input(&quot;please a number: &quot;)
  num2 = input(&quot;please a number: &quot;)
  print num1 + num2
  print num1 - num2
  print num1 num2
  print num1 / num2
  # python 3.py
  please a number: 5
  please a number: 3
  8
  2
  15
  1
  # vim 3.py
  #! /usr/bin/python
  num1 = input(&quot;please a number: &quot;)
  num2 = input(&quot;please a number: &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, num1num2)
  print &quot;%s / %s = %s&quot; % (num1, num2, num1/num2)
  # python 3.py
  please a number: 7
  please a number: 3
  7 + 3 = 10
  7 - 3 = 4
  73 = 21
  7 / 3 = 2
  python的数据类型
  案例 123和‘123’一样吗?
  123 代表数值
  ‘123’代表字符串
  还有   列表元祖字典
  数值类型
  长整型
  In : a = 12999999999999999999999
  In : a
  Out: 12999999999999999999999L
  In : type (a)
  Out: long
  也可以把小的数值长整型
  In : a = 100l
  In : type (a)
  Out: long
  浮点型   例如 0.0 12.0 -18.8 3e+7
  复数型
  换行符
  In : a = &quot;hello\nworld&quot;
  In : print a
  hello
  world
页: [1]
查看完整版本: python的类型 变量 数值和字符串