Python语句

赋值语句

>>> (x,y) = (5,10)

>>> x

5

>>> y

10

>>> x,y = 5,10

>>> x,y

(5, 10)

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

>>> x,y,z

(1, 2, 3)

>>> x,y = y,x

>>> x,y

(2, 1)

>>> [a,b,c] = (1,2,3)

#序列赋值

>>> a,b,c = 'abc'

>>> a

'a'

>>> b

'b'

>>> c

'c'

#左右两边不一致

>>> a,b,*c = 'abcdefgh'

>>> a

'a'

>>> b

'b'

>>> c

['c', 'd', 'e', 'f', 'g', 'h']

>>> a,*b,c = 'abcdefgh'

>>> a

'a'

>>> b

['b', 'c', 'd', 'e', 'f', 'g']

>>> c

'h'

>>> s = 'abcderf'

>>> a,b,c = s[0],s[1],s[2:]

>>> a

'a'

>>> b

'b'

>>> c

'cderf

>>> a,b,c,*d = 'abc'

>>> a

'a'

>>> b

'b'

>>> c

'c'

>>> d

[]

>>> a = b = c = 'hello'

>>> a

'hello'

>>> b

'hello'

>>> a = 'hello'

>>> b = 'world'

>>> print(a,b)

hello world

>>> print(a,b,sep='|')

hello|world

>>> print(a,b,sep='|',end='...\n')

hello|world...

>>> print(a,b,end='...\n',file=open('result.txt','w'))

条件语句

score = 75

if score >= 90:

print('优秀')

elif score >= 80:

print('良好')

elif score >= 60:

print('及格')

else:

print('不及格')

def add(x):

print(x + 10)

operation = {

'add': add,

'update': lambda x: print(x * 2),

'delete': lambda x: print(x * 3)

}

operation.get('add')(10)

operation.get('update')(10)

operation.get('delete')(10)

#三元表达式

result = '及格' if score >= 60 else '不及格'

循环语句

while循环

x = 'hello world'

while x:

print(x,end='|')

x = x[1:]

#hello world|ello world|llo world|lo world|o world| world|world|orld|rld|ld|d|

#continue

x = 10

while x:

x -= 1

if(x % 2 != 0):

continue

print(x)

#break、else

while True:

name = input('请输入姓名:')

if name == 'stop':

break

age = input('请输入年龄:')

print('{}---{}'.format(name,age))

else:

print('循环结束')

#else

for循环

for x in [1,2,3,4]:

print(x, end='|')

for key in emp:

print(key)

for k,v in emp.items():

print(k,v)

s1 = 'abcd'

s2 = 'cdef'

result = []

for x in s1:

if x in s2:

result.append(x)

result = [x for x in s1 if x in s2]

for x in range(1,100):

print(x)

for x in range(1,101,2):

print(x)

for index,item in enumerate(s1):

print(index,item)

迭代

迭代协议:__ next__()

全局函数:next()

f = open('a.txt',encoding='utf8')

for line in f:

print(line)

可迭代的对象分为两类:

? 迭代器对象:已经实现(文件)

? 可迭代对象:需要iter()-->__ iter__方法生成迭代器(列表)

f = open('a.txt')

iter(f) is f

True

l = [1,2,3]

iter(l) is l

False

l = iter(l)

l.__next__()

1

l = [1,2,3]

res = [x + 10 for x in l ]

res

[11, 12, 13]

res = [x for x in l if x >= 2]

res

[2, 3]

result = zip(['x','y','z'],[1,2,3])

result

for x in result:

print(x)

('x', 1)

('y', 2)

('z', 3)

Python之函数

函数的作用域:

Local

Global

Built-in

Enclousure(nolocal)

x = 100

def func():

x = 0

print(x)

print('全局x:', x)

func()

全局x: 100

0

------------------------------------------

x = 100

def func():

global x

x = 0

print(x)

print('全局x:', x)

func()

print('全局x:', x)

全局x: 100

0

全局x: 0

def func():

x = 100

def nested():

x = 99

print(x)

nested()

print(x)

func()

99

100

--------------------------------------------------------------

def func():

x = 100

def nested():

nonlocal x

x = 99

print(x)

nested()

print(x)

func()

99

99

参数

不可变类型:传递副本给参数

可变类型:传递地址引用

def fun(x):

x += 10

x = 100

print(x)

fun(x)

print(x)

100

100

---------------------------

def func(l):

l[0] = 'aaa'

l = [1,2,3,4]

print(l)

func(l)

print(l)

[1, 2, 3, 4]

['aaa', 2, 3, 4]

----------------------------------------

def func(str):

str = 'aaa'

str = '124'

print(str)

func(str)

print(str)

124

124

-------------------------------------------

def func(l):

l[0] = 'aaa'

l = [1,2,3,4]

print(l)

func(l.copy())

print(l)

[1, 2, 3, 4]

[1, 2, 3, 4]

---------------------------------------------

def func(l):

l[0] = 'aaa'

l = [1,2,3,4]

print(l)

func(l[:])

print(l)

[1, 2, 3, 4]

[1, 2, 3, 4]

*args和**kwargs

def avg(score, *scores):

return score + sum(scores) / (len(scores) + 1)

print(avg(11.2,22.4,33))

scores = (11,22,3,44)

print(avg(*scores))

----------------------------------------------------------------

