python3 笔记,,.1# 数的定义:#


.1# 数的定义:
#

1.整型:int 如 1

2.长整型 long ,是比较大的整数

3.浮点数float/double 如:1.1

4.复数: a+bj 如1+2j

格式化:

format ---字符串拼接

%d,%f,%s 格式化输出

str()、int()、float()强转类型函数

#age=int(input(‘请输入您的年龄:‘))
#print ( age-1)

#强转化的函数例子
#age=int(input(‘请输入您的年龄:‘))
#print ( ‘你的周岁是:‘+str(age-1))

#age=input(‘请输入你的年龄:‘)
#print(‘你的周岁是:‘+str(int(age)-1))
强转类型的函数总结(int、float、str)
1.如果想要将一个整数和浮点与一个字符串连接,str()函数比较方便
2.如果有一个些字符串,希望将他们用于数值运算,int()函数比较方便
3.如果将一个不能求值为整数的值传递给int(),python报错valueErro.
4.如果需要对浮点数进行取整运算,也可以int()函数。

#编写一个程序(可以询问姓名和年龄,并且告知姓名的字符个数和你明年的年龄)

print(‘welcome‘)

name=str(input(‘what\‘s your name:‘))

print(‘很高兴见到你:‘+name)

age=int(input(‘你的年龄:‘))

print (‘你的姓名字符个数:‘+str(len(name)))

print(‘你明年的年龄:‘+str(age+1))

#综合数据类型-list
列表 list 是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。
常规操作: 访问列表中的值 、更新列表、删除列表元素、列表函数&方法。
使用方括号定义列表。 type() 查看类型

>> str(11)
‘11‘
>> float(11)
11.0
定义列表:
>> list1=[1,1,2,3,]
>> list1
[1, 1, 2, 3]
查看类型:
>> type(list1)
<class ‘list‘>
>>

访问列表:

>> len(list1)
4
>> print(list1[2])
2
>> print(list1[1])
1
>> print(list1[0])
1
双重列表:
>> list2=[1.,2.,3.,4.]
>> list3=[list1,list2]
>> print(list1)
[1, 1, 2, 3]
>> print(list2)
[1.0, 2.0, 3.0, 4.0]
>> print(list3[0])
[1, 1, 2, 3]
>> print(list3[0] [2])
2
>> print(list3[1] [2])
3.0
>> print(list3[1] [2])
3.0
>> print(list3[1] [3])
4.0
>> print(list3)
[[1, 1, 2, 3], [1.0, 2.0, 3.0, 4.0]]
>> list3
[[1, 1, 2, 3], [1.0, 2.0, 3.0, 4.0]]

更新列表:

>> list3[0]=[1,2,3,4,5,6,7,8,]
>> list3
[[1, 2, 3, 4, 5, 6, 7, 8], [1.0, 2.0, 3.0, 4.0]]
>> del list3[0]
删除列表
>> list3
[[1.0, 2.0, 3.0, 4.0]]
>>

插入数据列表:

>> list1=[1,2,3,4]
>> list1.append(‘11‘)
>> list1
[1, 2, 3, 4, ‘11‘]

统计元素出现的次数

>> print(list1.count(2))
1
追加列表
>>list1.extend[1,2,3]
>>list1
[1,2,3,4,11,1,2,3]

在定制的索引位置添加元素

>>list1.insert(4,‘22‘)
>>list1
[1,2,3,4,‘22‘,11,1,2,3]

删除指定坐标的元素

>> list1.pop(2)
3
>> list1
[1, 2, 4, ‘22‘, 11, 1, 2, 3]
>>

remove移除列表某个值第一次匹配的项

>>list1.remove(1)
>>list1
[2, 4, ‘22‘, 11, 1, 2, 3]

方向列表

>> list1.reverse()
>> list1
[3, 2, 1, 11, ‘22‘, 4, 2]

排序列表 sort

>> list1.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()
报错,因为列表有字符串,先删掉字符在排序而python2不会报错这样错的。
>> list1
[1, 2, 3, 11, ‘22‘, 4, 2]
指定删除某个元素
>> list1.pop(4)
‘22‘
>> list1
[1, 2, 3, 11, 4, 2]
>> list1.sort()
>> list1
[1, 2, 2, 3, 4, 11]
>>

