python是一种解释性的、面向对象的、带有动态语义的高级程序设计语言。
交互式python解释器:IDLE
python是通过缩进来组织代码段的,而不像c#中的大括号。一般推荐使用四个空格作为缩进。
除法:
默认情况下,一个整数除以另一个整数,结果仍然为整数:1/2 =0
如果其中一个数是浮点数,那么结果就是浮点数。
1.0/2 =>0.5 1/2.0=>0.5 1/2. =>0.5
双斜线:使用双斜线即使参数是浮点数,结果也会是整除。
1.0//2=》0.0
幂运算:
2**3=>8 -2**3=>-8
获取用户输入:input(str)方法。
>>> x=input('x=') x=5 >>> y=input('y=') y=6 >>> print x*y 30
幂函数:pow(2,3)=>8
绝对值:abs(-10)=>10
四舍五入:round(0.4)=>0.0 round(0.6)=>1.0
可以使用import命令导入特殊的模块来增强功能。
例如:
import math math.floor(1.9) =>1.0
cmath和复数:
>>> import cmath >>> cmath.sqrt(-1) 1j
python本身是支持复数计算的:
>>> (1+2j)+(3+4j) (4+6j) >>> (1+2j)-(3+4j) (-2-2j) >>> (1+2j)*(3+4j) (-5+10j)
python保存为.py为后缀的文件
用#井号表示注释,井号右侧的内容不会被解释。
字符串可以使用单引号或者双引号,如果中间字符串内容也包括引号,那么可以使用转义字符。
>>> "let's say \"hello world\"" 'let\'s say "hello world"'
拼接字符串使用加号:
>>> x='a' >>> y='b' >>> x+y 'ab'
字符串表示:str和repr
str:把值转换为合理的字符串形式,便于用户理解;
repr:把值转换为合理的python表达式的形式。
>>> x="hello world"; >>> print str(x) hello world >>> print repr(x) 'hello world' >>> y=10000L >>> print str(y) 10000 >>> print repr(y) 10000L
input 与raw_input
input 会假设用户输入的是合法的python表达式,在下面的例子中只有输入字符串时才会执行成功。
raw_input会把输入作为原始数据,然后将其放入到字符串中。
一般情况下,建议使用raw_input.
#使用Input >>> name=input('your name?') your name?chenjing Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> name=input('your name?') File "<string>", line 1, in <module> NameError: name 'chenjing' is not defined your name?'chenjing' >>> print 'hello'+name hellochenjing #使用raw_input >>> name=raw_input('your name?') >>> print 'hello '+name hello chenjing 注意raw_input是以字符串形式返回,如果想要整型需要使用int()进行类型转换。例如: birth=int(raw_input('your birth')) if(birth<2000): print 'before 00' else: print 'after 00'
长字符串
如果比较长的字符串需要跨行,那么可以使用三个引号,而且其中的引号也不必再使用转义字符了。
>>> str='''hello world hello 'cathy' hello chenjing''' >>> print str
原始字符串
原始字符串以r开头,原始字符串不会把反斜线作为特殊字符处理,在原始字符串输入的每个字符都会直接保持一致输出,不过原始字符串不能以反斜线结尾。
c:\program files >>> print r'c:\newfile' <a>c:\newfile</a>
字符串编码
ASCII编码是1个字节,而Unicode编码通常是2个字节。
UTF-8编码:可变长编码。把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节
在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。
Python支持ASCII编码,通过ord()和chr()函数,可以把字母和对应的数字相互转换。
print ord('A') >>>65 print chr(65) >>>A
Python在后来添加了对Unicode的支持,以Unicode表示的字符串用<code>u'...'</code>表示
u'测试'
想转换为utf-8需要encode('utf-8')方法
u'测试'.encode('utf-8')

字符串格式化
还可以指定是否补位0,以及小数位数
如果字符串中包含%,则使用两个百分号代替%%
%s和%r:%s等同于调用str()函数,%r等同于调用repr()。例如:
x="There are %d books."%10
print x
print "I said %r"%x
print "I said %s"%x
print str(x)
print repr(x)
运行结果:
There are 10 books.
I said 'There are 10 books.'
I said There are 10 books.
'There are 10 books.'
Python内置的datetime模块提供了datetime,date,time等常用时间类型。
date()和time()方法可以分别提取日期和时间;
strftime()方法用于将时间格式化为字符串;
strptime()方法用于将字符串转换为日期格式;
两个时间相减可以获得delta类型的时间差。
# -*- coding: cp936 -*- from datetime import datetime,date,time dt=datetime(2016,5,23,14,40,0) print(dt.day)#23 print(dt.hour)#14 print(dt.date())#2016-05-23 print(dt.time())#14:40:00 #格式化字符串 print(dt.strftime("%Y-%m-%d %H:%M"))#2016-05-23 14:40 #字符串转换为时间 dt=datetime.strptime('201605231440','%Y%m%d%H%M') print(dt) #2016-05-23 14:40:00 #时间差 dt1=datetime(2016,5,23,14,40) dt2=datetime(2016,5,1) delta=dt1-dt2 print(type(delta))#<type 'datetime.timedelta'> print(delta)#22 days, 14:40:00 print(dt+delta)#2016-06-15 05:20:00 本文转自 陈敬(Cathy) 博客园博客,原文链接: http://www.cnblogs.com/janes/p/5520177.html ,如需转载请自行联系原作者