python learning - basic and operators

Next is the second day of the study, on the basis of operators and parts, first put the mind map:

while loop

(1) while loop, finished condition after the determination colon ":", start another four empty spaces write cycle specific logic

the while True:
     Print ( ' Life is short, I have Python ' )

The results of the print cycle:

Further written in python while there is a pass, skip this cycle, if there is no direct in java.

# Add writing pass is performed to pass, directly over, java wording does not seem to pass 
COUNT = 0
 the while COUNT <10 : 
    COUNT = COUNT +. 1
     IF COUNT ==. 7 :
         pass 
    the else :
         Print (COUNT)
 Print ( ' End ' )

The results, pass Keywords:

(2) break keyword

break keyword is the end of the cycle, no longer behind the implementation cycle

# break关键字的使用,终止当前循环,外层循环不会终止
# 通过break实现打印1-10
count = 1
while True:
    if count > 10:
        break
    print(count)
    count = count + 1
print('break结束')

执行结果,break关键字:

(3)continue关键字

continue关键字只是结束当前循环,不再执行continue后面的代码,直接跳到下一次循环。

# continue关键字的使用,直接跳到下一个循环,不再执行本次循环continue后面的代码
# 打印1~10,但是不包含7
count = 1
while count <= 10:
    if count == 7:
        count = count + 1
        continue
    print(count)
    count = count + 1
print('continue结束')

测试结果,执行到最后10次循环,只是跳过第7次循环

(4)while else,如果跳出while循环,直接执行else里面的代码块

# while else,很少用,else里面代码块需要while循环条件不满足时才会执行
count = 1
while count <= 10:
    if count == 7:
        count = count + 1
        continue
    print(count)
    count = count + 1
else:
    print('这是else里面的代码块')
print('while else结束')

测试结果:

格式化输出

格式化输出的作用是,当需要输出的内容有一部分需要被替换掉时,就会使用占用符先占着,后面有值就替换掉。

(1)%s:字符串替换

(2)%d:数字替换

(3)%%:当需要输出百分号%时使用

# -*- coding:utf-8 -*-

# 字符串格式化 主要使用三个 %s %d %%,这些都是占位符

# 最常用的是%s
# name = input('请输入姓名')
# do = input("请输入在做什么")
#
# msg = '%s今天在%s' % (name, do,)  # 最后需要以逗号结尾,暂时先全部这么处理
# print(msg)

# 如果有数字,使用%d,当然使用%s也是可以的
# name = input('请输入姓名')
# age_str = input("请输入年龄")
# age = int(age_str)
# do = input("请输入在做什么")
#
# msg = '%s今天%d在%s' % (name, age, do,)  # 最后需要以逗号结尾,暂时先全部这么处理
# print(msg)

# 如果需要打印%,需要使用%%
# name = input('请输入姓名')
#
# msg = '%s手机电量有100%%' % (name,)  # 最后需要以逗号结尾,暂时先全部这么处理
# print(msg)

# 案例
name = input('请输入姓名')
age = input('请输入年龄')
hobby = input('请输入爱好')

msg = '''
name:%s
age:%s
hobby:%s
'''
format_str = msg % (name, age, hobby,)
print(format_str)

测试结果:

运算符

运算符部分,跟Java类似,如下:

(1)算术运算:加减乘除取模取幂,还有取整数商
(2)比较运算:大于小于等于
(3)赋值运算:跟java类似,为简写形式
(4)逻辑运算:与或非,与java一样,写法不一样,写成and or not
(5)成员运算:in,not in
(6)运算符优先级 运算符是有优先级的,一般用的多就是逻辑运算>比较运算>算术运算,参考文末链接。

# -*- coding:utf-8 -*-

# 算术运算符,主要有以下几个暂时学习
'''
+ 加
- 减
* 乘
/ 除
% 取模
** 取幂# -*- coding:utf-8 -*-
// 取整除后的整数商
'''  # -*- coding:utf-8 -*-

number = 2 ** 8  # 256
print(number)

number = 9 // 2  # 4
print(number)

