python 逗号作用 语句间_python逗号作用

1.print和import1.1 print 略1.2 import(1)impore somemodule (2)from somemodule import somefunction(3)from somemodule import somefunction,anotherfunction(4)from somemodule import*(5)import somemodule as somename #为整个模块提供别名(6)from somemodule import somefunction as somename #为某个模块的某个函数提供别名

2.赋值2.1 序列解包:将多个值的序列解开,然后放到变量的序列中。2.2 链式赋值:将同一个值赋给多个变量。2.3 增量赋值:eg:x+=1

3.语句块:条件为真时执行或执行多次的一组语句,由代码前放置空格缩进语句创建。

4.条件语句4.1 布尔变量 python中的假:False None 0 “” () [] {}4.2 if语句4.3 else语句4.4 elif语句4.5 嵌套代码块4.6 条件(1)比较运算符

(2)相等运算符 ==

(3)同一性运算符 is

(4)成员资格运算符 in

(5)字符串和序列比较

(6)布尔运算符

4.7 断言: assert 放入检查点,确保某个条件一定为真才能让程序正常工作

循环

5.1 while循环

5.2 for循环

5.3 循环遍历字典元素

5.4 迭代工具

(1) 并行迭代:同时迭代两个序列

(2)按索引迭代

(3)翻转和排序迭代

5.5 跳出循环

(1) break

(2) continue

(3) while True/break

5.6 循环中的else子句

6.列表推导式-轻量级循环

7.pass,del,exec

本章相关代码:

-- coding: utf-8 --

1.print 与import

1.1 print 使用逗号输出

print 'Age:',42 #Age: 42name='xiaming'age='42'print name,age #xiaming 42

2.赋值语句

2.1.序列解包:将多个值的序列解开,然后放到变量的序列中

x,y,z=1,2,3print x,y,z #1 2 3x,y=y,x #2 1 3print x,y,zv=1,2,3x,y,z=vprint x #1

2.2.链式赋值 将同一个值赋给多个变量的捷径

x=y=4print x,y

2.3.增量赋值

x=2x+=1x*=2a='bo'a+='x'a*=2print x,a #6,boxbox

3:语句块:缩排

4.条件与条件语句

4.1.布尔变量的作用

4.2 if elif else语句

name=raw_input('what is your name?')if name.endswith('ming'):

print 'hello,ming'

elif name.endswith('hong'):

print 'hello,hong'

else:

print 'hello'

4.3 嵌套语句

num=raw_input('input a number')if num>10:

if num<20:

print '10

else:

print 'num>=20'

else:

print 'num<=10'

#5.循环

5.1.while循环

x=1while x<=10: print x x+=1

name=''while not name:

name=raw_input('enter your name:')

print 'Hello,%s!'%name #Hello,ming!

5.2.for 循环

names=['ming','hong','qiang','qing']for n in names:

print n

print range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] rang(0,10)for num in range(1,10):

print num

5.3.循环遍历字典元素

d={'x':1,'y':2,'z':3}for key in d:

print key,d[key] #y 2 x 1 z 3

5.4.一些迭代工具

5.4.1 并行迭代

names=['ming','hong','qiang','qing']ages=[10,11,12,13]for i in range(len(names)):

print names[i],'is',ages[i],'years old' #ming is 10 years old...

print zip(names,ages) #[('ming', 10), ('hong', 11), ('qiang', 12), ('qing', 13)] 将两个序列压缩,返回一个元组的列表for n,a in zip(names,ages):

print n, 'is',a, 'years old' #ming is 10 years old...

5.4.2按索引迭代

5.4.3翻转和排序迭代

5.5.跳出循环

5.5.1break

from math import sqrtfor n in range(99,0,-1):

print n

root=sqrt(n)

if root==int(root):

print "the biggst is %s"%n

break

5.5.2 continue

5.5.3 while True/break

while True:

word=raw_input('Please enter a word:')

if not word:break

print 'The word was '+word

5.6 循环体中的else子句

from math import sqrtfor n in range(99,80,-1):

print n

root=sqrt(n)

if root==int(root):

print "the biggst is %s"%n

break

else:

print "didn't find it"

6 列表推导式-轻量级循环:利用其它列表创建新列表

print [x*x for x in range(10)] #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]print [x*x for x in range(10) if x%3==0] #[0, 9, 36, 81]print [(x,y,z) for x in range(2) for y in range(2) for z in range(2)]

[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

7 三个语句:pass,del,exec

7.1 pass :

7.2 del:删除不再使用的对象

7.3 exec:执行一系列Py语句 exel:计算Py表达式,并且返回结果值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值