切片:

>> list1=[1,2,2,3,4]
>> print(list1[2:4])
[2, 3]
>> print(list1[1:4])
[2, 2, 3]
>> print(list1[-2:-4])
[]
>> print(list1[-4:-2])
[2, 2]
>>

元祖 tuple
python的元组合列表类似,不同之处在于元组的元素不能修改。
常规操作:
访问元组、修改元组、删除元组 、无关闭分隔符、内置函数
2.定义一个值得元祖

>> t2=(9)
>> t2
9
>> type(t2)
<class ‘int‘>
定义元祖
>> t3=()
>> type(t3)
<class ‘tuple‘>
>> t1=(1,2,3,4,4)
>> type(t3)
<class ‘tuple‘>
>> t3
()
>> t1
(1, 2, 3, 4, 4)
>>

访问元组

>> t1
(1, 2, 3, 4, 4)
>> print (t1[1])

修改元组元素 不能修改(保证数据安全),可以先转化为列表 在修改后在转元组。

>> egg=(‘hello‘,2,3,4,5)
>> egg[1]=99
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘tuple‘ object does not support item assignment

删除元组

>> del t1
>> t1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘t1‘ is not defined
>>

无关闭分隔符。

>> type((‘hello‘))
<class ‘str‘>
>> type((‘hello‘,))
<class ‘tuple‘>

多元素赋值

>> x,y,z=(1,2,3)
>> print(x,x,y)
1,2,3

例题:
x=2,y=3 进行互换:
1.使用变量,进行赋值

>> x=2
>> y=3
>> z=x
>> x
2
>> z
2
>> x=y
>> x
3
>> y=z
>> y
2

B.使用

>> x=2
>> y=3
>> x,y=(y,x)
>> print(x,y)
3 2

元组的优势
1.可以用元组告诉所有读代码的人,你不打算改变这个序列的值
如果需要一个永远不改变的值的序列,就使用的元组
2.因为元组的不可变性,内容不会变化的,python可以实现一些优化,让使用元组的代码闭
使用列表代码更快。

综合数据类型-dict

字典-dict

字典是另一种可变容器模型,且可存储任意类型对象。
常规操作。
访问字典里的值
修改字典
删除字典元素
字典键的特性
字典内置函数&方法

-花括号定义 键值对形式存在

>> dict={‘a‘:1,b:2}
>> type(dict)
<class ‘dict‘>

在字典中,键是唯一的,如果存在两个一样的键,后者会替代前者
#键不可变得,可以使用数字,字符串,元组,但是列表不行
访问字典

>> print(dict2[‘a‘])
4

如果访问不存在的键,会报错keyerro.

>> dict1[‘c‘]+dict1[‘a‘]+dict1[‘b‘]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: ‘a‘

--->>在列表中不存在的下标不能进行元素的修改。
----》》在字典中可以直接设置一个不存在的键,并且给他一个值

>> list1[6]=10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

>> dict1
{‘c‘: 1, ‘b‘: 2}
>> dict1[‘a‘]=dict1[‘c‘]+dict[‘b‘]
>> dict1
{‘c‘: 1, ‘a‘: 21, ‘b‘: 2}

删除字典中的元素

>> del dict2[‘c‘]
>> dict2
{‘a‘: 4, ‘b‘: 2}

清空:dict2.clear()

>> dict2.clear()
>> dict2
{}

字典函数:
创建一个字典,以序列中的元素作为新字典的键,val作为字典的使用键的初始值。
dict.fromkeys(seq,val)
如:

>> dict3={}
>> dict4=dict3.fromkeys([1,2,3,4],0)
>> dict4
{1: 0, 2: 0, 3: 0, 4: 0}
>> dict3
{}

当访问一个不存在键会报错,
为了避免返回错误,影响程序正常的运行
dict.get(key,default=value)
返回指定键的值,如果键不存在,返回default默认值value

>> dict5={‘a‘:1,‘b‘:2}
>> print(dict5.get(‘b‘,‘没有这个值‘))
2
>> print(dict5.get(‘c‘,‘没有这个值‘))
没有这个值
>>

ID 名字 年龄
1 后裔 24
2 嫦娥 30
3 露露

