day02-python数据类型和运算符

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GXH_APOLOGIZE/article/details/81773566
1、注释

image

2、变量

image

3、标识符和关键字
  • 标识符由字母、下划线和数字组成,且数字不能开头。
  • python区分大小写。
  • 不能使用python关键字作为变量名
  • 命名规则有小驼峰、大驼峰

    小驼峰式命名法(lower camel case): 第一个单词以小写字母开始;第二个单词的首字母大写,例如:myName、aDog


大驼峰式命名法(upper camel case): 每一个单字的首字母都采用大写字母,例如:FirstName、LastName

不过在程序员中还有一种命名法比较流行,就是用下划线“_”来连接所有的单词,比如send_buf
- 关键字有哪些

['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']
4、数据类型
  • Number
intfloatboolcomplex
  • String字符串
  • List列表
  • Tuple元组
  • Sets集合
  • 字典
#type是python中查看数据类型的函数
a=1
b=0.45
c=True
print(type(a))
print(type(b))
print(type(c))
5、字符串
  • 字符串的声明可以用单引号和双引号
a='hello'
b="100"
print(type(a))
print(type(b))
  • 如果字符串本身有单引号,那么就用双引号声明;如果字符串本身有双引号,那么就用单引号声明。否则转义字符。
a='hello"'
b="'100"
c='hand\'some'
print(a)
print(b)
print(c)

hello"
'100
hand'some

  • 下标

    列表与元组支持下标索引好理解,字符串实际上就是字符的数组,所以也支持下标索引。


