逻辑操作符

身份操作符 is

a = ['AAA', 3, None]
b = ['AAA', 3, None]
a is b #False
b = a
a is b #True

身份比较速度快,原因是对直接对内存地址进行比较,所以内容相同的俩个变量结果却是false。

a = None
a is None #True

比较操作符

这里只要注意一点

0 <= a <= 10

这种结链比较是可以的,夸张一点

a < b < c < e < d

逻辑操作符

逻辑操作符:and or not

其中 and or 都使用short-circuit逻辑实现

5 and 2 #2
2 and 5 #5
5 and 2 and 3 #3
5 or 2 #5
2 or 5 #2
2 or 3 or 5 #2
2 or 3 and 5 #2
2 and 3 or 5 #3
2 or 3 and 4 and 5 and 6 and 7 #2
2 or 3 and 4 and 0 #2
0 or 3 and 4 and 5 #5
True and 1 # 1
1 and True # True

还可以得出结论, and 的优先级大于 or

算术操作符

a = 1
a += 8
a = a + 8

这里需要记住的是,python中,int数据类型是固定的,一旦赋值就不能改变。因此,对固定的对象使用增强的赋值操作符时,实际上是创建一个对象来存储结果,之后,目标对象重新绑定。所以上面的:a+=8语句执行的时候,python 会计算a+8,将所得的结果9存储到新的int对象,之后将a重新绑定为引用这个新的int对象(如果a原来引用的原始对象没有其他对象引用,就会进入垃圾收集流程)。

第二 a+=8 与 a = a + 8 并不一致,这个没怎么明白。

特殊用法:

name = 'xxx'
name + 'ccc' #xxxccc
name += 'ccc' #xxx ccc

与int一样 字符串也是固定的,所以流程和上述相同,

不过列表在python中是可变的

a = ['a', 'b', 'c']
a += 'ddd' #['a', 'b', 'c', 'd', 'd', 'd']
a += ['ddd'] #['a', 'b', 'c', 'd', 'd', 'd', 'ddd']
a += 5 #报错

列表 += 右边的操作数必须是 iterable 对象

题目

1.生成数字

import random

digits = [7, 1, 9, 4, 2, 8, 3, 0, 6]
Digits = [[random.randint(1, 1000) for j in range(7)] for i in range(10)] try:
row = 0
while row < 7:
line = ''
column = 0
while column < len(digits):
number = int(digits[column])
digit = Digits[number]
line += str(digit[row]) + ' '
column += 1
print(line)
row += 1
except IndexError:
print('usage: <number>')
except ValueError as err:
print(err, 'in', digits)

2.循环输入数字输出长度,和,最大最小值,平均值

try:
digits = []
while True:
a = input('enter a number or Enter to finish: ')
if not a and a != 0:
break
else:
digits.append(int(a))
print('numbers: ', digits)
print('count = ', len(digits), ' sum = ', sum(digits),
' lowest = ', min(digits), ' highest = ', max(digits),
'mean = ', sum(digits) / len(digits))
except ValueError as err:
print(err, 'in', a)
  1. 超级超级巨大巨大罪过的诗
import random

article = ['a', 'the', 'this', 'that']
theme = ['cat', 'fish', 'dog', 'pig', 'car', 'house', 'tree']
verb = ['flied', 'walked', 'runned', 'smiled', 'jumped']
adv = ['loudly', 'quietly', 'well', 'badly'] try:
count = 0
while count < 5:
line = article[random.randint(0, len(article) - 1)] + ' ' +\
theme[random.randint(0, len(theme) - 1)] + ' ' +\
verb[random.randint(0, len(verb) - 1)] + ' ' +\
adv[random.randint(0, len(adv) - 1)]
print(line)
count += 1
except IndexError:
print('error index')

4.用户指定行数

import random

article = ['a', 'the', 'this', 'that']
theme = ['cat', 'fish', 'dog', 'pig', 'car', 'house', 'tree']
verb = ['flied', 'walked', 'runned', 'smiled', 'jumped']
adv = ['loudly', 'quietly', 'well', 'badly'] try:
count = 0
uplimit = int(input('the rows you need between 1 and 10: '))
if not uplimit or not 1<= uplimit <= 10:
uplimit = 5
while count < uplimit:
line = article[random.randint(0, len(article) - 1)] + ' ' +\
theme[random.randint(0, len(theme) - 1)] + ' ' +\
verb[random.randint(0, len(verb) - 1)] + ' ' +\
adv[random.randint(0, len(adv) - 1)]
print(line)
count += 1
except IndexError:
print('error index')

5.自制排序程序(从小到大)

import random

number = [random.randint(1, 99) for i in range(10)]
print(number)
try:
length = len(number)
for i in range(length - 1):
for j in range(i + 1, length):
if number[i] > number[j]:
number[i], number[j] = number[j], number[i]
print(number)
except IndexError:
print('index error')

