1. 初识编码

最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字。

ascii 只能表示256种可能,太少,
后来创办了万国码 unicode
16表示一个字符不行,32位表示一个字符。
  A 01000001010000010100000101000001
  B 01000010010000100100001001000010
  我 01000010010000100100001001000010
Unicode 升级 utf-8 utf-16 utf-32
  8位 = 1字节bytes
  utf-8 一个字符最少用8位去表示,英文用8位 一个字节
  欧洲文字用16位去表示 两个字节
  中文用24 位去表示 三个字节
  utf-16 一个字符最少用16位去表示

gbk 中国人自己发明的,一个中文用两个字节 16位去表示。

编码转换

#str --->byte  encode 编码
s = '亦双弓'
b = s.encode('utf-8')
print(b)
#byte --->str decode 解码
s1 = b.decode('utf-8')
print(s1) #字母数字特殊字符的编码 utf-8 与 gbk 可以互转
s = 'abc##123'
b = s.encode('utf-8')
print(b)
#byte --->str decode 解码
s1 = b.decode('gbk')
print(s1)

2.单位转换

  1bit 8bit = 1bytes
  1byte 1024byte = 1KB
  1KB 1024kb = 1MB
  1MB 1024MB = 1GB
  1GB 1024GB = 1TB

3.格式化输出

name = input('请输入姓名:')
age = input('请输入年龄:')
height = input('请输入身高:')
msg = '我叫%s,今年%s,身高%s'%(name,age,height)
print(msg)
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入工作:')
hobbie = input('你的爱好:')
msg = '''
------------ info of %s ------------ Name:%s Age:%d Job:%s hobbie:%s ----------------end-----------------
'''%(name,name,int(age),job,hobbie)
print(msg)

在输出语句中有时会使用到 % ,这时需要使用 %% 来转义,如下:

name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
print(msg)

4.逻辑运算

and or not
优先级,()> not > and > or

  not –表示取反运算。

  and –表示取与运算。

  or –表示取或运算。

print(1<4 and 8>7)
print(1<4 and 8>9)
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2) print(3>4 or 4<3 and 1==1) #F
print(1 < 2 and 3 < 4 or 1>2) #T
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) #T
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) #F
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) #F
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) #F

x or y,x 为非零,则返回x,否则返回 y

print(1 or 2)   #
print(3 or 2) #
print(0 or 2) #
print(0 or 100) #
print(2 or 100 or 3 or 4) #
print(0 or 4 and 3 or 2) #

x and y,x True,则返回y

print(1 and 2)  #
print(0 and 2) #
print(2 or 1 < 3) #
print(2 < 3) #T
print(3 > 1 or 2 and 2) #T
print(2 or 1 < 3 and 2) #
print (1 > 2 and 3 or 4 and 3 < 2) #F

int  ----> bool   非零转换成bool True   0 转换成bool 是False

print(bool(2))
print(bool(-2))
print(bool(0))
#bool --->int
print(int(True)) #
print(int(False)) #

5. while  else循环

count = 0
while count <= 5 :
count += 1
if count == 3:break
print("Loop",count)
else:
print("循环正常执行完啦")
print("-----out of while loop ------")

相关练习

1.判断一下输出

print(5<4 or 6)
print(5>4 or 6)
print(5<4 and 6)
print(5>4 and 6)

2. 计算 1-2+3...+99 中除88以外的数总和

自己写的第一种好理解,第三种逻辑很新颖,值得学习

#方法一
count=1
sum=0
while count <=99:
if count%2==1:
sum = sum + count
elif count == 88:
pass
else:
sum = sum - count
count += 1
print (sum) #方法二
count=1
sum=0
while count <=99:
if count == 88:
count += 1
continue
if count%2==1:
sum = sum + count
else:
sum = sum - count
count +=1
print (sum) i = input('数字')
if i=='':
print(1212)
if i =='':
print(11111)
else:
print(666) #方法三
i=0
j=-1
sum = 0
while i<99:
i += 1
j = -j
if i== 88:
continue
else:
sum +=i*j
print(sum)

3. ⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

#方法一:
i=0
j=2
while i<3:
user=input("请输入账号:")
password=input("请输入密码:" )
if user=='ysg' and password=='':
print('欢迎回来'+user)
break
elif i==2:
print("超出登录次数,账号已冻结")
else:
print('---输入错误,还剩余' + str(j) + '次机会,请重新输入---')
i += 1
j =j-1 #方法二:
i=2
while i>=0:
user=input("请输入账号:")
password=input("请输入密码:" )
if user=='ysg' and password=='':
print('欢迎回来%s'%(user))
break
elif i==0:
print("超出登录次数,账号已冻结")
else:
print('---输入错误,还剩余' + str(i) + '次机会,请重新输入---')
i -= 1 #方法三
i=2
while i>=0:
user=input("请输入账号:")
password=input("请输入密码:" )
if user=='ysg' and password=='':
print('欢迎回来%s'%(user))
break
elif i==0:
print("超出登录次数,账号已冻结")
again=input("再试一次?Y or N:")
if(again=='Y'):
i=3
else:
print('---输入错误,还剩余' + str(i) + '次机会,请重新输入---')
i -= 1
#else:
# print("还在试???")

