python 学习笔记 ---- 数据类型

Python有五个标准的数据类型:
  • Numbers(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Dictionary(字典)
① List 列表 和 Tuple 元组
    Tuple 与 list 相似
     #!/usr/bin/python #
     -*- coding: UTF-8 -*-
     list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
     tinylist = [123, 'john']
    print list # 输出完整列表
    print list[0] # 输出列表的第一个元素
    print list[1:3] # 输出第二个至第三个元素
    print list[2:] # 输出从第三个开始至列表末尾的所有元素
    print tinylist * 2 # 输出列表两次
    print list + tinylist # 打印组合的列表
 
   访问列表中的值
   读取列表的值
   list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]
   list1[0] ,list2[1:5]
 
  更新列表
  list = [];
  list.append('Google')
  list.append(Runoob')
 
 
  删除列表元素
  del list1[2]
 
  python列表函数&方法
   cmp(list1,list2) 比较两个列表
   len(list) 列表元素个数
   min(list) max(list) ,list(seq)将元组转化为列表
  list.append(obj),list.count(obj),list.extend(seq), list.index(obj),list.insert(index,obj),list.remove(obj)
,list.reverse(),list.sort(func)
 
 
   ② Dictionary(字典)
       字典当中的元素是通过键来存取的,而不是通过偏移存取。
      字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
      #!/usr/bin/python # -*- coding: UTF-8 -* dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # 输出键为'one' 的值 print dict[2] # 输出键为 2 的值 print tinydict # 输出完整的字典 print tinydict.keys()          ##输出所有键 print tinydict.values() # 输出所有值
 
   ③ python 元组
       tup1 = (1, 2, 3, 4, 5 );
      访问元组
     print(tup1[0])
     修改元组
      tup1[0] = 100
      删除元组
     del tup1[0]
 
数据类型的强制转换
int(x),long(x),folat(x),complex(real),str(x),repr(x),eval(x),
tuple(x), list(x) set(x),
 
1. python 的数据库的处理
操作步骤 MySQLdb , pymysql
① 连接数据库
connect = pymysql.Connect(host, port, user, password, dbname)
② 获取游标
cursor = connect.cursor
③ 执行 sql 语句
cursor.execute(sql)
④ 事务性的数据库操作 ,commit后执行
connect.commit()
 
2. pymysql 参数说明
① pymysql.Connect() 参数说明
host(str), MYSQL服务器地址
port(int) MYSQL服务器端口号
user(str) ,passwd,db,charset
 
② connect 对象支持的方法
cursor() 创建并返回游标
commit() 提交当前事务
rollback() 回滚当前事务
close() 关闭连接
 
③ cursor对象支持的方法
execute(op) 执行一条sql语句
fetchone() 取得结果集的下一行
fetchmany(size) 获取结果集的下几行
fetchall() 取得结果集中的所有行
rowcount() 返回数据的影响行数
close() 关闭游标对象
 
 
 
3.读取键盘输入
(1) raw_input
(2) input
 
 
4. 文件file 对象
file object = open(file_name[, access_mode][, buffering])
file 对象的属性
① file.closed 判断文件是否关闭
② file.mode 返回被打开文件的访问模式
// 访问模式 r , w ,rb , r读, w 写,b 以二进制的形式
③ file.name 返回文件名
④ file.softspace
file对象的方法
① file.write() 写入文件
② file.read(count) 读取文件 ,读取count长度的字符串
③ file.tell() 获取当前指针位置 、
④ file.seek(offset, from) 改变文件指针位置,偏移量
⑤ file.flush() 刷新文件内部缓存
⑥ file.isatty() ,file.next(),
⑦ file.readline(size) 读取整行数据、
⑧ file.truncate(size) 截取文件
 
 
5 . python 处理json 数据
① json.dumps Python对象编码成JSON字符串
② json.loads JSON字符串解码为Python对象
 
 
原文地址:https://www.cnblogs.com/maomaochongchong/p/8341428.html