1. 文件类型:

(1)源代码:

vim test.py
#!/usr/bin/python
print 'hello world!'

运行方法1:

python test.py
    hello world!
    [root@localhost python]#

运行方法2:

chmod +x test.py
 ./test.py
    hello world!
    [root@localhost python]#

(2)字节代码:

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

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

编译方法:

vim 2.py
    #!/usr/bin/python
    import py_compile
    py_compile.compile('test.py')
python 2.py
    [root@localhost python]# ls
test.pyc
    [root@localhost python]#
 python test.pyc
    hello world!
    [root@localhost python]#
(3)优化的代码:
    python -O -m py_compile test.py
 python -O -m py_compile test.py
    [root@localhost python]# ls
test.pyo
python test.pyo
    hello world!
    [root@localhost python]#

2.Python的变量

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

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

(1)变量的命名:

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

  例如: a   a1   _a

(2)变量的赋值:

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

a = 123
a = 123
a
    Out[2]: 123
id(a)
    Out[3]: 7891024
a = 456
 id(a)
    Out[5]: 19127624
    In [6]:

(3)运算符和表达式:

    赋值运算符

    算术运算符

    关系运算符

    逻辑运算符

    

表达式:

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

1)赋值运算符

a = 3
    In [69]: a
    Out[69]: 3
 a+=3
    In [71]: a
    Out[71]: 6
a-=4
    In [73]: a
    Out[73]: 2
a*=3
    In [77]: a
    Out[77]: 6
a/=2
    In [79]: a
    Out[79]: 3
 a%=3
    In [81]: a
    Out[81]: 0
    In [82]:

2)算术运算符

+ 2
    Out[82]: 3
 - 1
    Out[83]: 1
* 2
    Out[84]: 4
 / 2
    Out[85]: 3
%
    Out[86]: 0
    In [88]: 3.999999 / 2
    Out[88]: 1.9999995
 // 2
    Out[89]: 1.0
 **
    Out[90]: 9
    In [91]:

3)关系运算符:

>
    Out[91]: False
< 3
    Out[92]: True
 >= 1
    Out[93]: True
<= 56
    Out[94]: True
 == 3
    Out[95]: True
!= 34
    Out[96]: True
    In [97]:

4)逻辑运算符:

python怎么判断文件占用 python判断文件类型_python怎么判断文件占用

and 2 > 0
    Out[97]: True
and 2 < 1
    Out[98]: False
or 2 < 1
    Out[99]: True
not 1 > 2
    Out[100]: True

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

往右越高   上到下越高,

lambda 匿名函数。

python怎么判断文件占用 python判断文件类型_python怎么判断文件占用

python怎么判断文件占用 python判断文件类型_开发工具_03

练习:

写一个四则运算器:

要求从键盘读取数字。

input()与raw_input()
查看帮助:help(input)
raw_input()都当然成字符串处理
%s 格式化字符串。
 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)
 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)数值类型:

整型
 a = 123
 type(a)
 int
        In [8]:
    长整型
 a = 199999999999999999999999999999
 a
199999999999999999999999999999L
 type(a)
 long
        In [13]:
浮点型
        0.0, 12.0   -18.8   3e+7等
        科学计数法是浮点型
3e+7
        Out[11]: 30000000.0
 type(3e+7)
        Out[12]: float
 3.0/2
        Out[13]: 1.5
 type(3.0/2)
float
        In [15]:

    复数型

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

a = 3.14j
 a
        Out[9]: 3.14j
 type(a)
 complex

(2)字符串类型:

a = 'abc'
 a
        Out[13]: 'abc'
type(a)
str
        In [15]:

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

a = 'hello\nworld'
a
'hello\nworld'
a = "hello\nworld"
a
'hello\nworld'
a = '''hello\nworld'''
        In [40]: a
'hello\nworld'
print a
hello
world
        In [42]:
 type(a)
str

    序列索引:

a = 'abcde'
a[0]
        Out[43]: 'a'
a[1]
        Out[44]: 'b'
 a[-1]
        Out[45]: 'e'
 a[-2]
        Out[46]: 'd'

序列切片:

a = 'abcde'
a[0]
        Out[43]: 'a'
a[1]
        Out[44]: 'b'
 a[-1]
        Out[45]: 'e'
 a[-2]
        Out[46]: 'd'
a[0:2]
        Out[47]: 'ab'
a[0:4]
        Out[48]: 'abcd'
 a[0:3]
        Out[49]: 'abc'
a[1:3]
        Out[50]: 'bc'
a[0] + a[1]
        Out[56]: 'ab'
a[:2]
        Out[57]: 'ab'
 a[:]
        Out[58]: 'abcde'
 a[:-1]
        Out[59]: 'abcd'
a[::-1]
        Out[60]: 'edcba'
a[::1]
        Out[61]: 'abcde'
a[:3:1]
        Out[62]: 'abc'
a[::2]
        Out[63]: 'ace'
 a
        Out[64]: 'abcde'
a[-4::-2]
        Out[65]: 'b'
a[-4:-2]
        Out[66]: 'bc'
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]:


枫叶云  51CTO博客,原文链接:http://blog.51cto.com/fengyunshan911/2052744