Python学习第一课

字符串

修改字符串大小写

-首字母大写

list = "hello"
print(list.tilte())

-全部大写

upper()

-全部小写

lower()

合并拼接字符串

-使用+号

使用制表符或者换行符号来添加空白

-制表符:\t
-换行符:\n

删除空白

-删除字符串末尾空白:

list = " hello "
list.rstrip()

-删除字符串开头空白:

list = " hello "
list.lrstrip()

-删除字符串中的全部空白:

list = " hello "
list.strip()

引号的使用

-引号嵌套使用的时候必须使用不用的引号
正确的:

list = "他说:他的外号叫'大牛'"

错误的:

list = "他说:他的外号叫"大牛""

常用语法

print

-在Python2中:

print "hello"

-在Python3中:

print("hello")

数字

整数

加+

减-

乘*

除/

乘方**

浮点数

-就是小数

把其他的变量类型转换成字符串:str()

注释

-用#

列表:list[]

访问列表元素,列表名称[索引]

list = [1,2,3,4,5,65,8]
print(list[3])

-可以混用titl()函数

索引从0开始,负数代表反向访问

修改添加和删除元素

修改

list = [1,2,3,4,5,65,8]
list[2] = 100

添加

追加

append()

插入

inser(5,"hello")

删除元素

使用del删除

del list[0]

使用pop弹出元素

list.pop()

使用pop弹出任意位置元素

list.pop(4)

根据list的值删除元素

list = [1,2,3,4,5,65,8]
list.remove(65)
print(list)

组织列表

永久排序

-正向排序

list.sort()

-反向

list.sort(reverse=True)

临时排序

list.sorted()#反向:list.sorted(reverse=True)

列表长度

len(list)

查找倒数第一个元素用-1

操作列表

-遍历列表:for

for i in list:

posted @ 2018-03-15 19:13  李有才  阅读(247)  评论(0)    收藏  举报