day 2 - 逻辑运算的更多相关文章

  1. C语言 第四章 关系、逻辑运算与分支流程控制

    一.关系运算 若干表达式用关系运算符连接而成,就构成关系表达式. 关系表达式中,表达式成立,其值为1(真):表达式不成立,其值为0(假).其他表达式中,其结果如果是非0则为真,结果为0则为假. 关系运 ...

  2. SQL Server中可能为null的变量逻辑运算的时候要小心

    DECLARE @a int declare @b int IF(@a<>@b) print('@a<>@b') else print('@a=@b') ) print('b& ...

  3. C# Enum 进行逻辑运算

    Enum定义 enum 全称(Enumeration),即一种由一组称为枚举数列表的命名常量组成的独特类型. 通常情况下,最好是在命名空间內直接定义 enum,以便该命名空间中所有的类都能够同样方便地 ...

  4. zstu.4189: 逻辑运算(构建 && 前缀表达式入门)

    4189: 逻辑运算 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 274  Solved: 42 Description 还记得大学里学过的模电么, ...

  5. scjp考试准备 - 2 - 逻辑运算及类型转换

    判断如下代码的执行结果: public class Spock{ public static void main(String[] args){ Long tail = 2000L; Long dis ...

  6. R中逻辑运算

    一.是否相等的判断的方法 (1)判断字符串是否相等is.null(x) (2)判断x的每个元素是否在y中出现: x %in% y (3)判断判断每个相对应的元素是否相等: x == y (4)判断近似 ...

  7. 带控制端的逻辑运算电路_分别完成正整数的平方、立方和阶乘的运算verilog语言

    练习:设计一个带控制端的逻辑运算电路,分别完成正整数的平方.立方和阶乘的运算. //--------------myfunction---------- modulemyfunction(clk,n, ...

  8. 5 - SQL Server 2008 之 四则运算、比较运算、逻辑运算及字符连接运算

    四则运算如下: --加减乘除(+.-.*.\.%)取余运算 SELECT --加法运算 AS 加法结果2, --减法运算 -2.5 AS 减法结果1, 15.5+5.5 AS 减法结果2, --乘法运 ...

  9. Python新手学习基础之运算符——赋值与逻辑运算

    赋值也是一种运算符 我们在之前的章节,了解过,在Python里变量是不需要声明的,只要变量的标识符合法,就可以直接定义并赋值,而且Python也允许我们同时为多个变量赋值(包括为多个变量赋不同类型的值 ...

  10. 【转】nginx之逻辑运算

    nginx的配置中不支持if条件的逻辑与&& 逻辑或|| 运算 ,而且不支持if的嵌套语法,否则会报下面的错误:nginx: [emerg] invalid condition. 我们 ...

随机推荐

  1. 详解python的垃圾回收机制

    python的垃圾回收机制 一.引子 我们定义变量会申请内存空间来存放变量的值,而内存的容量是有限的,当一个变量值没有用了(简称垃圾)就应该将其占用的内存空间给回收掉,而变量名是访问到变量值的唯一方式 ...

  2. spring中获取当前项目的真实路径

    总结: 方法1: WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext ...

  3. 2. github创建 git仓库,克隆,拉取和推送操作(所有的git命令前提是一定在当前项目目录下)

    步骤: 1.点击小猫,回到初始页面 2.点击start a project,首先会出现一个验证email地址(我们注册的时候,有一个email输入,进去邮箱验证一下) 3.重新点击start a pr ...

  4. session会话对象

    一.session会话对象介绍: 会话对象让你能够跨请求保持某些参数,它也会在同一个session实例发出的所有请求之间保持cookie. 二.步骤 1.对session对象进行一次实例化 2.进行登 ...

  5. mysql 5.7 启动脚本

    最近这段时间,在看mysql,安装了,也应用过,对于生产环境中,一般都选择使用source code安装,在安装的时候可以自定义相关路径和内容,对于生产环境来说更有效.相对于mysql 5.5的安装, ...

  6. springboot1.5升级2.0后遇到的问题

    https://blog.csdn.net/zhiquanzhou/article/details/80566630

  7. (栈)leetcode 946. Validate Stack Sequences

    Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...

  8. thinkphp 攻略

    php框架     一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难, ...

  9. gulp入门指南

    1. 全局安装 gulp: $ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: $ npm install --save-dev ...

  10. [报错]Could not get a resource from the pool

    redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool解决:开启 ...