python-数据类型

一、python文件类型
 
1、源代码
以py为扩展名,不需要编译,由python程序解释
2、字节代码
源文件编译后会生成扩展名为“pyc”的文件;
编译方法:
import py_compile
py_compile.compile("hello.py")
3、优化代码
结果优化的源文件,扩展名为".pyo"
执行命令:
python -O -m pu_compile 1.py
 
上面三种文件都可以直接运行
 
2、python 变量
命令名称:字母、数字、下划线
不能以数字开头,不可使用关键字
 
变量的赋值
变量的声明和定义的过程
 
3、运算符
+ 、-、* 、/、//(整除、% 求余、**(幂运算)
除法运算符需要注意:
整数相除得到的也是整数:3/2=1
如果想要得到小数怎么办呢:3.0/2=1.5
整除法(//),相当于结果只取整数部分。
 
4、赋值运算符
=
-=
+=
*=
所有运算符加个=号就成了赋值运算符
5、关系运算符
<  >  <=  >=  !=  ==(恒等于)
关系运算符会返回运算结果。
例子:
>>> 1>2
False
 
6、运算符的优先级别(低到高)
lambda
or
and
not
in、not in
is、is not
<  <=  >  >=  !=  ==
|(按位或)
^(按位异或)
&(按位与)
<<   >>  (移位)
+ -
*、/、%
+x  -x(正负符号)
~ x(按位翻转)
** (指数)
 
 
 
6、逻辑运算符
and、or 、not
 
7、方法raw_input  从键盘上获取值
获取的是一个字符串,如果想要编程数字可以使用int
int(raw_input())
例子:
#!/usr/bin/python
a=int(raw_input("please input num1:"))
b=int(raw_input("please input num2:"))
 
print a+b
print a-b
print a*b
print a/b
 
8、id可查看变量的值在内存中的id,  type可查看变量类型
>>> id(num2)
32320136
>>> type(num2)
<type 'str'>
 
8、数据类型
 
数字:int(整型)、Long(长整型)、float(浮点型)、complex(复数类型)
python 中j表示复数的虚部 e表示实部
例子:
>>> b=8.9e+34j
>>> type(b)
<type 'complex'>
 
字符串
python中定义字符串,可以使用单引号、双引号、三重引号
例子:
>>> str1='he'
>>> str2="ll"
>>> str3="""wo"""
>>> type(str1)
<type 'str'>
>>> type(str2)
<type 'str'>
>>> type(str3)
<type 'str'>
这三种方式没有任何区别
有趣的事:
>>> say='let's go'
SyntaxError: invalid syntax
 
这种情况会报错,应为系统会认为单引号需要成对出现,因此报错,这种时候只能使用双引号啦
>>> say="let's go"
>>> type(say)
<type 'str'>
 
当然啦,也可以使用转义符号处理了
>>> say2='let\'s go'
>>> type(say2)
<type 'str'>
>>> print say2
let's go
 
换行符号:\n
但是使用换行符号太费劲了,还要时刻想着什么时候输入换行符
python中一般使用三重引号来实现格式化输出。
>>> mail=""" tom:
   i am yasmine
   how are you ?
   """
>>> print mail
 tom:
   i am yasmine
   how are you ?
 
字符串可以通过索引取值:
>>> mail=""" tom:
   i am yasmine
   how are you ?
   """
>>> print mail
 tom:
   i am yasmine
   how are you ?
 
 
python可以通过索引取字符
>>> c='2343432'
>>> c[1]
'3'
>>> c[2]+c[5]
'43'
 
 
序列类型:列表、元组、字符串都是序列类型,序列类型的特点就是可以使用索引和切片。
序列的基本操作:
1、求序列长度: len()
2、连接2个序列:-
3、重复序列:*  
4、判断元素是否包含在序列中:in
5、返回最大值和最小值:max()、min()
6、比较两个序列值是否相同:cmp(str1,str2)
 
元组:()和逗号分隔进行定义
例子:
>>> t=("myname",43,"woman")
>>> t[0]
'myname'
空元组:t=()
一元组:t=(1,)   一定要有一个逗号
 
列表list:列表使用[ ],包含多个以逗号分开的数字、字符串
例子:
>>> list1=[]
>>> type(list1)
<type 'list'>
 
>>> list2=["myname",43,"woman"]
>>> type(list2)
<type 'list'>
>>> t=("myname",43,"woman")
>>> type(t)
<type 'tuple'>
可看出列表和元组在定义时没有任何区别。
列表的操作方法:
1、取值:
切片和索引
list[]
2、添加
list.append()
例子:
>>> list2.append("4343")
>>> list2
['rrfds', 43, 'woman', '4343']
3、删除
del(list[]) ---系统函数
list.remove(list[])  ---list类的函数
 
例子:
>>> list2.remove(list2[3])
>>> list2
['rrfds', 43, 'woman']
4、修改
list[]=x
5、查找
var in list
 
 
字典:python中比较重要的类型
1、zip(list1,list2)可以使用该函数将连个列表对应起来
例子:
>>> list1=['name','age','gender']
>>> list2=['jim',20,'man']
>>> zip(list1,list2)
[('name', 'jim'), ('age', 20), ('gender', 'man')]
 
2、字典{}是python中唯一的映射类型(哈希表)
字典对象时可变的,但是字典的键必须使用不可变对象。
keys()或者values()返回键列表或值列表
items()返回包含键值对的元组
 
例子:
>>> 
>>> dic1={'name':'jim','age':20,'gender':'man'}
>>> dic1
{'gender': 'man', 'age': 20, 'name': 'jim'}
冒号前面为key,冒号后面为value
 
例子:可使用变量作为key进行定义
>>> name='jim'
>>> dic2={name:'hanmm','age':18,'gender':'woman'}
>>> dic2
{'gender': 'woman', 'age': 18, 'jim': 'hanmm'}
 
访问字典中值得方法:
1)直接使用key访问:key不存在会报错,可以使用had_key()或者in,not in判断
2)循环遍历
例子:for key in dict1.key():
3)使用迭代器:for key in dict1:
 