很粗暴的一种排序方式,不过不计较这么多了,简单易懂就行。

。。。

Python Revisited Day 01的更多相关文章

  1. Python 学习笔记01

      print:直接输出 type,求类型 数据类型:字符串,整型,浮点型,Bool型     note01.py # python learning note 01   print('Hello w ...

  2. Python web前端 01 HTML常用标签

    Python web前端 01 HTML常用标签 一.HTML创建项目 file ---->new project -----> 输入项目名------>创建文件夹 new dicr ...

  3. Python并发编程01 /操作系统发展史、多进程理论

    Python并发编程01 /操作系统发展史.多进程理论 目录 Python并发编程01 /操作系统发展史.多进程理论 1. 操作系统 2. 进程理论 1. 操作系统 定义:管理控制协调计算机中硬件与软 ...

  4. python网络编程01 /C/S架构|B/S架构、网络通信原理、五层协议、七层协议简述、端口映射技术

    python网络编程01 /C/S架构|B/S架构.网络通信原理.五层协议.七层协议简述.端口映射技术 目录 python网络编程01 /C/S架构|B/S架构.网络通信原理.五层协议.七层协议简述. ...

  5. Python数学建模-01.新手必读

    Python 完全可以满足数学建模的需要. Python 是数学建模的最佳选择之一,而且在其它工作中也无所不能. 『Python 数学建模 @ Youcans』带你从数模小白成为国赛达人. 1. 数学 ...

  6. Python Revisited Day 13 (正则表达式)

    目录 13.1 Python的正则表达式语言 13.1.1 字符与字符类 13.1.2 量词 {m, n} ? + * 组与捕获 ?:可以关闭捕获 断言与标记 13.2 正则表达式模块 正则表达式模块 ...

  7. python --常用内置模块01

    1.简单了解模块         模块就是我们把装有特定功能的代码进行归类的解构,从代码编写的单位来看我们的程序 从小到大的顺序:一条代码< 语句块<代码块(函数,类) < 模块 我 ...

  8. 【Python】torrentParser1.01

    在昨天的版本上做了一些改进,如增加getAll,修改getSingleFileName等 代码: #-------------------------------------------------- ...

  9. python 资产扫描01

    本地建立的三个文件: Asset1.txt 用来保存扫描到的资产 Asset2.txt 用来导入给定的资产 Repeat.txt 保存重复的资产 程序的功能: 1.资产扫描,以 位置:资产 格式保存到 ...

随机推荐

  1. 决策树ID3算法的java实现(基本适用所有的ID3)

    已知:流感训练数据集,预定义两个类别: 求:用ID3算法建立流感的属性描述决策树 流感训练数据集 No. 头痛 肌肉痛 体温 患流感 1 是(1) 是(1) 正常(0) 否(0) 2 是(1) 是(1 ...

  2. python 常见函数的用法

    filter(function,ls) 函数包括两个参数,分别是function和list.该函数根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表. 如: map ...

  3. spring拦截器(interceptor)简介

    1. 拦截器用途 (1)拦截未登录用户直接访问某些链接 (2)拦截日志信息 (3)拦截非法攻击,比如sql注入 2. 涉及jar.类 (1)spring-webmvc.jar (2)HandlerIn ...

  4. 排序算法之直接插入排序的思想以及Java实现

    1,基本思想 假设待排序的数据是数组A[1-.n].初始时,A[1]自成1个有序区,无序区为A[2-.n].在排序的过程中,依次将A[i] (i=2,3,-.,n)从后往前插入到前面已排好序的子数组A ...

  5. 本学期c#总结

     本学期我学习了C#编程认识到了什么是方法什么是关键字,和代码的作用.认识到了编程不仅仅是对一个程序的编程,也是对数据的编程.程序是由数据组成的.c#本身就是一种语言,C#适用于生成面向.net fr ...

  6. nginx stream 日志设置(Version 1.9.0 +)

    nginx自1.9.0开始提供tcp/udp的反向代理功能,直到1.11.4才开始提供session日志功能. 启用stream日志配置文件 主配置文件/etc/nginx/nginx.conf增加内 ...

  7. 获取Spring容器Bean对象工具类

    在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...

  8. ES5-ES6-ES7_字符串扩展—模板字符串

    includes(), startsWith(), endsWith() 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法 ...

  9. WPF自定义控件(五)の用户控件(完结)

    用户控件,WPF中是继承自UserControl的控件,我们可以在里面融合我们的业务逻辑. 示例:(一个厌恶选择的用户控件) 后端: using iMicClassBase; using iMicCl ...

  10. Jenkins+Ansible+Gitlab自动化部署三剑客-Ansible本地搭建

    可以通过git bash连接linux 关闭防火墙,禁用防火墙开机启动,并更爱selinux文件,重启 重新登录并检查禁用 getenforce 安装git yum -y install git ns ...