1.random
【
print(random.choice(range(0,101,5)))
print(random.choice(range(0,101)))
print(random.randint(0,100))
print(random.choice([34,4.5,7.8]))
[problem]水仙花数
- 从控制台输入一个三位数,如果是水仙花数就打印“是水仙花数”,否则打印“不是水仙花数”,例如:153 = 1的三次方 + 5的三次方 + 3的三次方,则153是水仙花数
num = input("请输入一个三位数:")
g = int(num[2])
s = int(num[1])
b = int(num[0])
total = g ** 3 + b ** 3 + s ** 3
if num == total:
print("%d是一个水仙花数" % (num))
num = input("请输入一个三位数:")
if len(num) == 3:
if num.isdigit():
num = int(num)
g = num % 10
s = num // 10 % 10
b = num // 100
total = g ** 3 + b ** 3 + s ** 3
if num == total:
print("%d是一个水仙花数" % (num))
else:
print("不是全数字")
else:
print("长度不符合")
3.循环(一)
-
在编程中,在满足条件的情况下,反复执行某一段代码,在编程语言中出现的这种现象被称为循环,这段被重复执行的代码被称为循环体即循环体就是被反复执行的某段具有特殊功能的代码 -
循环:在实际项目开发中,如果遇到同样的需求需要执行多次,则使用循环简化 -
循环的好处:提高代码的可读性,提高代码的可维护性 -
书写循环需要解决的核心问题:循环的条件,如果一个循环的条件永远成立,则代码会被执行无数次,则会形成死循环,导致后面的代码没有执行的机会
"""
while 条件:
语句【循环体】
当程序在从上往下执行的过程中,遇到while语句时,首先计算表达式
的值,如果表达式的值为假,则跳过整个while语句,程序继续向下
执行;如果表达式的值为真,则执行对应的语句;执行完语句,再去
计算表达式的值,如果表达式的值为假,则跳过整个while语句,
程序继续向下执行;如果表达式的值为真,则执行对应的语句。
如此循环往复,直到表达式的值为假,整个循环才停止
"""
n = 0
while n < 10:
print("hello world")
n += 1
n1 = 0
while n1 <= 9:
print(n1)
n1 += 1
n2 = 1
total2 = 0
while n2 <= 100:
total2 += n2
n2 += 1
print(total2)
n2 = 0
total2 = 0
while n2 <= 100:
total2 += n2
n2 += 2
print(total2)
n2 = 1
total2 = 0
while n2 <= 100:
if n2 % 2 == 0:
total2 += n2
n2 += 1
print(total2)
n4 = 1
total4 = 0
while n4 <= 100:
if n4 % 2 == 0:
total4 -= n4
else:
total4 += n4
n4 += 1
n2 = 1
total2 = 1
while n2 <= 15:
total2 *= n2
n2 += 1
print(total2)
n6 = 1
count6 = 0
while n6 <= 100:
if n6 % 2 == 1:
if n6 % 3 == 0:
count6 += 1
n6 += 1
print(count6)
"""
while 表达式:
语句1
else:
语句2
和if-else语句中的作用一样,表示当表达式的值为假的时候,执行
else后面的语句2
a.if-else,当条件不成立时,才会执行else
while-else,不管条件是否成立,else都会被执行
b.特殊情况:如果while中出现了break,并且break被执行了,
则else不会被执行
"""
while 1:
print("print forever,never stop!! ")
while True:
print("print forever,never stop!! ")
n = 0
while n < 10:
print(n)
n += 1
else:
print("else被执行了")
n = 0
while n < 10:
print(n)
if n == 3:
break
n += 1
else:
print("这个else亦被执行了")
"""
while 表达式1:
while 表达式2:
语句
"""
n = 0
while n < 5:
print(n)
n += 1
m = 0
while m < 3:
print(m)
m += 1
n = 0
while n < 5:
m = 0
while m < 3:
print("%d=%d" % (n,m))
m += 1
n += 1
n = 0
m = 0
while n < 5:
while m < 3:
print("%d=%d" % (n,m))
m += 1
n += 1
'''
[分析]
"""
行 列
1x1=1 1 1
1x2=2 2x2=4 2 2
1x3=3 2x3=6 3x3=9
.....
1x9=9 2x9=18 3x9=27 ...... 8x9=72 9x9=81 9 9
规律:
a.行的取值范围:1~9
b.列的最大数量和当前的行数相等
c.列的取值范围:1~当前行数
d.循环体:列x行= 乘积
"""
'''
line = 1
while line <= 9:
colum = 1
while colum <= line:
print("%dx%d=%d" % (colum,line,line * colum),end=" ")
colum += 1
line += 1
print()
"""
*
**
***
****
*****
"""
m = 1
while m <= 5:
n = 1
while n <= m:
print("*",end="")
n += 1
print()
m += 1
print("*" * 20)
n = 1
while n <= 5:
print("*" * n)
n += 1
"""
行 * 空格
* 1 1 4
*** 2 3 3
***** 3 5 2
******* 4 7 1
********* 5 9 0
规律:
a.行的取值范围:1~5 i
b.*的个数的取值范围:1~ 2 * i - 1
c.空格的个数的取值范围:1 ~ 5 - i
"""
i = 1
while i <= 5:
j = 1
while j <= 5 - i:
print(" ",end="")
j += 1
k = 1
while k <= (2 * i - 1):
print("*",end="")
k += 1
print()
i += 1
i = 1
while i <= 5:
print(" " * (5 - i),end="")
print("*" * (2 * i - 1))
i += 1
- break
- 作用:跳出for和while的循环体【直接跳出当前循环,继续执行循环后面的代码】
- 注意:跳出距离最近的for或者while语句
"""
break是一个关键字,可以单独作为一条语句使用
break的作用:主要应用在循环中,目的是为了结束循环,注意:跟if语句没有关系,if语句只是充当条件限制
"""
n = 0
while n < 10:
print(n)
if n == 4:
break
n += 1
"""
0
1
2
3
4
"""
m = 0
while m < 3:
n = 0
while n < 5:
print("%d=%d" % (m,n))
if n == 3:
break
n += 1
m += 1
print("=" * 30)
m = 0
flag = False
while m < 3:
n = 0
while n < 5:
print("%d=%d" % (m,n))
if n == 3:
flag = True
break
n += 1
if flag:
break
m += 1
- continue
- 作用:跳过当前正在执行的循环,然后继续进行下一次循环【结束本次循环,继续执行下一次循环】
n = 0
while n < 10:
print(n)
if n == 4:
n += 1
continue
n += 1
n = 0
while n < 10:
if n == 4:
n += 1
continue
print(n)
n += 1
error_num = 0
while True:
username = input("请输入你的用户名:")
password = input("请输入你的密码:")
if username == "admin" and password == "123":
print("登录成功")
break
else:
print("用户名或者密码有误,请重新输入")
error_num += 1
if error_num == 3:
print("已经累积错误三次,禁止输入")
exit()
else:
continue
- pass
- pass:是一个关键字,单独作为语句使用,被称为占位语句
- 为了保证代码结构的完整性,在if语句,while语句,for语句以及函数等代码块中使用
n = 0
if n < 10:
pass
else:
print("fag")
while 1:
pass
4逻辑运算符
- and or not
- A and B
a.A为真,则A and B的结果是B b.A为假,则A and B的结果为A 【短路原则】 - A or B
a.A为真,则A or B的结果是A 【短路原则】 b.A为假,则A or B的结果为B
"""
True and True = True
True and False = False
False and True = False
False and False = False
规律:全真为真,一假为假
"""
n1 = 10
n2 = 20
if n1 - 10 and n2:
print("ok")
else:
print("nook")
if n1 == n2 and n1 < n2:
print("ok")
else:
print("nook")
"""
True or True = True
True or False = True
False or True = True
False or False = False
规律:全假为假,一真为真
"""
if n1 - 10 or n2:
print("ok")
else:
print("nook")
if n1 == n2 or n1 < n2:
print("ok")
else:
print("nook")
year = int(input("请输入一个年份:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("闰年")
if not (n1 - 10):
print("ok")
else:
print("nook")
if not (n1 == n2):
print("ok")
else:
print("nook")
print(3 and 6)
print(True and "xyz")
print(7 and True)
print(6 and 0)
print(True and "")
print(0.0 and "")
print(3 or 6)
print(True or "xyz")
print(7 or True)
print(6 or 0)
print(True or "")
print(0.0 or "")
"""
短路原则
1.A and B,如果A为False,不需要计算B的值,整个表达式的结果为False
2.A or B,如果A为True,不需要计算B的值,整个表达式的结果为True
3.and和or混合使用
1>表达式从左往右运算,如果第一个运算符是or,or的左侧为True,则会短路or后面所有的表达式【不管是and还是or连接】,整个表达式的结果为True
2>表达式从左往右运算,如果第一个运算符是and,and的左侧为False,则短路后面所有的and,直到or出现,接着计算
3>如果or的左侧为False,或者and的左侧为True,则不能使用短路逻辑判断
4>所有被短路的表达式均不会被输出
"""
def a():
print("A")
return False
def b():
print("B")
return False
def c():
print("C")
return True
def d():
print("D")
return False
def e():
print("E")
return True
if a() and b() and c() and d() and e():
print("hello~~1")
def a():
print("A")
return True
def b():
print("B")
return False
def c():
print("C")
return True
def d():
print("D")
return False
def e():
print("E")
return True
"""
False and True and False and True ----->A B
整个表达式的结果为False
"""
if a() and b() and c() and d() and e():
print("hello~~2")
def a():
print("A")
return True
def b():
print("B")
return False
def c():
print("C")
return True
def d():
print("D")
return False
def e():
print("E")
return True
if a() or b() or c() or d() or e():
print("hello~~3")
def a():
print("A")
return False
def b():
print("B")
return False
def c():
print("C")
return True
def d():
print("D")
return False
def e():
print("E")
return True
"""
False or True or False or True --->A B
True or False or True ---->C
整个表达式的结果为True
"""
if a() or b() or c() or d() or e():
print("hello~~4")
def a():
print("A")
return False
def b():
print("B")
return False
def c():
print("C")
return True
def d():
print("D")
return False
def e():
print("E")
return True
def f():
print("F")
return False
def g():
print("G")
return False
def h():
print("H")
return True
def m():
print("M")
return False
def n():
print("N")
return True
"""
False or True and False and False or True or False and True ---->A
True and False and False or True or False and True --->E
False and False or True or False and True ---->F
False or True or False and True
True or False and True --->H
整个表达式的结果为True
"""
if a() and b() and c() and d() or e() and f() and g() or h() or m() and n():
print("hello~~~5")
5.成员运算符
- in:如果在指定的序列中找到值,则返回True,否则返回False
- not in:如果在指定的序列中没有找到值,则返回True,否则返回False
6.身份运算符
-
身份运算符用于比较两个对象的存储单元【地址】 is:判断两个标识符是不是引用自一个对象 is not:判断两个标识符是不是引用自不同对象
a = 10
b = 20
print(a == b)
print(a is b)
a = 10
b = 10
print(a == b)
print(a is b)
"""
【problem] is和==的区别
is:比较的是两个变量【对象】的地址
==:比较的是两个变量【对象】的内容
如果两个变量的地址相同,则这两个变量的内容一定相同
如果两个变量的内容相同,则这两个变量的地址不一定相同
"""
7.三目运算符
num = 15
result = ""
if num % 2 == 0:
result = "偶数"
else:
result = "奇数"
print(result)
num = 10
result = "偶数" if num % 2 == 0 else "奇数"
print(result)
result1 = num % 2 == 0 and "偶数" or "奇数"
print(result1)
|