>> dict1={1:{‘ID‘:‘001‘,‘名字‘:‘后裔‘,‘年龄‘:24},2:{‘ID‘:‘002‘,‘名字‘:‘嫦娥‘,‘
年龄‘:30},3:{‘ID‘:‘003‘,‘名字‘:‘露露‘}}
>> print(dict1[3].get(‘年龄‘,25))
25

9.与get类似,区别键不存在,增加键,并赋值。
dict.setdefault(key,value)

>> print(dict1.setdefault(‘年龄‘,18)) *与dict1 元素并列了*** .
18
>> dict1
{1: {‘名字‘: ‘后裔‘, ‘年龄‘: 24, ‘ID‘: ‘001‘}, 2: {‘名字‘: ‘嫦娥‘, ‘年龄‘: 30, ‘
ID‘: ‘002‘}, 3: {‘名字‘: ‘露露‘, ‘ID‘: ‘003‘}, ‘年龄‘: 18}
>> del dict1[‘年龄‘]
>> print(dict1[3].setdefault(‘年龄‘,18))
18
>> dict1
{1: {‘名字‘: ‘后裔‘, ‘年龄‘: 24, ‘ID‘: ‘001‘}, 2: {‘名字‘: ‘嫦娥‘, ‘年龄‘: 30, ‘
ID‘: ‘002‘}, 3: {‘名字‘: ‘露露‘, ‘年龄‘: 18, ‘ID‘: ‘003‘}}
>>

判断字典中是否存在某个键

>> dict1
{‘a‘: 0, ‘b‘: ‘meiyou‘}
>> print(‘a‘ in dict1)
True

返回字典中所有的键、所有的值。
dict.keys()/dict.values()

>> dict7={0:[1,2,3],1:[2,3,4],2:[4,5,6]}
>> print(dict7.keys())
dict_keys([0, 1, 2])
>> print(dict7.values())
dict_values([[1, 2, 3], [2, 3, 4], [4, 5, 6]])

把字典中键/值更新到另一个字典中。
dict.update()

>> dict1_1={‘a‘:1,‘b‘:2}
>> dict1_2={‘c‘:3,‘d‘:4}
>> dict1_1.update(dict1_2)
>> dict1_1
{‘a‘: 1, ‘b‘: 2, ‘d‘: 4, ‘c‘: 3}
>>

数据综合类型-set&序列、切片
A.序列和分片
str1=‘my name is leon‘
#1.取得name子串

>> str1=‘my name is leon‘
>> print(str1[3:7])
name

>> print(str1[-12:-8])
name
#2.取得leon子串
>> print(str1[11:])
leon
>> print(str1[-4:])
leon

F.集合 set
集合(set)和字典(dict)类似,它是一组key的集合,但不存储value.
集合的特性就是:key不能重复,唯一。
常用操作:
创建集合
遍历集合
添加元素
交集、并集、差集

-花括号定义-

>> s1={‘a‘,‘b‘,‘c‘}
>> type(s1)
<class ‘set‘>

>> s2=set(‘abc‘)
>> s2
{‘a‘, ‘b‘, ‘c‘}
>> type(s2)
<class ‘set‘>
访问 通过循环结构
>> for e in s2 :print(e)
...
a
b
c
转化列表在访问
>> list1=list(s2)
>> print(list1[0])
a

增加元素

>> s3=set(‘abcder‘)
>> s3.add(‘g‘)
>> s3
{‘d‘, ‘r‘, ‘g‘, ‘e‘, ‘b‘, ‘c‘, ‘a‘}
>>
删除元素
>> s3.remove(‘a‘)
>> s3
{‘d‘, ‘r‘, ‘g‘, ‘e‘, ‘b‘, ‘c‘}

交集、并集,差集
交集

>> set4_1=set(‘acvsdf‘)
>> set4_2=set(‘aacvskk‘)
>> print(set4_1&set4_2)
{‘a‘, ‘v‘, ‘s‘, ‘c‘}
>>
并集:
>> print(set4_1|set4_2)
{‘d‘, ‘k‘, ‘v‘, ‘c‘, ‘f‘, ‘a‘, ‘s‘}
>>

差集

>> print(set4_1-set4_2)
{‘d‘, ‘f‘}
>>
>> print(set4_2-set4_1)
{‘k‘}

python3 笔记

评论关闭