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

python学习记录(六)的更多相关文章

  1. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  2. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  3. python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍

    目录 python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍. 二丶列表,其它语言称为数组 1.列表的定义,以及语法 2.列表的使用,以及常用方法. 3.列表的常用操作 ...

  4. Python学习第六课

    Python学习第六课 课前回顾 列表 创建 通过 [] :写在[]里,元素之间用逗号隔开 对应操作: 查 增 append insert 改(重新赋值) 删除(remove del pop(删除后会 ...

  5. Python学习记录day8

    目录 Python学习记录day8 1. 静态方法 2. 类方法 3. 属性方法 4. 类的特殊成员方法 4.1 __doc__表示类的描述信息 4.2 __module__ 和 __class__ ...

  6. Python学习记录day7

    目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...

  7. Python学习记录:括号配对检测问题

    Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...

  8. 实验楼Python学习记录_挑战字符串操作

    自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...

  9. Python学习笔记六

    Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...

  10. 我的Python学习记录

    Python日期时间处理:time模块.datetime模块 Python提供了两个标准日期时间处理模块:--time.datetime模块. 那么,这两个模块的功能有什么相同和共同之处呢? 一般来说 ...

随机推荐

  1. 初学者的API测试技巧

    API(应用程序编程接口)测试是一种直接在API级别执行验证的软件测试.它是集成测试的一部分,它确认API是否满足测试人员对功能.可靠性.性能和安全性的期望.与UI测试不同,API测试是在没有GUI层 ...

  2. how to render html tag

    使用autoescaping If autoescaping is turned on in the environment, all output will automatically be esc ...

  3. .NET Core学习笔记(3)——async/await中的Exception处理

    在写了很多年.NET程序之后,年长的猿类在面对异步编程时,仍不时会犯下致命错误,乃至被拖出去杀了祭天.本篇就async/await中的Exception处理进行讨论,为种族的繁衍生息做出贡献……处理a ...

  4. vuex使用面板报 Unexpected token错误

    Module build failed: SyntaxError: D:/frontend/webtest/src/components/mutations.vue: Unexpected token ...

  5. [思维导图] C标准库

  6. Spring学习记录6——ThreadLocal简介

    Spring通过各种模板类降低了开发者使用各种数据持久化技术的难度.这些模板类是线程安全的,所以 多个DAO可以复用同一个模板实例而不会发生冲突.在使用模板类访问底层数据时,模板类需要绑定数据连接或者 ...

  7. Python工具类(一)—— 操作Mysql数据库

    如何调用直接看__main__函数里如何调用此工具类就阔以啦! # encoding=utf-8 import pymysql # 导入所有Mysql配置常量,请自行指定文件 from conf.se ...

  8. Mysql-SQL优化-子查询替代LEFT JOIN

    表A:批次信息表, 表B:实际批次明细表, Mysql版本:5.6.36 两表之间的数据体量差异:表B是表A的10000倍. 经过结转,表B通常保留 1千5百万数据.表A就是1千多条数据. 计算近24 ...

  9. Python Global和Nonlocal的用法

    nonlocal 和 global 也很容易混淆.简单记录下自己的理解. 解释 global 总之一句话,作用域是全局的,就是会修改这个变量对应地址的值. global 语句是一个声明,它适用于整个当 ...

  10. python 封装底层实现原理

    事实上,python封装特性的实现纯属"投机取巧",之所以类对象无法直接调用私有方法和属性,是因为底层实现时,python偷偷改变了它们的名称. python在底层实现时,将它们的 ...