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. 【接口测试】使用httpClient获取cookies+携带获取的cookies访问get接口

    数据准备 在本机或者远端机器安装部署moco-runner(参考:https://blog.csdn.net/qq_32706349/article/details/80472445) 这里我们只需要 ...

  2. 16.用pycharm导入自己写的模块时,import无法识别的解决办法

    我们用pycharm打开自己写的代码,当多个文件之间有相互依赖的关系的时候,import无法识别自己写的文件,但是我们写的文件又确实在同一个文件夹中, 这种问题可以用下面的方法解决: 1)打开File ...

  3. MariaDB的备份与主从、高可用实践

    1.编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份. [root@test-centos7-node1 scripts]# cat chose_backup_mysq ...

  4. python的list()函数

    list()函数将其它序列转换为 列表 (就是js的数组). 该函数不会改变   其它序列 效果图一: 代码一: # 定义一个元组序列 tuple_one = (123,','abc') print( ...

  5. java main 方法

    public static void main(String[] args) { BigDecimal b1 = new BigDecimal(0.01000000); BigDecimal b2 = ...

  6. 51Nod 2026 Gcd and Lcm

    题目传送门 分析: 开始玩一个小小的trick 我们发现\(f(n)=\sum_{d|n}\mu(d)\cdot d\)是一个积性函数 所以: \(~~~~f(n)=\prod f(p_i^{a_i} ...

  7. 史上最详细的二叉树、B树,看不懂怨我

    今天我们要说的红黑树就是就是一棵非严格均衡的二叉树,均衡二叉树又是在二叉搜索树的基础上增加了自动维持平衡的性质,插入.搜索.删除的效率都比较高.红黑树也是实现 TreeMap 存储结构的基石. 1.二 ...

  8. libc.so.6修改链接指向后导致系统无法使用的原因及解决方法

    https://www.cnblogs.com/weijing24/p/5890031.html http://man.linuxde.net/ldconfig

  9. java线程基础梳理

    java线程 概述 进程:运行时概念,运行的应用程序,进程间不能共享内存 线程:应用程序内并发执行的代码段,可以共享堆内存和方法区内存,而栈内存是独立的. 并发理解:在单核机器上,从微观角度来看,一段 ...

  10. php--->注册模式

    注册模式 什么是注册树模式? 注册树模式当然也叫注册模式,注册器模式.注册树模式通过将对象实例注册到一棵全局的对象树上,需要的时候从对象树上采摘的模式设计方法. 优点:单例模式解决的是如何在整个项目中 ...