python开发IDE:
pycharm、eclipse
# 专业版
# 不要汉化
pycharm: 选中后,ctrl+?,整体注释
布尔值:
真 True 假 False
if True:
pass
while True:
pass
1、运算符
结果是值
算数运算
a = 10 * 10
赋值运算 (+=,-=, *=,/=,**=,//=,%=)
a = a + 1 等于 a+=1
结果是布尔值 (有括号先计算括号里的,否则按顺序从前到后执行,先算and再算or)
比较运算 (==; >; <; <=; >=; != or <>; )
a = 1 > 5
逻辑运算 (and, or, not取反)
a = 1>6 or 1==1
成员运算 (判断某个东西是否在某个东西里面包含)
a = "蚊" in "郑建文"
2、基本数据类型
a1 = 123
a1 = 456
数字 int ,所有的功能,都放在int里
Python3:int(整形)可以无穷大
Python2:超出范围为long(长整形)
- int
将字符串转换为数字 (base:默认为10进制,其他进制需要说明)type:类型
a = "123"
print(type(a),a)
b = int(a)
print(type(b),b)
num = "0011"
v = int(num, base=16)
print(v)
- bit_lenghth
# 当前数字的二进制,至少用n位表示
r = age.bit_length()
字符串 str
s1 = "asdf"
s2 = "asdffas"
# 首字母大写
# test = "aLex"
# v = test.capitalize()
# print(v)
# 所有变小写,casefold更牛逼,很多未知的对相应变小写 (Upper,大写)
# v1 = test.casefold()
# print(v1)
# v2 = test.lower()
# print(v2)
# 设置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
# v = test.center(20,"中")
# print(v)
# 去字符串中寻找,寻找子序列的出现次数
# test = "aLexalexr"
# v = test.count('ex')
# print(v)
# test = "aLexalexr"
# v = test.count('ex',5,6)
# print(v)
# 欠
# encode
# decode
# 以什么什么结尾# 以什么什么开始
# test = "alex"
# v = test.endswith('ex')
# v = test.startswith('ex')
# print(v)
# 欠
# test = "12345678 9"
# v = test.expandtabs(6)
# print(v,len(v))
# 从开始往后找,找到第一个之后,获取其位置
# > 或 >=
# test = "alexalex"
# 未找到 -1
# v = test.find('ex')
# print(v)
# index找不到,报错 (忽略,不如find好用)
# test = "alexalex"
# v = test.index('8')
# print(v)
# 格式化,将一个字符串中的占位符替换为指定的值
# test = 'i am {name}, age {a}'
# print(test)
# v = test.format(name='alex',a=19)
# print(v)
# test = 'i am {0}, age {1}'
# print(test)
# v = test.format('alex',19)
# print(v)
# 格式化,传入的值 {"name": 'alex', "a": 19}
# test = 'i am {name}, age {a}'
# v1 = test.format(name='df',a=10)
# v2 = test.format_map({"name": 'alex', "a": 19})
# 字符串中是否只包含 字母和数字
# test = "123"
# v = test.isalnum()
# print(v)
列表 list
...
元祖 tuple
...
字典 dict
...
布尔值 bool
...
#!/usr/bin/env python # -*- coding:utf8 -*- a=10*10 a+=1 a*=2 a/=3 a%=3 print(type(a)) a='e' not in 'abcde' b='123' a=int(b) print(type(a),a) print(type(b),b) b='123.445' a=float(b) print(type(a),a) print(type(b),b) test="alex" v=test.capitalize() print(v) v=v.lower() print(v) v=v.upper() print(v) v=v.casefold() print(v) v=v.center(20,'*') print(v) v=v.count('*',10,15) print(v) v=test.endswith('a') print(test) v=test.startswith('a') print(v) v=test.find('e') print(v) v=test.index('e') print(v) test='I am {0}, i came from {1}' v=test.format('Sundance',"Shandong") print(v) test='I am {name}, i came from {province}' v=test.format(name='Sundance',province="Shandong") print(v) v=test.format_map({"name":'Sundance',"province":"Shandong"}) print(v) test='abcd34' v=test.isalnum() print(v)