美文网首页
Python序列类型的方法及类型转换

Python序列类型的方法及类型转换

作者: 子非初心 | 来源:发表于2017-11-08 22:57 被阅读37次

Python序列类型的方法及类型转换

0.上节课整理

1.声明变量,变量的三个属性,变量的命名规则,什么是变量?
2.数值类型(4种),以及常用运算符
3.三种序列类型,如何定义?可变与不可变
4.序列类型的取值,(索引,切片,步长) # 正负只是决定取值方向
4.如何判断可变与不可变
5.dir(),help(),type(),id(),print()   内置函数
常见的错误与问题:
1.print与idle直接运行
2.NameError:'x' is not defined
3.各种弹出提示框的报错:没注释掉非代码片段,少了符号,中文符号。
4.shell中返回值和print都会被打印出来,但是你可以用一个变量得到返回值打印,但是你用
print就不能用一个变量来得到print打印的内容
1.list的常用方法
2.字符串常用的方法
3.序列类型的转换及成员运算

1.list的常用方法

列表方法不多,每一种都会讲解

dir(list)可以查看列表的所有方法
 L.append(object) -> None -- append object to end
 L.clear()        -> None -- remove all items from L
 L.copy()         -> list -- a shallow copy of L
 L.count(value)   -> integer -- return number of occurrences of value
 L.extend(iterable)-> None -- extend list by appending elements from the iterable
 
 L.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.
 L.insert(index, object) -- insert object before index
 L.pop([index]) -> item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.
 
 L.remove(value) -> None -- remove first occurrence of value.
    Raises ValueError if the value is not present.
 L.reverse() -- reverse *IN PLACE*
 L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
 附加:
    内置函数:sorted() reversed()

2.str常用的方法

字符串的方法有44种,本节课只会挑选常用的讲解,但会给大家一份文件,里面有44中字符串方法的讲解。

S.count(sub[, start[, end]]) -> int
S.find(sub[, start[, end]]) -> int
    Return -1 on failure.
S.index(sub[, start[, end]]) -> int 
    Like S.find() but raise ValueError when the substring is not found.
S.endswith(suffix[, start[, end]]) -> bool
S.startswith(prefix[, start[, end]]) -> bool
S.isalpha() -> bool
S.isdigit() -> bool
S.lower() -> str
S.upper() -> str
S.replace(old, new[, count]) -> str
S.split(sep=None, maxsplit=-1) -> list of strings
S.join(iterable) -> str

数值类型主要是用来是做运算的,所以还会有如下的运算符

#数值运行常用运算符
+ - * / //(整除,向下取整) %(取余) ** (幂)

#赋值运算符   
=  +=  -=   *=  /=  ....

基本数值类型中前三种是常用的,复数在数学里面用得多,但是一般是用不上的。

3.数据类型的转换及成员运算

01.类型转换

在python中三种序列类型,序列类型简单来说就是对象里面的元素是有顺序的。

既然都是有序,那么这三者之间可以相互转换吗?

字符串 str
列表   list
元组   tuple
可变序列:list
不可变序列:tuple,str
序列类型转换:
    str(),list(),tuple()
数值类型转换:
    int(),float(),complex(),bool()

序列类型做为基本的数据类型是大家必须要掌握的,在python中大家会经常见到这三种数据类型。

02.总说运算符
算数运算: + - * / // % **
比较运算: 
    ==   
    != 
    > 
    < 
    >=
    <=

赋值运算:
=
+= 、 -=、 *=、 /=、 %= 、**=、 //=

逻辑运算:
and
or
not

成员运算:
in
not in

运算符的优先级

运算符 描述
** 指数 (最高优先级)
*,/,%,// 乘,除,取模和取整除
+,- 加法减法
<,>,<=,>= 比较运算符
==,!= 等于运算符
= ,%= ,/=, //=, -=, +=, *=, **= 赋值运算符
is , is not 身份运算符
in, not in 成员运算符
not, or , and 逻辑运算符

总结

last_li = ['1',['2','nvzhuangdalao', 'Thailander'],
              ['2','Java', 'Python', 'Ruby', 'PHP', ['3','zhaoliying','guanxiaotong','Hi python']],
              ['2','liuguoliang', 'luoshengmen', ('kezhendong','lamo'), ['3','luhan','wuyifan','wangbaoqiang']]
]
1.如何取出('kezhendong','lamo')
2.如何去除'zhaoliying'中的'zhao'?
3.如何删除'nvzhuangdalao'?

Python中数值类型和我们日常使用没有差别,是一样的。对于序列类型,大家记住每种序列类型的定义和通用操作,在后面使用多了之后自然就能加深理解。

第二次作业

 #1.用3种方法,往列表里面插值


#2.给定列表 li =['a','b','c','d'],要求用2种方法, 将列表'c'元素,替换成'jianeng'
# li=['a','b','jianeng','d']


#3.te = 'string test'  如何把te 中的'test' 替换成'OK'


#4.将字符串 s ='hello python !',转换成列表 li=['hello','python','!']

L.append(obj) 在列表末尾添加新的对象。
L.clear() 清空整个列表。
L.copy() 复制列表,和L[:]的复制方式一样属于浅复制。
L.count(obj) 统计某个元素在列表中出现的次数。
L.extend(obj) 用obj扩展原来的列表。 obj可以是列表和元组,如果是字典,则只会添加字典的key
L.index(obj) 从列表中找某个值第一个匹配项的索引位置。
L.insert(index,obj) 插入元素,可以指定位置。
L.pop(index) 出栈,可以指定位置。index默认是L[-1]
L.remove(obj) 移除指定元素从左边开始的第一个。
L.reverse() 反向列表中元素。
L.sort() 对原列表进行排序。列表中的元素要类型相同  (key = len int lambda)
    
内置函数:
sorted() 和 reversed()

s.count(x):返回字符串x在s中出现的次数,带可选参数
s.endswith(x):如果字符串s以x结尾,返回True
s.startswith(x):如果字符串s以x开头,返回True
s.find(x) :返回字符串中出现x的最左端字符的索引值,如果不在则返回-1
s.index(x):返回字符串中出现x的最左端的索引值,如果不在则抛出valueError异常
s.isalpha ()  :测试是否全是字母,都是字母则返回 True,否则返回 False.
s.isdigit () :测试是否全是数字,都是数字则返回 True 否则返回 False.
s.islower () :测试是否全是小写
s.isupper () :测试是否全是大写
s.lower () :将字符串转为小写
s.upper () :将字符串转为大写 
s.replace (x,y) :子串替换,在字符串s中出现字符串x的任意位置都用y进行替换
s.split():返回一系列用空格分割的字符串列表
s.split(a,b):a,b为可选参数,a是将要分割的字符串,b是说明最多要分割几个

int('12',16)这里有两个地方要注意:1)12要以字符串的形式进行输入,如果是带参数base的话
2)这里并不是将12转换为16进制的数,而是说12就是一个16进制的数,int()函数将其用十进制数表示,如下
complex(real,[,imag])
complex(1)  1+0j   complex(1,2)  1+2j complex('1')1+0j 
repr()是将一个对象可视化,使用字符串输出,使编程人员看到其的内部结构

相关文章

网友评论

      本文标题:Python序列类型的方法及类型转换

      本文链接:https://www.haomeiwen.com/subject/pieumxtx.html