2的32次方减1用python,第二章python入门

2.1 环境的安装

解释器:py2、py3

开发工具:pycharm

2.2编码

2.2.1编码基础

初识编码

ASCII码 :英文 8位表示一个东西 2**8 ,8位等于一个字节

Unicode:(分ecs2 ,ecs4) 32位表示一个东西 2**32 ,32位等于4字节

Utf-8:对Unicode进行压缩,尽量用少的位数去表示一个东西,以8个位为一个单位(Utf-8中最少用1字节=8位,最多用4字节=32位表示,中文:3字节=24位表示)

gb2312:中文用2字节

gbk:中文用2字节

单位

8bit =1byte

1024byte = 1kb

1024kb = 1mb

1024mb = 1gb

1024gb = 1tb

1024tb = 1pb

编码和解码要一致

python2 有中文的不能运行,python2默认用的是ASCII码;python3可以运行有中文的,python3用utf-8

编写文件时,保存文件要用utf-8格式,以什么编码保存,就要用什么编码方式打开,否则出现乱码

2.2.2python编码相关

对于python默认解释器编码:

1.py2:ASCII,在文件的头部加:#--coding:utf-8 --

2. py3:utf-8

注意:对于操作文件时,要按照以什么编码写入的就要按照什么编码打开,否则出现乱码

3.输入输出:

py2:输入:raw_input;输出:print""

py3:输入:input;输出:print()

2.3变量

2.3.1命名规范

1.变量名只能包含:字母/数字/下划线,不能用数字开头

2.不能用数字开头

3.不能是python的关键字

4.命名建议:见名知意:name = “alex” age= 18

用下划线连接:Alex_dad = “xxx”

驼峰式命名:Alexdad = “xxx”

2.3.2变量的意义

为某个值创建一个名称,以后在使用时通过此名称就可以直接调用

Content =“钓鱼要钓刀鱼,刀鱼要到岛上钓”

Print(content)

name= "123"

print(name)

2.4运算符

运算符描述示例

=

将右侧操作数的值分配给左侧操作数

c=a+b表示将a+b的值分配给c

+=

将右侧操作数相加到左操作数,并将结果分配给左操作数

c += a等价于c=c+a

-=

将左侧操作数减右操作数,并将结果分配给左操作数

c -= a等价于c = c -a

*=

将右侧操作数与左操作数相乘,并将结果分配给左操作数

c * = a等价于c =c * a

/=

将左侧操作数除以右侧操作数(求商),并将结果分配给左侧操作数

c /= a等价于c = c / a

%=

将左侧操作数除以右侧操作数的余数分配给左侧操作数

c %=a等价于c=c%a

**=

执行指数(幂)计算,分配给左侧操作数

c=a等价于c=c**a

//=

运算符执行地板除运算(除法时余数取整),并将值分配给左操作数

c//=a等价于c=c//a

in

判断“中国”是否在value所代指的字符串中,中国是否是value所代指的字符串子序列

value = "我是中国人"

2.4.1取余数

用 % 表示取余数

value = 11 % 3

print(value)

输出1-100内的所有偶数

count= 1sumall=0while count <= 100:if count % 2 ==0:

sumall+=count

count+= 1

print(sumall)#输出 1-100 内的所有奇数

count = 1sumall=0while count <= 100:if count % 2 == 1:

sumall+=count

count+= 1

print(sumall)

2.4.2次方的计算

用 **表示次方

例:

value= 2 **8 #次方

print(value)

2.4.3除法

用 / 表示

例:

value= 8 /2 #除法

print(value)

2.4.4除法中余数取整

用 // 表示

例:

value=9//2 #取整数

print(value)

使用两种方法实现输出 1 2 3 4 5 6 8 9 10 。

for i in range(1,11):

if i !=7:

print(i)

for i in range(1,11):

if i ==7:

pass

else:

print(i)

2.5赋值运算

2.6逻辑运算

逻辑运算 and or not注意:优先级()>not>and>or,同一级从左到右计算

大于等于小于在not之前not 2 > 1 #正确

