BD_学习Python简明教程

linux系统:ubuntu16.04

python :2.7.12

1、Python 2.7.12跟Python3.6.1使用help帮助的区别

即:Python2的help参数需加引号,Python3兼容Python2

2、转义字符&自然字符串

>>> print "this is a per\
... son"
this is a person
>>> print r"this is a per\
... son"
this is a per\
son
>>> 

3、python 自动级连

>>> print 'what\'s' ' your name?'
what's your name?
>>> print 'what\'s'' your name?'
what's your name?
>>> 

4、在vim var.py文件时,一不小心按到ctrl+s,使文件发生了异常。再次打开该文件提示:

E325: 注意
发现交换文件 ".var.py.swp"
            所有者: zh    日期: Mon Apr 22 16:55:03 2019
            文件名: ~zh/var.py
            修改过: 是
            用户名: zh      主机名: ubuntu16-04
           进程 ID: 4536
正在打开文件 "var.py"
              日期: Mon Apr 22 17:14:23 2019
      比交换文件新!

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    如果是这样,请用 ":recover" 或 "vim -r var.py"
    恢复修改的内容 (请见 ":help recovery")。
    如果你已经进行了恢复,请删除交换文件 ".var.py.swp"
    以避免再看到此消息。

交换文件 ".var.py.swp" 已存在!
以只读方式打开([O]), 直接编辑((E)), 恢复((R)), 删除交换文件((D)), 退出((Q)), 中>
止((A)): 

解决方案:

请参看:https://blog.csdn.net/ljihe/article/details/52231000

分析:异常退出,linux会针对该文件生成一个swp文件。

若不需恢复还原旧文件内容的打算,直接将该文件删掉即可,从而可以从新编辑var.py文件。注意,该文件,用ll命令是看不到的。

5、while 表达式:

while 表达式:
    语句块
else:
    语句

while 语句结束,else语句必执行,前提是while的终止跟break无关;

若break语句终止里while循环,则else语句不执行。

注意:else可选

6、for语句

for xx in xxxx:
    语句块
else:
    语句块

else语句可选,for循环执行完之后,才会执行else语句,前提是while的终止跟break无关;

若break语句终止里while循环,则else语句不执行。

7、global的使用

zh@ubuntu16-04:~/python_simple$ python func_global.py 
x is 50
Changed local x to 2
value of x is 2
zh@ubuntu16-04:~/python_simple$ cat func
func_global.py  func_local.py   func_param.py   function.py
zh@ubuntu16-04:~/python_simple$ cat func_global.py 
#!/usr/bin/python
#Filename:func_global.py
def func():
	global x
	print "x is",x
	x=2
	print "Changed local x to",x
x=50
func()
print "value of x is",x

zh@ubuntu16-04:~/python_simple$ 

8、函数返回值:return

函数没有明确返回值的情况:

zh@ubuntu16-04:~/python_simple$ python function_return1.py 
None
zh@ubuntu16-04:~/python_simple$ cat function_return1.py 
#!/usr/bin/python
#Filename:func_return1.py
def someFunction():
	pass
print someFunction()

zh@ubuntu16-04:~/python_simple$ 

9、文档字符串__doc__属性的使用:

zh@ubuntu16-04:~/python_simple$ python func_doc.py 
5 is maximum
Prints the maximum of two numbers.

	The two values must be integers.
zh@ubuntu16-04:~/python_simple$ cat func_doc.py 
#!/usr/bin/python
#Filename:func_doc.py
def printMax(x,y):
	'''Prints the maximum of two numbers.

	The two values must be integers.'''
	x=int(x)
	y=int(y)
	if x>y:
		print x,'is maximum'
	else:
		print y,'is maximum'
printMax(3,5)
print printMax.__doc__

zh@ubuntu16-04:~/python_simple$ 

9、help()的使用:只是取函数的__doc__属性,然后展示

zh@ubuntu16-04:~/python_simple$ python func_doc.py 
5 is maximum
Prints the maximum of two numbers.

	The two values must be integers.
help()的使用:

zh@ubuntu16-04:~/python_simple$ cat func_doc.py 
#!/usr/bin/python
#encoding:utf-8
#Filename:func_doc.py
def printMax(x,y):
	'''Prints the maximum of two numbers.

	The two values must be integers.'''
	x=int(x)
	y=int(y)
	if x>y:
		print x,'is maximum'
	else:
		print y,'is maximum'
printMax(3,5)
print printMax.__doc__

print "help()的使用:"
help(printMax)


zh@ubuntu16-04:~/python_simple$ 

 

Help on function printMax in module __main__:

printMax(x, y)
    Prints the maximum of two numbers.
    
    The two values must be integers.
(END)

10、import的含义

import 模块名

含义:代表输入模块,当执行该语句的时候,它在sys.path变量中所列目录中寻找sys.py模块,当找到这个文件,该模块的主块将被运行。最后该模块将可以被使用。

若想要使用该模块中的变量。可以使用from 模块名 import 变量名

11、注意对象的引用跟copy的区别

切片操作符来取得拷贝;

将一个对象赋给一个变量的时候,这个变量仅仅引用那个对象

12、os模块

os.path.exists(绝对路径的文件)#判断某文件是否存在,存在返回True ,否则返回False  

os.system(命令字符串)#代码中实现某命令行命令的执行,执行成功返回0,否则返回错误码

os.sep#代表某平台的文件路径分隔符,不用考虑平台是windows还是linux;windows文件分隔符为“\\”,linux文件分隔符为“/”

os.mkdir(目录名的绝对地址)#创建一级目录

os.makedirs(目录的路径)#创建多级目录

13、

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值