本章相关代码:
# -*- coding: utf-8 -*-
#1.print 与import
#1.1 print 使用逗号输出
print 'Age:',42 #Age: 42
name='xiaming'
age='42'
print name,age #xiaming 42
#2.赋值语句
#2.1.序列解包:将多个值的序列解开,然后放到变量的序列中
x,y,z=1,2,3
print x,y,z #1 2 3
x,y=y,x #2 1 3
print x,y,z
v=1,2,3
x,y,z=v
print x #1
#2.2.链式赋值 将同一个值赋给多个变量的捷径
x=y=4
print x,y
#2.3.增量赋值
x=2
x+=1
x*=2
a='bo'
a+='x'
a*=2
print 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=1
while 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 sqrt
for 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 sqrt
for 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表达式,并且返回结果值
相关知识
python语法认为条件x
python学习总结day2
设三个变量x=1,y=2,z=3,则表达式y+=z
11.1.1 JavaScript基本语法
[教学]命令条件语语法 178
黑马程序员:Java基础语法(二)
听Python之父
Python学习手册
javascript基础语法
六、Python 基础语句
网址: python语法认为条件x https://m.mcbbbk.com/newsview757629.html
上一篇: 四川宠物用品外观结构工业设计公司 |
下一篇: 有了这个摄像头,上班也可以跟家里 |