not 2 >1 #错误

需要记的是字符串转数字#数字转字符串#""/0 转换布尔值false

2.6.1  or

value = 1 or 9第一个值如果是转换成布尔值如果是真的,则value=第一个值

第一个值如果转换成布尔值如果是假的,则value=第二个值

如果有多个or条件,则从左到右依次进行计算

v1= 0 or 1v2= 8 or 10v3= 0 or 9 or 8

‘‘‘

‘‘‘value= 0 or 9

print(value)

value= 0 or " "

print(‘--->‘,value,‘

value= 0 or 9 or 8 #从左到右计算

print(value)‘‘‘

2.6.2 and

#对于and#如果第一个值转换成布尔值是true,则value= 后面的值#如果第一个值转换成布尔值是false,则value= 第一个值#多个and条件,从左到右依次执行上述流程

v1= 1 and 9

print(v1) #9

v2= 1 and0print(v2) #0

v3= 0 and 7

print(v3) #0

v4= 0 and " "

print(v4)#0

v5= 1 and 0 and 9

print(v5) #0

2.6.3 计算顺序

#综合 先看and在看or

v1 = 1 and 9 or 0 and 6

print(v1)#一般情况下

‘‘‘if 1>0 and 1>2:

print(‘666‘)

#二班情况

##小知识:int str bool

#数字转字符串

v1= 666

v2 = str(v1)

#字符串转数字

v1 = ‘666‘

v2 = int(v1)

value

v1 = 1

v2 = bool(v2)

print (v2) #数字转bool值,除了0是false以外 其他的都是true

v1 = ‘alex‘

v2 = bool(v1)

print (v2) #字符串转布尔值,空字符串是false,其他的都是true

#布尔值转换其他

v1 = True

v2 = int(v1)

print (v2) #当转换成整形是false时是0,true是1,当转换成字符串时true就是true,false就是false

2.6.4 例题

6 or 2 > 1 #6 or true 6

3 or 2 > 1#3 or true 3

0 or 5 < 4#0 or False False

5 < 4 or 3#3

2 > 1 or 6#True

3 and 2 > 1#true

0 and 3 > 1#0

2 > 1 and 3#3

3 > 1 and 0#0

3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 #

#true and 2 or false and 3 and 4 or true#2 or false and 4 or true#2 or false or true#2

‘‘‘print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)

2.7 while循环语句

#通过循环,让count每次循环+1

count = 1

whileTrue:print(count)

count= count +1

2.7.1 关键字:break(终止当前的循环)

关键字: break(终止当前的循环)"""while True:

print(666)

break

print(‘结束‘)

#通过break实现1-10

count = 1

while True:

print(count)

if count ==10:

break

count = count +1

print(‘结束‘)

while True:

print(‘你好‘)

while True:

print(666)

break #终止当前循环

2.7.2 关键字:continue 本次循环如果遇到continue,则不继续往下走,而是回到判断位置

关键字:continue本次循环如果遇到continue,则不继续往下走,而是回到判断位置#打印123456 89

‘‘‘count = 1

while count<=10:

if count ==7:

count = count + 1

continue

print(count)

count = count +1

2.8while else循环语句

#while else 语句

count = 1

whileTrue :print(count)if count ==10:breakcount= count +1

else: #不在满足while后的条件时,触发,或条件=false

print(‘else代码块‘)print(‘结束‘)

2.9 if嵌套

message = """欢迎致电10086

1、话费查询;

2、流量服务;

3、业务办理;

4、人工服务"""

print(message)

index= input("请输入你要选择的业务:")

index=int(index)if index ==1:print("话费查询")elif index ==2:print("流量服务")elif index ==3:

content= """业务办理

1、修改密码;

2、更改套餐;

3、停机"""

print(content)

value= input("请输入要办理的业务:")

value=int(value)if value == 1:print("修改密码")elif value == 2:print("更改套餐")elif value == 3:print("停机")else:print("错误")elif index ==3:print("人工服务")else:print("输入错误")

原文:https://www.cnblogs.com/xu1296634150/p/12437320.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值