print("hello word")
hello word
age=18
h=0xAB#16进制
b=0b0011#2进制
print(h)
print(b)
171
3
print(11+2)
print(11*2)
print(11/2)#浮点数
print(11//2)#留整数
print(11%2)#整除留余数
print(11**3)#11三次方
print(11//2.0)#有小数点结果为浮点数
13
22
5.5
5
1
1331
5.0
#数形转换
print(int(3.12))
print(float(2))
3
2.0
#type会打印出数据类型
name='tom'
print(name)
print("abc type={0}".format(type(name)))
print("abc type={0}".format(name))
print(type(23))
print(type(3.12))
print(type("hello"))
print(type(None))
tom
abc type=<class 'str'>
abc type=tom
<class 'int'>
<class 'float'>
<class 'str'>
<class 'NoneType'>
name="tom"
myName="jerry"
#变量命名首字母不能为数字或特殊符号
#布尔类型
istodaymonday=False
mondaygoclass=True
if istodaymonday and mondaygoclass:
print("上课")
else:
print("不上")
不上
x=5
x+=2
#相当于x=x+2
print(x)
7
a=10
b=20
print(a>b)
#真ture假flase
False
#逻辑运算符
a=1
b=12
print(a and b)#a假反a,否者反b
print(a or b)#a真反a,否则反b
print(not a)#not 与a变为相反之 反ture或flase
12
1
False
item=2
itemList=[1,2,3,4]
print(item in itemList)
#成员运算符,判断是否包含某个元素
True
n=13
if n%2==0:
print("偶数")
else:
print("奇数")
奇数
a=13
b=12
if a<b:
print("a<b")
elif a==b:
print("a==b")
else :
print("a>b")
a>b
t=0
i=1
while i<5:
t=t+i
i+=1
print(t)
1
3
6
10
total=0
for i in range(10):
print("a={0}".format(i))
total=total+(i+1)
print(total)
a=0
a=1
a=2
a=3
a=4
a=5
a=6
a=7
a=8
a=9
55
t=0
#range(start,end,step)
for i in range(2,20+2,2):
t=t+1
print(t)
10
#数值列表求和
mylist=[1,2,3,4,5,6]
total=0
for i in range(len(mylist)):
item=mylist[i]
print("i={0},item={1}".format(i,item))
i=0,item=1
i=1,item=2
i=2,item=3
i=3,item=4
i=4,item=5
i=5,item=6
mylist=[3,4,5,6]
result=[]
for item in mylist:
newitem=item*2
result.append(newitem)
print(result)
#该进
result=[item*2 for item in mylist]
print(result)
[6, 8, 10, 12]
[6, 8, 10, 12]
#\开为转义字符
# 例如 \续行符 \\反斜杠 \'单引号 \" 双引号 \n换行 \t 横向制表图 \r回车
#格式化输出
name="张三"
age=20
te="用户名{}的年龄是{}".format(name,age)
print(te)
ta=f"用户名{name}的年龄是{age}"
print(ta)
用户名张三的年龄是20
用户名张三的年龄是20
#是否包含字符
tex="hello word"
sub="lo"
if sub in tex:
print("包含字串")
#字符串拼接
t="this is our lia"+": good"
print(t)
print('-'*len(t))
#是否包含字符
tex="hello word"
sub="lo"
if sub in tex:
print("包含字串")
#字符串拼接
t="this is our lia"+": good"
print(t)
print('-'*len(t))
包含字串
this is our lia: good
---------------------
.
#查找字符串下表,若无则反-1
print ('do it now'.find('it'))
print ('do it now'.find('ab'))
print ('do it now'.find('it',1,5))#在string(startIndex,sstopIndex)内查照
3
-1
3
#str.join(sep)将sep序列中的字符串用str拼接起来
dirs=('','home','python','code')
path='/'.join(dirs)
print(path)
#这里将,换成/
/home/python/code
#替换字符
name="hello word hehe"
newName=name.replace('he','HE')
print(newName)
name="hello word hehe"
newName=name.replace('he','HE',2)
print(newName)
HEllo word HEHE
HEllo word HEhe
#删除
name='---do it--now--'
sname=name.strip('-')#strip可以删除规定的头尾字符,默认删除头尾的空格和换行等
print(sname)
do it--now
mystring="hello word"
for char in mystring:
print (char)
h
e
l
l
o
w
o
r
d
TOM
#换大小写
name="tom"
print (name.upper())
name="TOM"
print (name.lower())
TOM
tom
python 基础知识(2022 8.15)
最新推荐文章于 2024-05-09 20:43:47 发布