Python入门
禁转
Python简单入门 这篇教程里有python数据类型及操作
python数据类型
空值:None
整型:16; 0b10000(二进制); 0x10(十六进制)
浮点型:0.1; 1e-1
字符型:‘hello’; “hello”; ‘’‘hello’’’; “”“hello”""
布尔型:True; False
列表:[1,‘a’,True,[1,2,3]](元素可以是任意类型)
元组:(1,2,3,True,[1,2,3])(不可修改)
集合:{1,2,3}
字典:{1:‘1’, ‘2’:‘2’, 3:‘3’}(键值对 键:值)
变量赋值:a=16
列表和元组的区别:列表可修改,元组不可
#添加
a = [1,2,3]
a.append(5)
a
####[1,2,3,5]
b = (1,2,3)
b.append(5) #error
集合操作:
c = {1,2,3}
#空集合
c = set() #注意不是c={},这是空字典
字典操作:
#空字典
d = {}
#添加
d = {'a':1, 'b':2}
d['c'] = 3
python语句
条件语句
if 3>2:
print('3>2')
else:
print('3<=2')
循环语句
# for循环
a = [1,2,3] #a为可迭代对象
for i in a:
print(i**2) #输出平方
# while循环
while True:
print('true')
函数
def foo():
return 1
a = foo()
a #1
类
#定义类
class Something(object):
def __init__(self):
self.bar = 'something'
#实例化
a = Something()
a.bar
#'something'
模块
#定义模块
def print_hello(name):
print "hello ", name
return