【RUNOOB】python学习之python3实例以及知识点解析
1.python数字求和
1 #!/usr/bin/python3 2 3 num1=input('input first number:') 4 num2=input('input second number:') 5 6 sum=float(num1) + float(num2) 7 print('number {0} + number {1} is {2}').format(num1,num2,sum)
1.1 读取输入
https://www.runoob.com/python3/python3-inputoutput.html
(1) input内置函数用于读取用户输入;
1.2 类型转换(显式类型转换与隐式类型转换)
https://www.runoob.com/python3/python3-type-conversion.html
(1) 隐式类型转换中,python自动将一种数据类型转换为另外一种数据类型,不需要用户干预;
(2) 显式类型转换需要使用int(), float(), str()等预定义函数;
1.3 str.format()函数格式化输出
https://www.runoob.com/python3/python3-inputoutput.html
2.python平方根
1 #!/usr/bin/python3 2 3 num = float(input('请输入一个数字: ')) 4 num_sqrt = num ** 0.5 5 print(' %0.3f 的平方根为 %0.3f'%(num ,num_sqrt))
2.1 旧式字符串格式化
https://www.runoob.com/python3/python3-inputoutput.html
1 #!/usr/bin/python3 2 import cmath 3 4 num = int(input("请输入一个数字: ")) 5 num_sqrt = cmath.sqrt(num) 6 print('{0} 的平方根为 {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
2.2 模块的使用以及math与cmatch模块的对比
https://www.runoob.com/python3/python3-module.html
https://www.runoob.com/w3cnote/python-math-and-cmath-module.html
3.计算圆的面积
3.1 函数与函数调用
https://www.runoob.com/python3/python3-function.html
1 #!/usr/bin/python3 2 3 def findArea(r): 4 PI = 3.142 5 return PI * (r*r) 6 7 # 调用方法 8 print("圆的面积为 %.6f" % findArea(5))
4.python交换变量(使用临时变量或不使用临时变量)
4.1 不使用临时变量的变量交换
1 #!/usr/bin/python3 2 x = input('输入 x 值: ') 3 y = input('输入 y 值: ') 4 5 # 不使用临时变量 6 x,y = y,x 7 8 print('交换后 x 的值为: {}'.format(x)) 9 print('交换后 y 的值为: {}'.format(y))
5.python if与内嵌if
https://www.runoob.com/python3/python3-conditional-statements.html
1 #!/usr/bin/python3 2 3 num = float(input("输入一个数字: ")) 4 if num > 0: 5 print("正数") 6 elif num == 0: 7 print("零") 8 else: 9 print("负数") 10 11 12 num = float(input("输入一个数字: ")) 13 if num >= 0: 14 if num == 0: 15 print("零") 16 else: 17 print("正数") 18 else: 19 print("负数")
6.python判断字符串是否为数字
(1) python float函数(),异常处理try/except语句,pass语句
https://www.runoob.com/python/python-func-float.html
https://www.runoob.com/python/python-exceptions.html
https://www.runoob.com/python/python-pass-statement.html
1 #!/usr/bin/python3 2 def is_number(s): 3 try: 4 float(s) 5 return True 6 except ValueError: 7 pass 8 9 try: 10 import unicodedata 11 unicodedata.numeric(s) 12 return True 13 except (TypeError, ValueError): 14 pass 15 16 return False
7.python获取最大值函数
(1) python max()函数;
https://www.runoob.com/python/func-number-max.html
1 #!/usr/bin/python3 2 print(max(1, 2)) 3 print(max('a', 'b')) 4 5 # 也可以对列表和元组使用 6 print(max([1,2])) 7 print(max((1,2))) 8 9 # 更多实例 10 print("80, 100, 1000 最大值为: ", max(80, 100, 1000)) 11 print("-20, 100, 400最大值为: ", max(-20, 100, 400)) 12 print("-80, -20, -10最大值为: ", max(-80, -20, -10)) 13 print("0, 100, -400最大值为:", max(0, 100, -400))
8.python质数判断
(1) python range()函数, 运算符, 循环语句for/else结构;
https://www.runoob.com/python/python-func-range.html
https://www.runoob.com/python3/python3-basic-operators.html
https://www.runoob.com/python3/python3-loop.html
1 #!/usr/bin/python3 2 num = int(input("请输入一个数字: ")) 3 4 # 质数大于 1 5 if num > 1: 6 # 查看因子 7 for i in range(2,num): 8 if (num % i) == 0: 9 print(num,"不是质数") 10 print(i,"乘于",num//i,"是",num) 11 break 12 else: 13 print(num,"是质数") 14 15 # 如果输入的数字小于或等于 1,不是质数 16 else: 17 print(num,"不是质数")
9.python输出指定范围内的素数
(1) 循环中的else子句(如for...else)-只有当循环正常结束时,才会执行的else子句;如果循环中执行了break语句,那么循环中的else子句将不会执行;
1 #!/usr/bin/python3 2 3 lower =int(input("min value of a range:")) 4 upper=int(input("max value of a range:")) 5 6 for num in range(lower, upper+1) 7 if num>1: 8 for i in range(2,num): 9 if (num%i)==0: 10 break 11 else: 12 print(num)
10.python阶乘实例
1 #!/usr/bin/python3 2 3 num=int(input("pls input a number:")) 4 5 factorial=1 6 7 if num<0: 8 print("you shoule input a number that is larger than 0") 9 elif num==0: 10 print("0!=1") 11 else: 12 for i in range(1,num+1): 13 factorial=factorial*i 14 print("(%d)! is %d"%(num,factorial))