python学习记录(六)

0903--https://www.cnblogs.com/fnng/archive/2013/04/21/3034442.html

基本语句的用法

 

使用逗号输出(想要同时输出文本和变量值,又不希望使用字符串格式化,使用逗号输出没毛病)

>>> print ('age:',25)
age: 25
>>> name = 'lulu'
>>> salutation = 'Miss'
>>> greeting = 'Hello.'
>>> print (greeting,salutation,name)
Hello. Miss lulu

模块导入函数

从模块导入函数的时候,可以使用

import somemodule

或者

form somemodule immport  somefunction

或者

from somemodule import somefunction.anotherfunction.yetanotherfunction

或者

from somemodule import *  

最后一个版本只有确定自己想要从给定的模块导入所有功能进。

如果两个模块都有open函数,可以像下面这样使用函数:

module.open(...)

module.open(...)

当然还有别的选择:可以在语句末尾增加一个as子句,在该子句后给出名字。

>>> import math as foobar   #为整个模块提供别名
>>> foobar.sqrt(4)
2.0
>>> from math import sqrt as foobar  #为函数提供别名
>>> foobar(4)
2.0

赋值语句

序列解包

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

可以获取或删除字典中任意的键-值对,可以使用popitem方

>>> scoundrel ={'name':'robin','girlfriend':'marion'}
>>> key,value = scoundrel.popitem()
>>> key
'name'
>>> value
'robin'

链式赋值

链式赋值是将同一个值赋给多个变量的捷径。

>>> x = y = 42
# 同下效果:
>>> y = 42
>>> x = y
>>> x
42

增理赋值

>>> x = 2
>>> x += 1  #(x=x+1)
>>> x *= 2  #(x=x*2)
>>> x
6

控制语句

if    else语句

name = input('What\'s your name?')
if name.endswith('lulu'):
  print('Hello!Miss liu~')
else:
  print('Hello!stranger~')

#输入

>>>What's your name?lulu

#输出

Hello!Miss liu~

 

elif子句(else if的简写)

注意:input()返回的数据类型是str类型,不能直接和整数进行比较,必须先把str转换成整型,使用int()方法

num = int(input('enter a number:'))

if num>0:
  print('the number is positive')
elif num<0:
  print('the number is negative')
else:
  print('the number is zero')

#输入

>>>enter a number:-1

#输出

the number is negative

 

if嵌套

name = input('What\'s your name?')
if name.endswith('liu'):
  if name.startswith('miss.'):
    print('hello.miss.liu')
  elif name.startswith('mr.'):
    print('hello.mr.liu')
  else:
    print('hello.liu')
else:
  print('hello.stranger')

#输入

>>>What's your name?miss.liu

#输出

hello.miss.liu

 

断言

如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert 语句可以在程序中设置检查点。

>>> age = 10
>>> assert 0 < age < 100
>>> age = -1
>>> assert 0 < age < 100 , 'the age must be realistic'

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    assert 0 < age < 100 , 'the age must be realistic'
AssertionError: the age must be realistic

 

循环语句


 

打印1到100的数(while)

x = 1
while x<= 100:
    print (x)
    x += 1

#输出
1
2
3
4
.
.
100

(while循环),用一循环保证用户名字的输入:

name = ''
while not name:
    name = input('please enter your name:')
print ('hello,lulu')
#输入
>>>please enter your name:lulu
#输出
hello,lulu

打印1到100的数(for 循环)

range()函数和for-in循环

函数原型:range(start, end, scan):

参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);

                  end:技术到end结束,但不包括end 。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

                  scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

for number in range(1,101):
    print (number)
#输出
1
2
3
.
.
100

for 语句循环字典的所有键:

d = {'x':1,'y':2,'z':3}
for key in d:
    print(key,'value',d[key])
#输出
>>>x value 1
y value 2
z value 3

break语句

break 用来结束循环,假设找100以内最大平方数,那么程序可以从100往下迭代到0,步长为-1

from math import sqrt
for n in range(99,0,-1):
    root = sqrt(n)
    if root == int(root):
        print(n)
        break
#输出
81

continue 语句

continue结束当前的迭代,“跳”到下一轮循环执行。

while True:
    s = input('enter something:')
    if s == 'quit':
        break;
    if len(s) < 3:
        continue
    print ('Input is of sufficient length')
#输入
enter something:qqqq
Input is of sufficient length
enter something:dfsdfsdfsfsdfsdfsdf
Input is of sufficient length
enter something:qq
enter something:quit

 

posted @ 2018-09-04 09:24  liu_lu  阅读(156)  评论(0编辑  收藏  举报