Python- entry

A first one Python code

Hello.py create files in the / home / dev / directory, as follows:

1
Hello.py file execution, namely:  python /home/dev/hello.py

Python internal process performed as follows:

Second, the interpreter

When executed python /home/dev/hello.py the previous step, clearly pointed out hello.py script to be executed by the python interpreter.

If you want to execute a shell script similar to the same run python script, for example:  ./hello.py , then you need to specify the interpreter in the head hello.py file, as follows:

#!/usr/bin/env python
 
print "hello,world"
As a result, execution: /hello.py  can be.

 

ps: hello.py to be given permission to perform before the execution, chmod 755 hello.py

Third, content encoding

When loading the interpreter code python .py file, the content is not encoded (default ascill)

ASCII (American Standard Code for Information Interchange, American Standard Code for Information Interchange) is a set of computer coding system based on the Latin alphabet, used to display modern English and other Western European languages, it can only be represented by a maximum of eight (one byte ), namely: 2 ** 8 = 256, therefore, can only represent the ASCII code 256 symbols.

ASCII code obviously can not be a variety of text and symbols on the whole world says so, we need a kind of new coding can represent all characters and symbols, namely: Unicode

Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多

UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...

所以,python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),如果是如下代码的话:

报错:ascii码无法表示中文

#!/usr/bin/env python
  print  "你好,世界" 

改正:应该显示的告诉python解释器,用什么编码来执行源代码,即:

1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 print "你好,世界"

四、注释

 当行注视:# 被注释内容

  多行注释:""" 被注释内容 """

五、执行脚本传入参数

Python有大量的模块,从而使得开发Python程序非常简洁。类库有包括三中:

  • Python内部提供的模块
  • 业内开源的模块
  • 程序员自己开发的模块

Python内部提供一个 sys 的模块,其中的 sys.argv 用来捕获执行执行python脚本时传入的参数

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
 
print sys.argv 

六、 pyc 文件

执行Python代码时,如果导入了其他的 .py 文件,那么,执行过程中会自动生成一个与其同名的 .pyc 文件,该文件就是Python解释器编译之后产生的字节码。

ps:代码经过编译可以产生字节码;字节码通过反编译也可以得到代码。

七、变量

1、声明变量

1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 name = "wupeiqi"

 

 

上述代码声明了一个变量,变量名为: name,变量name的值为:"wupeiqi"

变量的作用:昵称,其代指内存里某个地址中保存的内容

变量定义的规则:

  • 变量名只能是 字母、数字或下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 以下关键字不能声明为变量名
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

2、变量的赋值

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-

name1 = "wupeiqi"
name2 = "alex"


复制代码
 
复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-

name1 = "wupeiqi"
name2 = name1


复制代码
八、输入
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 # 将用户输入的内容赋值给 name 变量
5 name = raw_input("请输入用户名:")
6  
7 # 打印输入的内容
8 print name

 

 

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3  
 4 import getpass
 5  
 6 # 将用户输入的内容赋值给 name 变量
 7 pwd = getpass.getpass("请输入密码:")
 8  
 9 # 打印输入的内容
10 print pwd

九、流程控制和缩进

需求一、用户登陆验证

1 # 提示输入用户名和密码
2  
3 # 验证用户名和密码
4 #     如果错误,则输出用户名或密码错误
5 #     如果成功,则输出 欢迎,XXX!

 

需求二、根据用户输入内容输出其权限

1 # 根据用户输入内容打印其权限
2  
3 # alex --> 超级管理员
4 # eric --> 普通管理员
5 # tony --> 业务主管
6 # 其他 --> 普通用户

 

 
外层变量,可以被内层变量使用
内层变量,无法被外层变量使用
 
十、初识基本数据类型
1、数字

2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子。

int(整型)

  在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
long(长整型)
  跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
  注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
  浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
  复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
注:Python中存在小数字池:-5 ~ 257
 
2、布尔值
  真或假
  1 或 0
3、字符串
"hello world"
万恶的字符串拼接:
  python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
字符串格式化
1 name = "alex"
2 print "i am %s " % name
3  
4 #输出: i am alex
 
字符串是 %s;整数 %d;浮点数%f
字符串常用功能:
  • 移除空白
  • 分割
  • 长度
  • 索引
  • 切片
4、列表
创建列表:
1 name_list = ['alex', 'seven', 'eric']
2 3 name_list = list(['alex', 'seven', 'eric'])

 

基本操作:

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 切片
  • 循环
  • 包含
5、元祖
创建元祖:
1 ages = (11, 22, 33, 44, 55)
2 3 ages = tuple((11, 22, 33, 44, 55))

 

基本操作:
  • 索引
  • 切片
  • 循环
  • 长度
  • 包含
6、字典(无序)
创建字典:
1 person = {"name": "mr.wu", 'age': 18}
2 3 person = dict({"name": "mr.wu", 'age': 18})

 

常用操作:

  • 索引
  • 新增
  • 删除
  • 键、值、键值对
  • 循环
  • 长度
PS:循环,range,continue 和 break
 
十一、运算
算数运算:

比较运算:

赋值运算:

逻辑运算:

成员运算:

身份运算:

位运算:

运算符优先级:

更多内容:猛击这里

十二、初识文本的基本操作
 
打开文件:
  file_obj = file("文件路径","模式")
打开文件的模式有:
  • r,以只读方式打开文件
  • w,打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
  • a,打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
  • w+,打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。

读取文件的内容:

1 # 一次性加载所有内容到内存
2 obj.read()
3  
4 # 一次性加载所有内容到内存,并根据行分割成字符串
5 obj.readlines()
6  
7 # 每次仅读取一行数据
8 for line in obj:
9   print line

 

写文件的内容:

 1 obj.write('内容') 

关闭文件句柄:

1 obj.close()

 

Guess you like

Origin www.cnblogs.com/tanxiaox/p/12080154.html