# 计算1-100的和
count = 1
sum = 0
while count <= 100:
    sum = sum + count
    count = count + 1
print('1-100相加的结果为:')
print(sum)

# 计算1-2+3-4+5+...-100
count = 1
sum = 0
while count <= 100:
    if count % 2 == 0:
        sum = sum - count
    else:
        sum = sum + count
    count = count + 1
print('1-2+3-4+5+...-100运算的结果为:')
print(sum)

# 比较运算符 与java类似,跳过
'''
== 
!=
>
<
>=
<=
'''

# 赋值运算 与java类似,其中**和//java中好像没有,待确认
'''
= 简单赋值 c=a+b
+= 加法赋值运算符 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
'''

# 逻辑运算符
'''
and 与
or 或
not 非
'''

# 入坑面试题
var = 1 or 9
print('运算结果为:', var)  # 1

'''
(1)字符串转数字
count=int('666')
(2)数字转字符串
score=str(666)
(3)数字转布尔
v1=bool(1)
v2=bool(9)
v3=bool(0)
(4)字符串转布尔值
v4=bool('666')
v5=bool('')
v6=bool(' ')
'''
count = int('666')
print(count)

score = str(666)
print(score)

v1 = bool(1)
print(v1)  # true

v2 = bool(9)
print(v2)  # true

v3 = bool(0)
print(v3)  # false

v4 = bool('666')
print(v4)  # true

v5 = bool('')
print(v5)  # false 字符串转布尔,只有空字符串才能为false

v6 = bool(' ')
print(v6)  # true

# 转换
'''
--字符串转数字
--数字转字符串
--字符串和数字都可以转布尔型,只有字符串""和数字0,转换后都是false,其他都是true
'''

'''
官方解释:
(1)x or y 如果x是true,后面就不用看了,直接为x,如果x为false,还要看y的,结果为y 
(2)x and y 如果x为false,后面就不用看了,直接为x,如果x为true,还要看y的,结果为y
(3)not x 如果x为false,结果为True,否则为False
'''

var = 0 or 9
print(var)  # 9

var = 0 or ""
print('--->', var, '<---')  # --->  <---

var = 0 or 9 or 8
print(var)  # 9

print('---下面是and---')

var = 1 and 0
print(var)  # 0

var = 1 and 9
print(var)  # 9

var = 1 and ''
print('--->', var, '<---')  # --->  <---

var = 0 and 9
print(var)  # 0

var = 0 and ''
print('--->', var, '<---')  # ---> 0 <---

var = 1 and 0 and 9
print(var)  # 0

print('---下面是and和or的混合,先看and再看or---')

var = 1 and 9 or 0 and 6
print(var)  # 9

var = 5 < 4 or 3
print(var)

print('---下面练习---')
# 运算符优先级 not and or 大于 比较运算符
var = 6 or 2 > 1
print(var)  # 6

var = 3 or 2 > 1
print(var)  # 3

var = 0 or 5 < 4
print(var)  # False

var = 5 < 4 or 3
print(var)  # 3

var = 2 > 1 or 6
print(var)  # True

var = 3 and 2 > 1
print(var)  # True

var = 0 and 3 > 1
print(var)  # 0

var = 2 > 1 and 3
print(var)  # 3

var = 3 > 1 and 0
print(var)  # 0

'''
下面的运算结果是2
'''
var = 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
print(var)  # 2

var = 3 > 1 and 2 or 2 < 3 and 3
print(var)
View Code

编码

编码第一课已经说明了,这里再记录下,主要有如下几种:

(1)ASCII :1个字节
(2)Unicode:4个字节,万国码
(3)UTF-8:变长字节1~4个字节,中文使用3个字节
(4)GBK:2个字节,中文使用节省空间
(5)gb2312:2个字节,中文使用节省空间

 

参考链接:

(1)https://pythonav.com/wiki/detail/1/3/#4.2%20GBK

(2)https://blog.csdn.net/ZYY88886666/article/details/75285780

(3)https://www.cnblogs.com/youngchaolin/p/10927325.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/10963219.html