python基础--表达式和语句,不可改变的数据类型:数字,字符串,元组
目录:
一、python中的表达式
二、python中的语句
三、python中字符串的用法
一、python中的表达式
运算操作符:x + y, x - y, x * y, x / y, x // y, x % y 逻辑运算:x and y, x or y, not x 成员关系运算:x in y, x not in y 对象实例测试:x is y, x not is y 比较运算:x < y, x > y, x <= y, x >= y, x == y, x != y 位运算:x & y, x | y, x ^ y, x << y, x >> y 一元运算:-x, +x, ~x 幂运算:x ** y 索引和分片:x[i], x[i:j], x[i:j:stride] 调用:fun(...) 取属性:x.attribute 元组tuple:(x,y,z) 列表list:[x,y,z] 集合set:{x,y,z} 字典dict:{key1:value1, key2:value2} 三元选择表达式:x if y else z 匿名函数:lambda args: expression 生成器函数发送协议:yield x
二、python中的语句
赋值语句 调用 print:打印对象 if/elif/else:条件判断 for/else:序列迭代 while/else:普通迭代 pass:占位符 break continue def return yield global:命名空间 raise:触发异常 import from:模块属性访问 class try/exception/finally:捕捉异常 del:删除引用 assert:调试检查 with/as:环境管理器
三、python中字符串的用法
Python有五个标准的数据类型:
  Numbers(数字)String(字符串)List(列表)Tuple(元组) Dictionary(字典)
不可改变的数据类型:数字,字符串,元组
可变的数据类型:列表,字典
python3支持Unicode,可以表示世界上任何书面语言的字符。python3的字符默认就是16位的Unicode编码。使用内置函数ord()可以把字符转换成对应的Unicode码,使用内置函数chr()可以把十进制数字转换成对应的字符。
1、python3.x的input():
将键盘输入的内容(字符串)赋给变量a
a = input("输入一个数字:") print(a) print(type(a)) # 打印变量a的类型 b = 2 if int(a) > 1: #print("a的值为%s"%a) print("a的值为%s,b的值为%s"%(a, b))
2、字符串与数值类型的转换
a = input("输入一个字符串:") print("变量a的值为:%s"%a) print(type(a)) # 打印变量a的类型 a = int(a) # 字符串 => int print(type(a)) a = str(a) # int => 字符串 print(type(a))
3、len(字符串变量): 字符串长度
a = input("输入一个数字:") print("变量a的值为:%s"%a) print("变量a的长度为:%s"%len(a))
4、字符串的用法
a = "hello" b = ' world' print(a + b) # 字符串拼接 print("a" * 10) # 打印10个a c = "a+b=%s"%(a+b) print(c)
字符串下标:从0开始
a = "hello" for s in a: print(s) print("=" * 50) i = 0 while i < len(a): print("字符串a的第%d个字符为:%s"%(i, a[i])) i += 1
字符串切片
a = "hello" print(a[0:3]) # 结果:hel.包左不包右,索引范围[0,2] print(a[0:-2]) # 结果:hel.包左不包右, 从下标0开始,直到倒数第2个(不包含) print(a[0:len(a)]) # 相对于print(a[0:]) print(a[0:-1:2]) # 起始位置:终止位置:步长
字符串倒序
a = "hello" # 字符串倒序 print(a[len(a)::-1]) print(a[-1::-1]) print(a[::-1])
5、字符串常见操作
字符串方法

a = "helloworldhello" # find print(a.find("world")) # 第一次找到"world"的索引 print(a.rfind("hello")) # 从右边开始搜索,第一次找到"hello"的索引 print(a.find("haha")) # 找不到,结果:-1 # index print(a.index("hello")) print(a.rindex("hello")) #print(a.index("haha")) # index与find的区别:find找不到结果为-1,index找不到抛异常 #print(a.rindex("haha")) # count(str, start=0, end = len(mystr)):返回str在start和end之间mystr里面出现的次数 print(a.count("hello")) # replace:把mystr中的str1替换成str,指定了count,则替换不超过count次 # mystr.replace(str1, str2, mystr.count(str)), 原来的字符串不变 b = a.replace("hello", "HELLO", 2) print("a的值为%s, b的值为%s"%(a,b)) # a的值为helloworldhello, b的值为HELLOworldHELLO # split mystr = "hello world hello python" array = mystr.split(" ") print("mystr的值%s"%mystr) # 原来的字符串不变 for str in array: print(str) # capitalize:把字符串的第一个字符大写 # title:把字符串的每个单词首字母大写 print(a.title()) #Helloworldhello print(mystr.title()) #Hello World Hello Python # startswith/endswith print(a.startswith("hello")) # True print(a.startswith("world")) # False # lower/upper:原来的字符串不变 print(a.upper()) # HELLOWORLDHELLO
mystr = " hello python " #print(mystr.center(20)) print(mystr.strip()) # 去除前后空格 # string.isalnum():如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False # string.isalpha():如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False # string.isdigit():如果 string 只包含数字则返回 True 否则返回 False. # join strArray = ["aaa", "bbb", "ccc"] regex = ", " str = regex.join(strArray) print(str)
posted on 2019-10-07 20:38 wenbin_ouyang 阅读(229) 评论(0) 收藏 举报
                    
                
                
            
        
浙公网安备 33010602011771号