小驼峰式命名法(lower camel case): 第一个单词以小写字母开始;第二个单词的首字母大写,例如:myName、aDog
不过在程序员中还有一种命名法比较流行,就是用下划线“_”来连接所有的单词,比如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']
int、float、bool、complex
#type是python中查看数据类型的函数
a=1
b=0.45
c=True
print(type(a))
print(type(b))
print(type(c))
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
列表与元组支持下标索引好理解,字符串实际上就是字符的数组,所以也支持下标索引。
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 是否只包含字母
C语言中让变量i+1的方法:i++ ++i i+=1 i=i+1
而python中只有 i+=1 i=i+1
# 空字符串、空列表、空元组、空字典的布尔值是False
str1=""
str2="hello"
num1=0
print(bool(str1)) #False
print(bool(str2)) #True
print(bool(num1)) #False
name='lyf'
age=23
address='北京市海淀区'
print('我是%s,年龄%d岁,来自%s'%(name,age,address))
列表是由一系列特定顺序排列的元素组成,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()
元组和列表类似,不同之处在于元组不能增改,只能查看。(非要增删改怎么办?转为列表,操作完再转元组)元组用小括号创建,列表用方括号创建。
tup01=('lyf', 'hg', 'hjh', 'yh')
tup01=('lyf', 'hg', 'hjh', 'yh')
print(tup01[1]) #hg
#需要说明的是,元组里的某个元素不能单独增删改,这里我们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)
字典是一种可变容器类型,可以存储任意类型对象。
dict01={'name':'lyf','age':23,'address':'北京市海淀区'}
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)) #
#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', '北京市海淀区')])
字符串、Number、元组是不可变类型,
列表、字典是可变类型。
- 不可变对象在进行重新赋值的时候,实际上是将原始值丢弃,将变量指向一个新值
- 可变对象的可变性实际上是指更改可变对象中的子对象
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指向了同一个内存地址
'''
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内存地址不同
但是里面的元素内存地址相同
'''
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]]