Python学习笔记之条件、循环和其他语句

时间:2022-09-18 01:39:27

一、函数导入

1、为模块提供别名

>>> import math as foobar #设置math 别名为foobar

>>> foobar.sqrt(4)

显示:2.0

2、为函数提供别名

>>> from math import sqrt as foobar

>>> foobar(4)

显示:2.0

二、赋值

1、多值赋值

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

>>> print(x,y,z)

显示:1,2,3

2、多值交换

>>> x,y=y,x

>>> print(x,y,z)

显示:2,1,3

3、增量赋值

>>> x=2

>>> x+=1

>>> x*=2

>>> x

显示:6

三、条件执行和if语句

1、if嵌套代码

>>> name=input('What is your name? ')

>>> if name.endswith('Gumby'):

>>>  if name.startswith('Mr.'):

>>>    print('Hello, Mr. Gumby')

>>>  elif name.startswith('Mrs.')

>>>    print('Hello, Mrs. Gumby')

>>>  else:

>>>    print('Hello, Gumby')

>>> else:

>>>  print('Hello,stranger')

2、is,同一性运算符,和==相似,但事实上不一样

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

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

>>> x==y

显示:True

>>> x==z

显示:True

>>> x is y

显示:True

>>> x is z

显示:False

3、in,成员资格运算符

>>> if 's' in name:

>>>  print('Your name contains the letter "s".')

>>> else:

>>>  print('Your name does not contain the letter "s".')

4、assert,断言

>>>age=-1

>>> assert 0<age<100, 'The age must be realistic'

显示:AssertionError:The age must be realistic

5、while循环

>>> x=1

>>> while x<= 100:

>>>  print(x)

>>>  x+=1

6、for循环

>>> numbers=[0,1,2,3,4,5,6,7]

>>> for number in numbers:

>>>  print(number)

7、循环遍历字典元素

>>> d={'x':1,'y':2,'z':3}

>>> for key in d

>>>  print(key,'corresponds to',d[key])

显示: z corresponds to 1

>>> for key,value in d.items():

>>>  print(key,'corresponds to',value)

8、并行迭代

>>> names=['anne','beth','george','damon']

>>> ages=[12,45,32,102]

>>> for i in range(len(names)):

>>>  print(names[i],'is',ages[i],'years old')

采用内建zip来实现上述功能

>>> zip(names,ages)

>>> for name,age in zip(names,ages)

>>>  print(name,'is',age,'years old')

9、翻转和排序迭代

>>> sorted([4,3,6,8,3]) #排序

显示:[3,3,4,6,8]

>>> sorted('Hello,world!')

显示:[' ','!',',','H','d','e','l','l','l','o','o','r','w']

>>> list(reversed('Hello,world!'))

显示:['!','d','l','r','o','w',' ',',','o','l','l','e','H']

>>> ' '.joing(reversed('Hello, world!'))

显示:'!dlrow,olleH'

10、跳出循环

>>> from math import sqrt

>>> for n in range(99,0,-1):

>>>  root=sqrt(n)

>>>  if root==int(root):

>>>    print(n)

>>>    break

显示:81

11、列表推导式,是利用其他列表创建新列表

>>> [x*x for x in range(10)]

显示:[0,1,4,9,16,25,36,49,64,91]

获取能被3整除的

>>> [x*x for x in range(10) if x % 3 ==0]

显示:[0,9,36,91]

更优推导式

>>> girls=['alice','bernice','clarice']

>>> boys=['charis','arnold','bob']

>>> letterGirls={}

>>> for girl in girls:

>>>  letterGrils.setdefault(girl[0],[]).append(girl)

>>> print([b+'+'+g for b in boys for g in letterGirls[b[0]]])

显示:['charis+clarice','arnold+alice','bob+bernice']

12、占位

>>> if name=='Ralph Auldus Melish':

>>>  print('Welcome!')

>>> elif name=='Enid':

>>>  pass #占位

>>> elif name=='Bill Gates':

>>>   print('Access Denied')

13、exec执行

>>> from math import sqrt

>>> scope={}

>>> exec('sqrt=1',scope)

>>> sqrt(4)

显示:2

>>> scope['sqrt']

显示:1

14、eval用于求值

>>> eval(input('Enter an arithmetic expression: '))

Enter an arithmetic expression: 6+18*2

显示:42