首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

编程学习篇——Python(一)

文章说明

这里只是对本人每日学习的一个总结,并非系统的python学习平台。

今日要点

map()函数

reduce()函数

filter()函数

字符串与列表的转换

Python Day1 语法基础

1

类型判断

type()方法和isinstance()方法

代码演示

>>> x=3

>>> print(type(x))

>>> x='hello world'

>>> print(type(x))

>>> x=[1,2,3]

>>> print(type(x))

>>> isinstance(3,int)

True

>>> isinstance(x,tuple)

False

2

元组不可变

字符串和元组是不可变的,不可用下标索引改变其值

代码演示

>>> x=(1,2,3)

>>> print(x)

(1, 2, 3)

>>> x[0]=2

Traceback (most recent call last):

File "

", line 1, in

x[0]=2

TypeError: 'tuple' object does not support item assignment

3

变量存储方式

Python采用的是基于内存的管理方式,如果不同变量赋予相同的值,这个值在内存中只有一份,多个变量指向同一个内存地址

4

字符串合并

代码演示

>>> a='abc'+'123'

>>> a

'abc123'

>>> x='1234''abcd'

>>> x

'1234abcd'

>>> x=x+'5678'

>>> x

'1234abcd5678'

>>> x=x'efgh'

SyntaxError: invalid syntax

5

关系运算

代码演示

>>> 1

True

>>> 'hello'>'world'

False

>>> [1,2,3]

True

>>> [1,2,3]

True

>>>

True

6

成员测试运算符

用运算符in测试,测试一个对象是否时另一个对象的元素

代码演示

>>> 3 in [1,2,3]

True

>>> 'idef' in 'wsdidefwsd'//子字符串测试

True

>>> 'abc' in 'wadbrc'//子字符串测试

False

>>> for i in (3,5,7):

print(i,end='\n')

3

5

7

>>>

7

对象测试运算符

用is关键字来测试两个对象是否是同一个,如果是返回true。不是返回false。如果两个对象是同一个,二者具有相同的内存地址。

代码演示

>>> x=4

>>> x is x

True

>>> 'hello' is 'hello'

True

>>> x=[23,45,12]

>>> x[0] is x[1]//两个值不相同,则返回false

False

>>> x=[23,23,45,45,12]

>>> x[2] is x[3]//两个值相同,则返回true

True

>>> y=[23,23,45,45,12]

>>>x is y//这里创建的是两个列表对象,虽然值 一样但也不是一个对象

False

>>>x[1] is y[1]//两个值相同,则返回true

True

8

集合运算(并交叉)

集合的运算可以通过位运算符来实现,而差集则使用减号运算符来实现(注意,并集运算不是加号)

代码演示

>>> x=

>>> y=

>>> x|y //并集,自动去除重复元素

>>> x&y //交集

>>> x^y //对称差集

>>> x-y //差集(项在x中,但不在y中)

9

自增,自减

python不支持++和--操作。但是实际上有别的意思。

代码演示

>>> i=3

>>> ++i //正正得正

3

>>> +(+3)

3

>>> i++

SyntaxError: invalid syntax

>>> --i //负负得正

3

>>>

10

任意同类型相加

单个任何类型的对象或常量属于合法表达式,使用运算符连接的变量和常量以及函数调用的任意组合也属于合法的表达式。

代码演示

>>> a=[1,2,3]

>>> b=[4,5,6]

>>> c=a+b

>>> c

[1, 2, 3, 4, 5, 6]

>>>d=list(map(str,c))

>>> d

['1', '2', '3', '4', '5', '6']

>>>('hello,'*5).rstrip(',')+'!'

'hello,hello,hello,hello,hello!'

rstrip()函数的功能是返回删除 string 字符串末尾的指定字符后生成的新字符串。

>>>str = "88888888this is string example....wow!!!8888";

>>>print str.rstrip('8');

88888888this is string example....wow!!!

11

常用内置函数

此处列举了常用的内置函数

代码演示

>>>float(3)

3.0

>>>help(map)

Help on class map in module builtins:

class map(object)

| map(func, *iterables) --> map object

|

| Make an iterator that computes the function using arguments from

| each of the iterables. Stops when the shortest iterable is exhausted.

|

| Methods defined here:

|

| __getattribute__(self, name, /)

| Return getattr(self, name).

|

| __iter__(self, /)

| Implement iter(self).

|

| __new__(*args, **kwargs) from builtins.type

| Create and return a new object. See help(type) for accurate signature.

|

| __next__(self, /)

| Implement next(self).

|

| __reduce__(...)

| Return state information for pickling.

>>> string=input('please input something')

please input something hello

>>> string

' hello'

>>>len(string)

7

>>>list(string) //将字符串转换为列表

[' ', ' ', 'h', 'e', 'l', 'l', 'o']

>>>tuple(string) //将字符串转换为元组

(' ', ' ', 'h', 'e', 'l', 'l', 'o')

>>>list1=range(1,100,2) //返回range对象

>>> for i in list1:

print(i,end=' ')

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

>>>str(1234)

'1234'

>>>sum(list1)

2500

>>>sum([[1],[2],[3]],[])

[1, 2, 3]

>>> x=[34,76,12,0,344,122]

>>>sorted(x) //排序

[0, 12, 34, 76, 122, 344]

>>>list(reversed(x)) //倒序输出

[122, 344, 0, 12, 76, 34]

>>>list2 =sorted(x) //将排序结果赋给list2

>>> list2

[0, 12, 34, 76, 122, 344]

range()函数返回的列表包含左闭右开内的step步长的整数。

12

map()函数

map()函数把一个函数func依次映射到序列或迭代器对象的每个元素上,并返回一个可迭代的map对象作为结果,map中的每个元素都是经过函数func处理后的结果。

代码演示

>>> list(map(str,range(5)))

['0', '1', '2', '3', '4']

>>> def add5(v):

return v+5

>>> list(map(add5,range(7)))

[5, 6, 7, 8, 9, 10, 11]

>>> def add(x,y): //接收两个参数的函数

return x+y

>>> list(map(add,range(5),range(5,10,2)))

[5, 8, 11]

13

reduce()函数

reduce() 函数会对参数序列中元素进行累积。

函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

operator库里这个模块提供了一系列的函数操作

紫色部分为字符串和列表的转换

13

filter()函数

filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。

该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

代码演示

>>>def is_odd(n):

return n % 2 == 1

>>>tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

>>>newlist = list(tmplist)print(newlist)

[1, 3, 5, 7, 9]

>>>import math

>>>def is_sqr(x):

return math.sqrt(x) % 1 == 0

>>>tmplist = filter(is_sqr, range(1, 101))

>>>newlist = list(tmplist)

>>>print(newlist)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181028G1JQPT00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com