昨日回顾 python的种类javapyhtoncpythonpypy 编码 !/usr/bin/env python-*- coding:utf-8 -*-print("abc") name = input("name:") input交互内容为字符串 需要数字需要用int()定义 while elsebreak 终止整个循环continue 终止当次循环进入下一次循环pycharm 安装专业版不要汉化汉化无法使用部分功能 运算符 in not inname = "Amanda"if "man" in name: print("yes")else: print("no")if "man" not in name: print("yes1")else: print("no1") 布尔值 :真 True假 Falsename = "Amanda"v = ‘man‘ in nameprint(v)v2 = ‘man‘ not in nameprint(v2) 比较运算==><>=<=!=不等于(推荐)<>不等于notnot True = Falsenot False = True user = "Amanda"pwd = "123" v = user == "Amanda" and pwd == "123" or 1==1 and pwd == "456"print(v)没有优先级,从左到右按顺序运行v = (user == "Amanda" and pwd == "123") or (1==1 and pwd == "456")print(v)推荐使用括号 括号先计算算数运算+ 加+ 减* 乘/ 除** 幂% 取余// 取整赋值运算count = count = 1count += 1count = count - 1count -= 1count = count * 1count *= 1count = count / 1count /= 1count = count ** 1count **= 1count = count % 1count %= 1count = count // 1count //= 1 算数运算和赋值运算都是取得真实结果比较运算 逻辑运算 成员运算 结果是布尔值 #数据类型 数字a = 123b = 456字符串a = "asdas "b = "assfsd"布尔值True 真False 假数字python2 "整形""长整形"python3 intint()u = "123"v = int(u)将字符串转换为数字u2 = "a"v2 = int(u2, base=16)将字符串按16进制转换为数字age = 10r = age.bit_length()计算至少用几位来表示当前数字 字符串 strtest = "amAnda"v = test.capitalize()print (v)Amanda首字母大写v1 = test.casefold()print(v1)amanda转小写功能多v2 = test.lower()print(v2)amanda转小写功能少v3 = test.center(20, "*")print(v3)*******amAnda*******设置总长度 将内容居中 *一个字符填充空白处 默认空格v4 = test.count("a")print(v4)2 字符串中a的个数v5 = test.count("a",5)print(v5)1从第五个字节开始有几个av6 = test.endswith("da")print(v6)True 是否已da结尾v7 = test.startswith("am")print(v7)True 是否以am 开头 test = "AmandaAmanda"v = test.find("a")print(v)2 重头开始找an 返回an在第几位 从0开始 没有找到返回值-1test = "AmandaAmanda"v = test.index("a")2 重头开始找an 返回an在第几位 从0开始 没有找到报错test = "I am {name}, age{age}"v = test.format(name="Amanda", age="21")print(v) 格式化 将字符串中的占位符替换为制定值还有一种test = "I am {0}, age{1}"v = test.format("Amanda", "21")按顺序替换字典格式化test = "I am {name}, age{age}"v = test.format_map({"name":‘Amanda‘, "age":21})test = "sdf123"v = test.isalnum()print(v) 字符串中是否只包含数字和字母# 列表 list元祖 tuple字典 dict 布尔值 bool
时间: 2024-10-12 20:02:29