Python中 运算符,返回值,函数,元祖

运算符

运算符Python 表达式结果描述支持的数据类型
+[1, 2] + [3, 4][1, 2, 3, 4]合并字符串、列表、元组
*‘Hi!’ * 4[‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]复制字符串、列表、元组
in3 in (1, 2, 3)True元素是否存在字符串、列表、元组、字典
not in4 not in (1, 2, 3)True元素是否不存在字符串、列表、元组、字典

+号

"hello " + “itcast”
‘hello itcast’

[1, 2] + [3, 4]
[1, 2, 3, 4]

(‘a’, ‘b’) + (‘c’, ‘d’)
(‘a’, ‘b’, ‘c’, ‘d’)

*号

‘ab’*4
‘ababab’

[1, 2]*4
[1, 2, 1, 2, 1, 2, 1, 2]

(‘a’, ‘b’)*4
(‘a’, ‘b’, ‘a’, ‘b’, ‘a’, ‘b’, ‘a’, ‘b’)

in

‘itc’ in ‘hello itcast’
True

3 in [1, 2]
False

4 in (1, 2, 3, 4)
True

“name” in {“name”:“Delron”, “age”:24}
True

注意,in在对字典操作时,判断的是字典的键

python内置函数

Python包含了以下内置函数

序号方法描述
1cmp(item1, item2)比较两个值
2len(item)计算容器中元素个数
3max(item)返回容器中元素最大值
4min(item)返回容器中元素最小值
5del(item)删除变量

cmp

cmp(“hello”, “itcast”)
-1

cmp(“itcast”, “hello”)
1

cmp(“itcast”, “itcast”)
0

cmp([1, 2], [3, 4])
-1

cmp([1, 2], [1, 1])
1

cmp([1, 2], [1, 2, 3])
-1

cmp({“a”:1}, {“b”:1})
-1

cmp({“a”:2}, {“a”:1})
1

cmp({“a”:2}, {“a”:2, “b”:1})
-1

注意:cmp在比较字典数据时,先比较键,再比较值。

len

len(“hello itcast”)
12

len([1, 2, 3, 4])
4

len((3,4))
2

len({“a”:1, “b”:2})
2

注意:len在操作字典数据时,返回的是键值对个数。

max

max(“hello itcast”)
‘t’

max([1,4,522,3,4])
522

max({“a”:1, “b”:2})
‘b’

max({“a”:10, “b”:2})
‘b’

max({“c”:10, “b”:2})
‘c’

del
del有两种用法,一种是del加空格,另一种是del()

a = 1
a
1

del a
a
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘a’ is not defined

a = [‘a’, ‘b’]
del a[0]
a
[‘b’]

del(a)
a
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘a’ is not defined

多维列表/元祖访问的示例

tuple1 = [(2,3),(4,5)]
tuple1[0]
(2, 3)

tuple1[0][0]
2

tuple1[0][2]
Traceback (most recent call last):
File “”, line 1, in
IndexError: tuple index out of range

tuple1[0][1]
3

tuple1[2][2]
Traceback (most recent call last):
File “”, line 1, in
IndexError: list index out of range

tuple2 = tuple1+[(3)]
tuple2
[(2, 3), (4, 5), 3]

tuple2[2]
3

tuple2[2][0]
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘int’ object is not subscriptable

Python基础语言

我们可以用id()来判断两个变量是否为同一个值的引用。 我们可以将id值理解为那块内存的地址标示。

a = 1
b = a
id(a)
13033816

id(b) # 注意两个变量的id值相同
13033816

a = 2
id(a) # 注意a的id值已经变了
13033792

id(b) # b的id值依旧
13033816

a = [1, 2]
b = a
id(a)
139935018544808

id(b)
139935018544808

a.append(3)
a
[1, 2, 3]

id(a)
139935018544808

id(b) # 注意a与b始终指向同一个地址
139935018544808
在这里插入图片描述

可变类型与不可变类型
可变类型,值可以改变:
列表 list
字典 dict

不可变类型,值不可以改变:
数值类型 int, long, bool, float
字符串 str
元组 tuple

元组

Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。

aTuple = (‘et’,77,99.9)
aTuple
(‘et’,77,99.9)

<1>访问元组
在这里插入图片描述
<2>修改元组
在这里插入图片描述
说明: python中不允许修改元组的数据,包括不能删除其中的元素。

<3>元组的内置函数count, index
index和count与字符串和列表中的用法相同

a = (‘a’, ‘b’, ‘c’, ‘a’, ‘b’)
a.index(‘a’, 1, 3) # 注意是左闭右开区间
Traceback (most recent call last):
File “”, line 1, in
ValueError: tuple.index(x): x not in tuple

a.index(‘a’, 1, 4)
3

a.count(‘b’)
2

a.count(‘d’)
0

函数返回值

在python中我们可不可以返回多个值?

>>> def divid(a, b):
...     shang = a//b
...     yushu = a%b 
...     return shang, yushu
...
>>> sh, yu = divid(5, 2)
>>> sh
5
>>> yu
1

本质是利用了元组

函数参数

1.缺省参数
调用函数时,缺省参数的值如果没有传入,则被认为是默认值。下例会打印默认的age,如果age没有被传入:

def printinfo( name, age = 35 ):
   # 打印任何传入的字符串
   print "Name: ", name
   print "Age ", age

# 调用printinfo函数
printinfo(name="miki" )
printinfo( age=9,name="miki" )


#以上实例输出结果:
#Name:  miki
#Age  35
#Name:  miki
#Age  9

注意:带有默认值的参数一定要位于参数列表的最后面。

def printinfo(name, age=35, sex):
… print name

File “”, line 1
SyntaxError: non-default argument follows default argument

2.不定长参数
有时可能需要一个函数能处理比当初声明时更多的参数。这些参数叫做不定长参数,声明时不会命名。基本语法如下:

    def functionname([formal_args,] *args, **kwargs):
       "函数_文档字符串"
       function_suite
       return [expression]

加了星号(*)的变量args会存放所有未命名的变量参数,args为元组;而加**的变量kwargs会存放命名参数,即形如key=value的参数, kwargs为字典。

def fun(a, b, *args, **kwargs):
… “”“可变参数演示示例”""
… print “a =”, a
… print “b =”, b
… print “args =”, args
… print "kwargs: "
… for key, value in kwargs.items():
… print key, “=”, value

fun(1, 2, 3, 4, 5, m=6, n=7, p=8) # 注意传递的参数对应
a = 1
b = 2
args = (3, 4, 5)
kwargs:
p = 8
m = 6
n = 7

c = (3, 4, 5)
d = {“m”:6, “n”:7, “p”:8}
fun(1, 2, *c, **d) # 注意元组与字典的传参方式
a = 1
b = 2
args = (3, 4, 5)
kwargs:
p = 8
m = 6
n = 7

fun(1, 2, c, d) # 注意不加星号与上面的区别
a = 1
b = 2
args = ((3, 4, 5), {‘p’: 8, ‘m’: 6, ‘n’: 7})
kwargs:

3.引用传参
可变类型与不可变类型的变量分别作为函数参数时,会有什么不同吗?
Python有没有类似C语言中的指针传参呢?

def selfAdd(a):
… “”“自增”""
… a += a

a_int = 1
a_int
1

selfAdd(a_int)
a_int
1

a_list = [1, 2]
a_list
[1, 2]

selfAdd(a_list)
a_list
[1, 2, 1, 2]

Python中函数参数是引用传递(注意不是值传递)。对于不可变类型,因变量不能修改,所以运算不会影响到变量自身;而对于可变类型来说,函数体中的运算有可能会更改传入的参数变量。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值