python3.5入门笔记(6) 条件、循环语句(一)

基础

1文本编辑器

Sublime Text、Notepad++

2import使用  

(1)import math      从pythin标准库引入math.py模块

    r=5

   print('半径为5的圆的面积:%.2f' %(math.pi*r**2))    math.pi的值为3.1415926...

   半径为5的圆的面积  78.54

(2)import sys      从python标准库引入sys模块

>>> print('python的搜索路径为: %s' % sys.path)

python的搜索路径为: ['', 'D:\\python\\Lib\\idlelib', 'D:\\python\\python36.zip', 'D:\\python\\DLLs', 'D:\\python\\lib', 'D:\\python', 'D:\\python\\lib\\site-packages']

(3)from math import  *    从python标准库引入math模块中所有方法

    print( pi)

    3.141592653589793

3、序列解包

(1)x,y,z=1,2,3

>>> print(x,y,z)

      1 2 3

(2)x,y,z=1,2,3

        x,y=y,x

>>> print(x,y,z)

       2 1 3

条件语句

1、布尔变量

假(false): false none 0 “” () [] {}

True+1      2

False+1    1

2、if语句

a=7

if a>10:

    print('a大于10')

elif 2<=a<=10:

    print('a大于等于2,小于等于10')

else:

    print('a小于2')

>>>a大于等于2,小于等于10

3、断言

x=7

assert x<5,'x小于5'

assert x>5,'x大于5'

Traceback (most recent call last):

  File "C:/Users/Administrator/Desktop/11.py", line 2, in <module>

    assert x<5,'x小于5'

AssertionError: x小于5

循环语句

1、for循环

test=['a','b','c']

for x in test:

    print('当前字母:',x)

>>>当前字母: a

>>>当前字母: b

>>>当前字母: c

2、range并行迭代

test=['a','b','c']

mm=[1,2,3]

for x in range(len(mm)):        即取0,1,2

      print(test[x],'的字母顺序是:',mm[x])  

>>>a 的字母顺序是: 1

>>>b 的字母顺序是: 2

>>>c 的字母顺序是: 3

3、跳出循环

Break  停止循环

for x in 'hello':

    if x=='l':

        break

    print('当前字母',x)

>>>当前字母 h

>>>当前字母 e

Continue  跳过当前循环

a='hello'

for x in 'hello':

    if x=='l':

       continue

    print('当前字母',x)

>>>当前字母 h

>>>当前字母 e

>>>当前字母 o

4、pass 不做任何事情,占位语句

发布了28 篇原创文章 · 获赞 1 · 访问量 3191

猜你喜欢

转载自blog.csdn.net/pdd51testing/article/details/83785837