:条件循环
Posted ling07
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了:条件循环相关的知识,希望对你有一定的参考价值。
第1讲:基础技能的补充
1.print函数的总结
print("hello world!") print("hello "+"world!") name = "Bela" print(name) print("your name is:",name) print(1,2,3) print((1,2,3)) print("==========") age = 19 # Bela age is 19 print(name,"age is",age) 结果: hello world! hello world! Bela your name is: Bela 1 2 3 (1, 2, 3) ========== Bela age is 19
# 变量不允许+连接,会出错 print(name+"age is"+age) 结果: Traceback (most recent call last): File "C:/Users/lenovo/PycharmProjects/Demo/demo.py", line 13, in <module> print(name+"age is"+age) TypeError: can only concatenate str (not "int") to str
2.模块
import math
from math import pi,cos,pow
from math import *
import math as tmath
import math as tmath from math import pi as p
import math # 导入整个模块,使用该模块下的函数方式为:math.* print(math.pi) r = 2 s = math.pi * r * r print("圆的面积为:",s) print("==============================") from math import pi,cos,pow # 导入math模块下pi.cos.pow函数 print(pi) r = 2 s = pi * r * r print("圆的面积为:",s) print(pow(2,5)) print("==============================") from math import * # 慎用该方式,只有确定自己想要从给定的模块导入所有功能时,才使用 print(pi) r = 2 s = pi * r * r print("圆的面积为:",s) print("==============================") import math as tmath # 导入模块并给模块起别名 print(tmath.pi) r = 2 s = tmath.pi * r * r print("圆的面积为:",s) print("==============================") from math import pi as p # 导入模块下的函数,并给函数起别名 print(p) r = 2 s = p * r * r print("圆的面积为:",s) 结果: 3.141592653589793 圆的面积为: 12.566370614359172 ============================== 3.141592653589793 圆的面积为: 12.566370614359172 32.0 ============================== 3.141592653589793 圆的面积为: 12.566370614359172 ============================== 3.141592653589793 圆的面积为: 12.566370614359172 ============================== 3.141592653589793 圆的面积为: 12.566370614359172
3.赋值魔法
x = 1 print(x) print("====同时给多个变量赋值====") x,y,z = 2,3,1 print(x) print(y) print(z) print("====同时给多个变量赋予同样的值====") a=b=c=2 print(a) print(b) print(c) print("====给某个变量赋予多个值====") values=1,2,3 print(values) 结果: 1 ====同时给多个变量赋值==== 2 3 1 ====同时给多个变量赋予同样的值==== 2 2 2 ====给某个变量赋予多个值==== (1, 2, 3)
4.增量赋值
# 增量赋值
num = 2
print(num)
print("=============")
# num = num + 1
num += 1
print(num)
print("=============")
num *=2
print(num)
print("=============")
info = "hello"
info +=" world"
print(info)
info *=2
print(info)
结果:
2
=============
3
=============
6
=============
hello world
hello worldhello world
以上是关于:条件循环的主要内容,如果未能解决你的问题,请参考以下文章