网站建设方案预算,wordpress前端会员,南京建站公司模板,wordpress外链mp4一、变量的命名和使用
变量名只能包含字母、数字和下划线。变量名可以字母或下划线开头#xff0c;不能以数字开头变量名不能包含空格#xff0c;但可使用下划线分隔其中的单词。不要将Python关键字和函数名用做变量名。不要使用Python保留用于特殊用途的单词#xff0c;如…一、变量的命名和使用
变量名只能包含字母、数字和下划线。变量名可以字母或下划线开头不能以数字开头变量名不能包含空格但可使用下划线分隔其中的单词。不要将Python关键字和函数名用做变量名。不要使用Python保留用于特殊用途的单词如print变量名应简短又具描述性。慎用大写字母I和大写字母O因为它们可能被人看成数字的1和0。
二、字符串
Python中用引号括起来的都是字符串其中引号可以是单引号也可以是双引号。
使用方法修改字符串的大小写
name helLO woRLd
print(name.title())
print(name.upper())
print(name.lower())
title()方法以首字母大写的方式显示每个单词即将每个单词的首字母都改为大写。
合并/拼接字符串
Python使用合并字符串称为拼接。
使用制表符或换行符添加空白
\n和\t
删除空白
要确保字符串末尾没有空白使用rstrip()
也可以删除字符串开头的空白或者同时删除开头和结尾的空白可以使用lstrip()和strip()。
三、数字
整数
python中可对整数执行-*/运算。两个乘号**表示乘方。
python支持运算次序可以使用括号修改运算次序。
浮点数
python将带小数点的数字都称为浮点数。需要注意的是结果包含的小数位可能是不确定的。
print(0.2 0.1)
print(3 * 0.1)
所有语言都存在这种问题不用担心。
使用函数str()避免类型错误
age 23
# message Happy age rd Birthday!
message Happy str(age) rd Birthday
print(message)
四、列表
列表由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表也可以将任何东西加入列表其中的元素之间可以没有任何关系。使用复数名词表示列表的变量名。
python中使用方括号[]表示列表并用逗号分隔其中的元素。
bicycles [trek, cannondale, redline, specialized]
print(bicycles)
访问列表元素
使用元素的位置或索引访问列表元素
bicycles [trek, cannondale, redline, specialized]
print(bicycles[0])
或者使用title()方法美化输出
bicycles [trek, cannondale, redline, specialized]
print(bicycles[0].title())
访问最后一个列表元素可以使用-1索引
bicycles [trek, cannondale, redline, specialized]
print(bicycles[-1])
修改、添加和删除元素
motorcycles [honda, yamaha, suzuki]
print(motorcycles)motorcycles[0] dayun
print(motorcycles)
添加元素
列表末尾添加
append()方法将元素添加到列表的结尾
motorcycles [honda, yamaha, suzuki]
print(motorcycles)motorcycles.append(dayun)
print(motorcycles)
创建空列表使用append()方法添加若干元素到列表
motorcycles []
motorcycles.append(dayun)
motorcycles.append(jianshe)
motorcycles.append(chunlan)print(motorcycles)
在列表中插入元素
insert()方法在指定索引处插入元素将原来该位置以及其后的元素都右移一个位置。
motorcycles [honda, yamaha, suzuki]motorcycles.insert(1, dayun)
print(motorcycles)
从列表删除元素
使用del语句删除元素
motorcycles [honda,yamaha,suzuki]
print(motorcycles)del motorcycles[1]
print(motorcycles)
使用pop()方法删除元素
pop()方法删除列表末尾的元素并让你能够接着使用它。弹出pop源于类比列表就像一个栈删除列表末尾的元素相当于弹出栈顶元素
motorcycles [honda, yamaha, suzuki]
print(motorcycles)popped_motorcycle motorcycles.pop()
print(motorcycles)
print(popped_motorcycle )
弹出列表中任何位置的元素
motorcycles [honda, yamaha, suzuki]
print(motorcycles)popped_motorcycle motorcycles.pop(1)
print(motorcycles)
print(popped_motorcycle)
根据值删除元素
使用remove()方法删除
motorcycles [chunlan, yamaha, dayun, jianshe]
print(motorcycles)motorcycles.remove(yamaha)
print(motorcycles)