Python教程

python 01基础符号

本文主要是介绍python 01基础符号,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

python 

使用中文字符
# -*- coding: utf-8 -*-

1.输入
input() 与 raw input()
当输入为纯数字时

input返回的是数值类型,如int,float
raw_inpout返回的是字符串类型,string类型

输入字符串为表达式

input会计算在字符串中的数字表达式,而raw_input不会。

如输入 “57 + 3”:

input会得到整数60
raw_input会得到字符串”57 + 3”

python input的实现

看python input的文档,可以看到input其实是通过raw_input来实现的,原理很简单,就下面一行代码:

def input(prompt):
return (eval(raw_input(prompt)))

2 查询python文档
python -m pydoc open
python -m pydoc sys
。。。。。。(按q退出)


3参数,解包和变量
argv是 argument variable
from sys import argv

script, filename = argv

4文档读写

txt = open (filename) #创建了一个file对象
print txt.read()

提示符下也可打开文档(转行有问题)
>>> t= open('try.txt','r')
>>> t.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\nLots and lots of fun to have in here.\n'
>>> t.close()

5文档的其它操作

# 通过CTRL-c进行程序终止
print "If you want that , hit CTRL_C"
print "If not , hit RETURN"
raw_input("?")

w+是写读模式,会把文件清空 a是添加 r只读模式
target = open(filename,'w')
w a() r

line1 = raw_input("line1:")
target.write(line1)
target.write("\n")

清除
target.truncate()

6更多文件操作
(控制台可用cat try.txt显示文档内容)

判断文件是否存在
#import引入exist
from os.path import exists
print "exist: %r" %exists(to_file)

#得到文件长度
print "lenth is %d"%len(indata)

将指针返回文档开头
f.seek(0)

读取一行
readline()扫描文档里的每个字节直到找到一个\n然后停止读取并且返回此前文件内容,文件f记录调用后的读取位置,方便下次调用时取接下来一行

7 阅读一些代码

bitbucket.org

lauchpad.net

freecode.com

sourceforge.net

这篇关于python 01基础符号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!