例子:
>>> dic1
{'gender': 'man', 'age': 20, 'name': 'jim'}
>>> for k in dic1:
print k
 
 
gender
age
name
>>> 
>>> for k in dic1:
print dic1[k]
 
 
man
20
jim
>>> 
 
 
字典基本操作:
1)新增key值:
例子:通过该例子可以看出字典是无序的
>>> dic1
{'gender': 'man', 'age': 20, 'name': 'jim'}
>>> dic1['tel']='443899024'
>>> dic1
{'gender': 'man', 'age': 20, 'tel': '443899024', 'name': 'jim'}
 
2)修改value值
>>> dic1['tel']='7890'
>>> dic1
{'gender': 'man', 'age': 20, 'tel': '7890', 'name': 'jim'}
 
3)删除字典元素
例1:使用函数del
>>> del(dic1['tel'])
>>> dic1
{'gender': 'man', 'age': 20, 'name': 'jim'}
例2:使用dic1.pop('key') 删除并返回键‘key’的元素
例3:使用dic1.clear() 删除字典所有元素
例4:del dict1 删除整个字典
 
总结字典常用方法:
1、dic.get(key,返回值) 通过该方法可以取字典中的值,如果存在者返回value,不存在在返回给定的“返回值”
例子:
>>> dic1
{'gender': 'man', 'age': 20, 'name': 'jim'}
>>> dic1.get('age','error')
20
>>> dic1.get('tel','error')
'error'
2、len(),hash(),用于判断某个对象是否可以作为一个字典的key值,非hash类型报typeError
3、dict.fromkeys(seq,val=None):以seq中的元素为键创建并返回一个字典,value为制定的默认值。
4、dict.has_key():判断字典是否存在该key,建议使用in,not in
5、dict.items():返回key值对元组的列表
6、dict.keys():返回字典中键的列表
7、dict.tier*():iteritems(),iterkeys(),itervalues()返回迭代子而不是列表
8、dict.setdefault(key,difault=None):同set
9、dict.update(dict1):将dict1中的键值对添加到字典dict中,如果有重复覆盖,原字典不存在的条目添加进。
10、dict.values():返回字典中所有的列表。
 
 
 
 
 
 
 
 
 
 
 
 
常见报错日志:
1、NameError: name 'c' is not defined
变量、函数或类的名称不正确
 
2、TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'builtin_function_or_method'
类型不正确
 
3、TypeError: descriptor 'remove' requires a 'list' object but received a 'str'
提示删除操作应该是一个list类型,但是我给的是一个str类型
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值