emp = {

'name':'Tome',

'age':20,

'job':'dev'

}

def display_employee(**employee):

print(employee)

display_employee(name='Tom',age=20)

display_employee(**emp)

lambda

f = lambda name:print(name)

f('Tom')

f2 = lambda x,y: x+y

print(f2(3,5))

f3 = lambda : print('haha')

f3()

------------------------------

def hello(action,name):

action(name)

hello(lambda name:print('hello',name),'Tom')

--------------------------------

def add_number(x):

return x+5

l = list((range(1,10)))

#map这种方式特别灵活,可以实现非诚复杂的逻辑

print(list(map(add_number,l)))

print(list(map(lambda x:x**2,l)))

def even_number(x):

return x % 2 == 0

l = list(range(1,10))

print(list(filter(even_number,l)))

print(list(filter(lambda x : x % 2 == 0,l)))

python赋值语句的作用_Python之语句与函数相关推荐

  1. python中赋值语句的作用_python中return可以使用赋值语句吗?

    在python中,有各种不同类型的语句.一个python程序是由模块构成的;一个模块由一条或多条语句组成;每个语句由不同的表达式组成;表达式可以创建和操作对象.下面来看看python中的语句. 赋值语 ...

  2. python中匿名函数的作用_Python 中的匿名函数,你会用吗

    原标题:Python 中的匿名函数,你会用吗 概念 我们从一个例子引入. 这里有一个元素为非空字符串的列表,按字符串最后一个字母将列表进行排序.如果原列表是 ['abc', 'g', 'def'],则 ...

  3. python感叹号的作用_Python的作用

    电脑上安装python这个软件的作用是什么,我不懂.pycharm是输入python语如果把C语言比作笔芯,那么python就像装了笔芯的笔,两者都可以用来写字,但后者写起来可能更顺畅.具体来说,py ...

  4. python修饰符作用_python函数修饰符@的使用

    python函数修饰符@的作用是为现有函数增加额外的功能,常用于插入日志.性能测试.事务处理等等. 创建函数修饰符的规则: (1)修饰符是一个函数 (2)修饰符取被修饰函数为参数 (3)修饰符返回一个 ...

  5. python多线程的作用_Python多线程中三个函数的强大功能简介

    在Python多线程中有三个比较简单的函数,在实际的相关操作中你对这一实际操作是否了解?是否对其感兴趣?如果你想对Python多线程的开发与多线程程序及相关实际操作方案有所了解的话,你就可以点击以下的 ...

  6. python主函数调用格式_Python的模块与函数

    一.概述Python的程序由包.模块和函数组成. 函数是一段可重用的有名称的代码.通过输入的参数值,返回需要的结果,并可存储在文件中供以后使用.几乎任何Python代码都可放在函数中.Python为函 ...

  7. python定义空函数体_Python 2.2 定义函数

    定义函数 Python中,定义函数是用def语句,一次写出函数名.括号.括号中的参数.和冒号:,然后在缩进模块中编写函数体,函数的返回值使用return语句返回. 我们以自定义一个求绝对值的my_ab ...

  8. python条件语句作用_Python 条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null)值为tr ...

  9. python break语句作用_Python break语句详解

    Python break语句,就像在C语言中,打破了最小封闭for或while循环.break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句. bre ...

最新文章

  1. 一键编译php,编译安装php 附加一键安装php5.6.30脚本
  2. 第14课:项目实战——深度优化你的神经网络模型
  3. 第70天:jQuery基本选择器(一)
  4. 3D Slicer源代码编译与调试
  5. Python3.0 新特性
  6. 大数据架构师学习方向---加油。
  7. UI基础设计规范,确定不了解一下?
  8. 人生真正拉开距离不是高考!是大学毕业后第一个十年
  9. paip.php 与js 的相似性以及为什么它们这么烂还很流行。。
  10. OO第二次博客——电梯系列总结
  11. 阿里巴巴:购擎天科技25%股份
  12. 夏令时到底是个什么东西?
  13. FlashFXP连接ftp服务器上传下载
  14. Excel插件快捷键弹窗事件(VSTO+键盘钩子实现)
  15. 贵州华芯通半导体驻北京研发中心开业
  16. 为什么8G运行内存的电脑,开几个WORD文档,运行内存就被占满了,WPS很占用内存吗
  17. 《实验细节》MELD文本预处理
  18. python控制modem的at指令_MODEM AT指令集
  19. mysql内部联结_MYSQL:内部联结、自然联结以及外部联结
  20. 电脑c语言培训班学费多少,专业c语言培训学费

热门文章

  1. 详解CorelDRAW中如何合并与拆分对象
  2. 汤姆大叔 深入理解JavaScript系列(20):《你真懂JavaScript吗?》答案详解 后六道题答案...
  3. LINUX的简单命令
  4. HTML DOM教程 47-JavaScript Date 对象
  5. php内核介绍及扩展开发指南 pdf vp进,PHP内核介绍及扩展开发指南—Extensions 的编写...
  6. godot python_我的godot开发环境调教记录分享
  7. OpenJudge NOI 1.4 20:求一元二次方程的根
  8. 信息学奥赛一本通(1021:打印字符)
  9. 书架(信息学奥赛一本通-T1228)
  10. 学术会议墙报_中国化学会第十四届全国电分析化学学术会议在南京顺利召开