变量类型
Number(数字) 包括int,long,float,double,complex
String(字符串) 例如:hello,"hello",helloList(列表) 例如:[1,2,3],[1,2,3,[1,2,3],4]Dictionary(字典) 例如:{1:"nihao",2:"hello"}Tuple(元组) 例如:(1,2,3,abc)File(文件) 例如:f = open(a.txt,rw)类型转换
ord() 将字符转换成ASCII
chr() 将ASCII转换成字符hex() 将整数转换成十六进制oct() 将整数转换成八进制bin() 将整数转换成二进制
此外还有int(),str()等。
1.运算符
print 1/2 #0print 1/2. #0.5print 1.0/2 #0.5print 1%3 #取模 1
除了用浮点(不知是否得当)来得到精确数据外,还可以引入__future__的division
from __future__ import division #导入精确除法 print 3/4 #0.75print 3//4 #0 强制整除
2.控制语句
SyntaxError: invalid syntax
原因可能是if when等控制语句首行末尾忘记写:
或则用tab来替代四个空格
当然也有可能是版本问题,因为3.0以后print 已经是一个函数了 因此必须print("msg")
age=25if age<18: print "你还年轻"elif 18<25: print "你不小了" else: print "要开始发力了" print "不管什么时候,理想都不能失去" #*****for*****for i in range(1,5): print i;else: print "结束for循环" #*****while***** i=15;while i<10: print i; i+=1;else:#当while一开始不满足的时候 就执行 "结束while循环"
结果
要开始发力了不管什么时候,理想都不能失去1234结束for循环
关于break与continue
#breakt="True";while t: t2=raw_input("enter your flag:"); if t2=="stop": break; print "continue"print "结束" while t: t2=raw_input("enter your flag:"); print type(t2) if t2=="stop": break; if len(t2)<5: continue; print "continue"print "结束"
3.变量
x=input("first input number:")y=input("second input number:")print x*yhello="hello"msg=hello+" world"print msg
结果:
first input number:10second input number:20200hello world
4.函数
在python的世界里,函数确切的定义应该是内建函数,比如一个人有听的功能
因此他不需要import
比如
r=pow(2,3)print r #结果8
5.模块
当内建函数不满足我们需求的时候,我们通过import来引入更多的函数
import mathmath.floor(32.9)#32.0 print int(math.floor(32.9))#32
当然,如果觉得每次输入math都嫌麻烦,可以
from math import floorfloor(r)
同样达到我们想要的效果
关于str与repr
'''str是给人看的repr是给解释器看的'''hello='hello world'print str(hello)#hello worldprint repr(hello)#'hello world'c=100Lprint str(c)#100print repr(c)#100L
关于python的整体把握,可以参考