初识python、基础知识、条件、循环
整数的基础知识
- 除号(/),不管分子分母结果都是浮点数
- 整除(//) ,以分子分母类型为依据
-
5.2//2=2.0 5//2=2
- 取余(%)
- 基础:±*/
-
** 幂运算符
- 大整数:
-
print(2 ** 60) 输出:1152921504606846976
进制转换(注:直接输出为 十进制)
- 十进制
-
0-9 10
-十六进制
-
0-9 A-F 10
- 二进制 0b 八进制 0o 十进制 十六进制 0x (除十进制之外前面都跟0)
- 从其他进制转十进制,适用int,两个参数
参数1 :字符串类型,待转换数
参数2 : 数值类型
int(参数1,参数2)
print(int("0b1100110",2)) #二进制转十进制 102 print(int("0o123124",8)) #8进制转十进制 42580 print(int("0xAA11D",16)) #16进制转十进制 696605
- 进制函数
-
bin 二进制 oct 八进制 hex 十六进制
-
直接输出结果为十进制
格式输出
- format (参数,输出格式)
浮点类型 12.1f 前面12位补空的个数,后面以为小数后保留位数
> < ^ 左右中心对齐
科学计数法 e 或 0.2e
x = 123.1231231
#保留小数点后两位 123.12
print(format(x, '0.2f'))
#保留小数点1位,数字长度12右对齐,左侧保留空格
#' 123.1'
print(format(x,'12.1f'))
#保留小数点1位,数字长度12左对齐,左侧保留空格
#'123.1 '
#'123.1 12'
#'123.10000000'
#'000123.10000'
print(format(x,'<12.1f'))
print(format(x,'<12.1f'),12)
print(format(x,'0<12.1f'))
print(format(x,'0^12.1f'))
#加千分位逗号 '22,123.1'
y = 22123.1231231
print(format(y,',.1f'))
获取用户输入
name = input(输入内容提示)
内建函数、模块提供、自定义
#coding = utf-8 保证中文保存
#内建函数
print(pow(2, 3))# 8
print(abs(-33)) #绝对值 33
print(round(3.7))#取整 4 四舍五入
print(round(3.3))#取整 3
#模块提供
from cmath import sin
print(sin(3.14))
注释
#单行注释 ‘’’ 多行注释 \转义符 + 字符串拼接 sep 分割
print('1','2','3',sep='#')输出:1#2#3
变量赋值
x,y=1,2
x,y=y,x
print(x) #2
print(y) #1
业务判断逻辑
- 布尔(boolean)
#在python中每一种类型都可以被解释成布尔类型 #None 0 “” () [] {}都会解释成false print("None=",bool(None))#None= False print(1==False)#false print("" == False) #false print(bool("")==False)#true print(1==True)#true
- if
#2
if 1==False:
print("1")
else:
print("2")
#0
if 1==False:
print("1")
elif 1==True:
print("0")
else:
print("2")
- 比较运算
print("hello"=="hello")#true
print("Hello"=="hell0")#false
list = [1,2,3,4,5]
print(1 in list) #true
print(1 not in list)#false
x=20
y=30
s1="hello"
s2="world"
#x<y and s1<s2
if x >y:
print("x>y")
elif x<y and s1<s2:
print("x<y and s1<s2")
elif not x>y :
print
断言assertions
x= 20
assert x<10
执行结果:
'''
条件语句加抛出异常
Traceback (most recent call last):
File "D:\devspace\pytest\src\com\py\demo1\demo2.py", line 2, in <module>
assert x<10
AssertionError
'''
while
x=1
while x<=10:
print(x)
x=x+1# 或者x += 1
#控制台输出菱形
line = input("enter:")
line = int(line)
if line % 2 != 0:
maxspacenum = line//2 #最大空格数
i=1
linespace = maxspacenum
#输出上三角
while linespace >= 0:
#输出每行*左侧的空格
print(" " * linespace,end="")
print("*"*(2*i-1))
linespace -= 1
i+=1
#下三角
i -= 2
linespace +=2
while linespace <=maxspacenum:
print(" "*linespace,end="")
print("*"*(2*i-1))
i-=1
linespace +=1
else:
print("行数必须的偶数")
for
- continue
跳出本次循环执行下次循环 - break
跳出整个循环
m = [1,2,3,4,5,6,7]
i = 0
for i in m:
if i == 7:
break
print(m[i])
###输出1 2 3 4 5 6 7
else
#else 只在while和for正常退出时执行
#执行结果:
#1
#执行
#----
#2
#----
x = 0
while x<2:
x+=1
if x ==2:
print("----")
print(x)
print("----")
break
else:
print(x)
print("执行")
exec、eval
#使用exec和eval函数动态执行phthon代码
#exec执行函数
#eval执行表达式
exec('i=10')
exec('i*=2')
exec('print(i)')
##输出20
##############################
'''
执行输入结果:
>>>i=10
>>>print(i)
>>>
10
>>>
'''
codes=""
while True:
code = input(">>>")
if code =="":
exec(codes)
codes=""
continue
codes += code + "\n"
####################
########################
'''
执行结果:
>>>x=20
>>>print(eval('x==20'))
>>>
True
'''
while True:
code = input(">>>")
if code == "exit":
break
exec(code)