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
python学习记录(六)的更多相关文章
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
- python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍
目录 python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍. 二丶列表,其它语言称为数组 1.列表的定义,以及语法 2.列表的使用,以及常用方法. 3.列表的常用操作 ...
- Python学习第六课
Python学习第六课 课前回顾 列表 创建 通过 [] :写在[]里,元素之间用逗号隔开 对应操作: 查 增 append insert 改(重新赋值) 删除(remove del pop(删除后会 ...
- Python学习记录day8
目录 Python学习记录day8 1. 静态方法 2. 类方法 3. 属性方法 4. 类的特殊成员方法 4.1 __doc__表示类的描述信息 4.2 __module__ 和 __class__ ...
- Python学习记录day7
目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...
- Python学习记录:括号配对检测问题
Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...
- 实验楼Python学习记录_挑战字符串操作
自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...
- Python学习笔记六
Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...
- 我的Python学习记录
Python日期时间处理:time模块.datetime模块 Python提供了两个标准日期时间处理模块:--time.datetime模块. 那么,这两个模块的功能有什么相同和共同之处呢? 一般来说 ...
随机推荐
- spring cloud微服务快速教程之(三)声明式访问Feign、负载均衡Ribbon
0-前言 eureka实际上已经集成了负载均衡调度框架Ribbon: 我们有了各个微服务了,那怎么来调用他们呢,一种方法是可以使用 RestTemplate(如:String str= restTem ...
- 1209. Construct the Rectangle
1209. Construct the Rectangle class Solution { public: /** * @param area: web page’s area * @retur ...
- Flink系列之Time和WaterMark
当数据进入Flink的时候,数据需要带入相应的时间,根据相应的时间进行处理. 让咱们想象一个场景,有一个队列,分别带着指定的时间,那么处理的时候,需要根据相应的时间进行处理,比如:统计最近五分钟的访问 ...
- Theia APIs——通过JSON-RPC进行通信
上一篇:Theia APIs——事件 通过JSON-PRC进行通信 在本节中,我将讲解如何创建后端服务并通过JSON-PRC来连接它. 我将使用debug logging system作为例子来进行讲 ...
- Vector人工智能机器人SDK使用笔记
Cozmo是2016年推出的,2两年后的2018年Vector上市,具备语音助手和更多功能,元件数由300+升级到700+. Vector的SDK具体说明在:developer.anki.com/ve ...
- 深入理解协程(二):yield from实现异步协程
原创不易,转载请联系作者 深入理解协程分为三部分进行讲解: 协程的引入 yield from实现异步协程 async/await实现异步协程 本篇为深入理解协程系列文章的第二篇. yield from ...
- Alodi:环境创建从未如此简单
一个满足你各种想象的快速方便生成临时环境的系统 在『Alodi:为了保密我开发了一个系统』文章中有讲到我们开发了一个系统用来快速生成临时测试环境,短短三个月已有数百个环境被创建,简化了工作,节省了时间 ...
- 用Python爬取了考研吧1000条帖子,原来他们都在讨论这些!
写在前面 考研在即,想多了解考研er的想法,就是去找学长学姐或者去网上搜索,贴吧就是一个好地方.而借助强大的工具可以快速从网络鱼龙混杂的信息中得到有价值的信息.虽然网上有很多爬取百度贴吧的教程和例子, ...
- 通过自己实现接口来加深理解SpringMVC的执行流程
功能介绍 上篇文章[从源码角度了解SpringMVC的执行流程]通过接口源码向大家介绍了SpringMVC的执行流程,主要偏重于源码.这篇文件我们来自己实现那几个关键接口,来真实体验下SpringMV ...
- 通过例子进阶学习C++(六)你真的能写出约瑟夫环么
本文是通过例子学习C++的第六篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然后从出局的下一个人重新 ...