image
- 切片
切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作 ![image](http://t1.aixinxi.net/o_1cku51v8qijhhfl1bee1pjs1c1ka.png-j.jpg)
  • 字符串的常见操作
find:查找一个指定字符串是否包含在str字符串中,如果是则返回开始的索引值,如果不是则返回-1
index用法与find类似,只是指定字符串不在str字符串中时会直接异常。
count:返回查找字符串出现的次数


str='abcdefgac'
print(str.find('a')) #0
print(str.find('l')) #-1
#print(str.index('l')) #ValueError: substring not found
print(str.count('a')) #2
print(str.count('c',0,2)) #0 位置0-2之间c出现的次数
print(str.replace('a','v')) #vbcdefgvc
print(str.split('d'))  #['abc', 'efgac']
print(str.capitalize()) #Abcdefgac
print(str.upper()) #ABCDEFGAC
print(str.title()) #Abcdefgac 每个单词首字母大写
print(str.startswith('b')) #False
print(str.endswith('c'))  #True
print(str.center(13))
print(str.isspace()) #False 是否只包含空格
print(str.isalnum()) #True 是否全部是字母或数字
print(str.isdigit()) #False 是否只包含数字
print(str.isalpha()) #True 是否只包含字母
6、运算符
  • 算数运算符
  • 位运算
  • 赋值运算符
C语言中让变量i+1的方法:i++    ++i     i+=1    i=i+1
而python中只有 i+=1    i=i+1
  • 比较运算符和逻辑运算符
    image
# 空字符串、空列表、空元组、空字典的布尔值是False
str1=""
str2="hello"
num1=0
print(bool(str1))  #False
print(bool(str2))  #True
print(bool(num1))  #False
7、输入输出

image

name='lyf'
age=23
address='北京市海淀区'
print('我是%s,年龄%d岁,来自%s'%(name,age,address))
8、列表

列表是由一系列特定顺序排列的元素组成,python里面的列表的元素可以是不同类型的,并且元素之间可以没有任何关系。

字符串的下标和切片在列表中也可以使用。

  • 列表的创建
list01=['lyf,hg,hjh,yh']
  • 通过下标访问
list01=['lyf','hg','hjh','yh']
print(list01[0])  #lyf

list02=['lyf','hg','hjh',['haha','hehe','xixi'],'yh']
print(list02[3])  #['haha,hehe,xixi']
print(list02[3][1])   #hehe
  • 修改列表元素
list01=['lyf','hg','hjh','yh']
list01[1]='jjf'
print(list01) #['lyf', 'jjf', 'hjh', 'yh']
  • 添加列表元素
list01=['lyf','hg','hjh','yh']
list01.append('jjf') #列表末尾添加元素
list01.insert(2,'jjf') #在指定位置(2)添加元素
print(list01)
  • 删除列表元素
list01 = ['lyf', 'hg', 'hjh', 'yh']
print(list01.pop(1))  #hg
print(list01.pop())  #yh
print(list01) #['lyf', 'hjh']
list01.remove('hjh')
print(list01) #['lyf']
del list01
print(list01)  #NameError: name 'list01' is not defined

#pop方法删除
#del语句删除,元素一旦删除之后就无法访问
#remove方法删除
  • 查找列表元素
list01 = ['lyf', 'hg', 'hjh', 'yh']
name='hjh'
print(name in list01) #True
print(name not in list01) #False
  • 其它常见
list01 = ['lyf', 'hg', 'hjh', 'yh']
list02=['haha','hehe']
#列表长度
print(len(list01)) #4
#统计某个元素在列表出现的次数
print(list01.count('hg')) #1
#在列表末尾一次性追加新列表
list01.extend(list02)
print(list01) #['lyf', 'hg', 'hjh', 'yh', 'haha', 'hehe']
#某个元素第一次出现的位置,找不到会抛异常ValueError
print(list01.index('hg')) #1
#清空列表
list01.clear()
9、元组

元组和列表类似,不同之处在于元组不能增改,只能查看。(非要增删改怎么办?转为列表,操作完再转元组)元组用小括号创建,列表用方括号创建。

  • 元组的创建
tup01=('lyf', 'hg', 'hjh', 'yh')
  • 通过下标访问
tup01=('lyf', 'hg', 'hjh', 'yh')
print(tup01[1]) #hg
  • 通过del语句删除
#需要说明的是,元组里的某个元素不能单独增删改,这里我们del删除是删除整个元组
tup01=('lyf', 'hg', 'hjh', 'yh')
print(tup01[1]) #hg
del tup01
  • 其它操作
tup01=('lyf', 'hg', 'hjh', 'yh')
list01 = ['lyf', 'hg', 'hjh', 'yh']
print(len(tup01)) #4
print(max(tup01))
print(min(tup01))
#将列表转元组
tup02=tuple(list01)
print(tup02)
#将元组转列表
list01=list(tup02)
10、字典

字典是一种可变容器类型,可以存储任意类型对象。

  • 字典的创建
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
  • 通过key当下标来访问
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
print(dict01['name'])  #lyf
  • 修改元素
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
print(dict01['name'])  #lyf
dict01['name']='hg'
  • 添加元素
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
dict01['hobby']='volleyball'
  • 删除元素
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
#del删除元素
del dict01['address']
print(dict01)
#del删除字典,内存删除
del dict01
#clear删除字典,剩下一个空字典
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
dict01.clear()
  • 其它操作

dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
print(len(dict01))  #3
#字典转字符串
str1=str(dict01)
print(type(str1)) #<class 'str'>
#get方法获取值,可指定默认值
print(dict01.get('name'))  #lyf
print(dict01.get('gender'))  #None
print(dict01.get('gender','男'))  #男
#判断指定key是否存在字典中
print('name' in dict01) #True
#获取所有key
print(dict01.keys()) #dict_keys(['name', 'age', 'address'])
#获取所有值
print(dict01.values())  #dict_values(['lyf', 23, '北京市海淀区'])
#将键值对保存到元组返回
print(dict01.items())
#dict_items([('name', 'lyf'), ('age', 23), ('address', '北京市海淀区')])
11、可变类型、不可变类型

字符串、Number、元组是不可变类型,
列表、字典是可变类型。
- 不可变对象在进行重新赋值的时候,实际上是将原始值丢弃,将变量指向一个新值
- 可变对象的可变性实际上是指更改可变对象中的子对象

12、浅拷贝与深拷贝
  • 直接赋值:其实就是对象的引用(别名)
a=[1,2,3]
b=a
print(id(a)) #212360938888
print(id(b)) #212360938888
a[0]=4
print(a) #[4, 2, 3]
print(b) #[4, 2, 3]
'''
a和b指向了同一个内存地址
'''
  • 浅拷贝:不拷贝子对象(针对子对象中的item),当子对象进行更改的时候,原始对象也会改变。常见操作:列表切片、list()、字典的copy()函数、copy模块的copy()函数。
a=[1,2,3]
b=[11,22,33]
c=[111,222,333]
list01=[a,b,c]
print(list01) #[[1, 2, 3], [11, 22, 33], [111, 222, 333]]
list02=list01[:]
a[0]=5
print(list01) #[[5, 2, 3], [11, 22, 33], [111, 222, 333]]
print(list02) #[[5, 2, 3], [11, 22, 33], [111, 222, 333]]
print(id(list01)) #447431689160
print(id(list02)) #447431689416
print(id(list01[0]))  #447431689608
print(id(list02[0]))  #447431689608
'''
list01和list02内存地址不同
但是里面的元素内存地址相同
'''
  • 深拷贝:会拷贝子对象。常见操作是copy模块的deepcopy()函数。
import copy

a=[1,2,3]
b=[11,22,33]
c=[111,222,333]
list01=[a,b,c]
list02=copy.deepcopy(list01)
print(id(list01)) #166895348232
print(id(list02)) #166895371144
a[0]=5
print(list01)  #[[5, 2, 3], [11, 22, 33], [111, 222, 333]]
print(list02)  #[[1, 2, 3], [11, 22, 33], [111, 222, 333]]

猜你喜欢

转载自blog.csdn.net/GXH_APOLOGIZE/article/details/81773566
今日推荐