您的位置:首页 > 编程语言 > Python开发

python基础知识(一)运算符-字符串详解

2018-09-19 22:17 861 查看
python基础知识

一:python简单介绍
a.python基础
-基础练习
-基础数据类型
-函数
-面向duix
b.网络编程
c.web编程
d.设计模式与算法
介绍:Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。
学习python课程,从基础知识到web项目开发以及爬虫系统项目代码开发实践课程记录。
二:本次python开发版本3.0+ ,python安装不进行演示,使用开发工具pycharm
三:执行python
变量:
n1 = input("输入变量名")
print(n1)
#变量构成:
字母
数字
下划线
不能使用数字开头,不能使用python关键字

字符串:
name = "hello"
运算符:
+ - * / % **
in not in 判断字符是否包含在其中
n1=9
n2=2
n3=n1+n2
n3=n1-n2
n3=n1*n2
n3=n1/n2
n3=n1%n2
n3=n1**n2
循环:

死循环:
while 1==1:
print("ok")
循环遍历0到9
count=0
while count<10:
print(ok)
count += 1
1、输出1-100内的所以偶数
count=1
while count<=100:
if(count%2!=0):
pass
else :
print(count)
count = count+1
2、求1-2+3-4+5...99的所有数的和
count=1
sum1=0
sum2=0
while count<100:
if(count%2!=0):
sum1=count+sum1
else :
sum2=count+sum2
count = count+1
print(sum1-sum2)
判断:

name = xixihaha
if "xi" not in name:
print("ok")
else :
print("error")
布尔值: 真True 假 False
逻辑运算 : not and or
数据类型:
str:

test="alex"
test.capitalize() 首字母大写,其他小写
test.casefold()   全部小写(牛)
test.lower()      全部小写
test.upper()      全部大写
test.swapcase()   大小写转换
test.islower()    是否全是小写
test.isupper()    是否全是大写
test.center(20,"*")设置宽度,并将内容居中,20代指总长度,* 空白位置填充(一个字符)
test.count("a")   计算字符中出现个数从0开始
test.count("a",5,7) 从第5位开始,到第7位结束
test.endswith("a") 以什么结尾,返回bool值
test.startswith('x')以什么开始,返回bool值
test.find("ex")   从开始到最后,找到第一个后获取其位置,未找到返回-1
test.find("ex",3,7) 从第3位开始,到底7位结束

格式化,将一个字符串的占位符替换为指定的值
test='i am {name},age {age}'
test.format(name='alex',age='20')
格式化,将一个字符串的占位符替换为指定的值
test='i am {0},age {1}'
test.format('alex',10)
格式化,传入的值{"name":"xie","age":23}
test='i am {name},age {age}'
test.format('alex',10)
test.format_map({"name":"xie","age":23})

识别\t ,\n 等标签
test="1234567\t89"
test.expandtabs(2)空白位置数

join插入,将字符串的每一个元素安装指定分隔进行拼接
test="我就是我不一样的烟火"
t=''
t.join(test)   #等同于 "".join(test)
t='_s'
test.join(t)

分隔
test="testegesjuisfsgwsfhwrweydasgstee"
test.partition("s")#只能把字符串分隔为三份
test.rpartition()
test="testegesjuisfsgwsfhwrweydasgstee"
test.split("s")#全部找到进行分隔,无法保留分隔符“s”
test.split("s",1)
test.rsplit()

test="alex" #索引,下标,获取字符串中的某一个字符
test[2]
test[0:2]# 0<= val <2
test[0:-1] #切片

字符串一旦创建,不可以修改
一旦修改或者拼接,都会造成重新生成字符串
#替换
test="alexalexalex"
test.replace("ex","bbb")#全部替换
test.replace("ex","bbb",1)#替换第一个
test.replace("ex","bbb",2)#替换前两个
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python第一课