网时代it培训官网,东莞搜索seo关键词,怎样进网站ftp,dede旅游网站程序设计入门—Python 对象和类型 五种基本对象类型
字符串 #xff08;string#xff09;#xff0c;简记为 str 使用 ’ ’ 或 ” ” 括起来的一系列字符 整数#xff08;integer#xff09;#xff0c;简记为 int 十进制#xff1a;21#xff0c;八进制#xf…程序设计入门—Python 对象和类型 五种基本对象类型
字符串 string简记为 str 使用 ’ ’ 或 ” ” 括起来的一系列字符 整数integer简记为 int 十进制21八进制025十六进制0x15 浮点数float 1.4821.021..212.1E2 布尔数boolean简记为 bool TrueFalse 复数complex 11j
对象类型
type(小明)-- type str
type(男)-- type str
type(15)-- type int
type(1.48)-- type float
type(43.2)-- type float
type(江苏)-- type str
为什么区分对象类型
不同类型对象运算规则不同 如整数的加法和字符串的加法含义不同 不同类型对象在计算机内表示方式不同 5 – 101’5’– 1001101
为何区分整数与浮点数 浮点数表示能力更强浮点数有精度损失1.1 1.1 1.1 3.300000000003
CPU有专门的浮点数运算部件
强制类型转换
int(123) -- 123
str(123) -- 123
float(123) -- 123.0
float(123) -- 123.0
bool(123) -- True
bool(0) -- False
以下不能 转换
a_str test
a_num int(a_str)
#必须是字符中可转换的时候才能转换 运算符 算术运算符Arithmetic Operators 2/3 0
3.0/60.5
Python 2 中“/”表示向下取整除floor division 两个整数相除结果也是整数舍去小数部分 如果有一个数为浮点数则结果为浮点数
自动类型转换
若参与运算的两个对象的类型同则结果类型不变 如1 / 2 0 若参与运算的两个对象的类型不同则按照以下规则进行自动类型转换 bool – int – float – complex
1.0 3 4.0
True 3.0 4.0
求余运算符
求余运算符% 如10 % 3 1 应用 若今天是星期六则10天后是星期几 (6 10) % 7 2 判断一个数 x 是否为偶数 x % 2 是否等于 0
math 模块 模块module 实现一定的功能的 Python 脚本集合
引入模块 import module_name
math模块
import math
查看模块内容
dir(math)
查看帮助 “ help(math.sin) dir(math) [‘doc‘, ‘file‘, ‘name‘, ‘package‘, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘hypot’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘modf’, ‘pi’, ‘pow’, ‘radians’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘trunc’] “ 关系运算符Relational Operators 判断一个数 x 是否为偶数 x % 2 是否等于 0 x % 2 0 若为True则 x 为偶数 若为False则 x 为奇数 用于判断两个值的关系 大小、相等或不相等 运算的结果只有两种布尔型 若结果为True表示条件成立 若结果为False表示条件不成立 逻辑运算符Logical Operators
运算符优先级
看看下面两个表达式 2 * 1 3 先乘后加 2 * (13) 先加后乘
括号 改变了语言内在的默认优先级 具有最高优先级 嵌套括号按照由内而外结合 (2 * (1 2))**2 36 2 * (1 2)**2 18 变量 程序控制结构 程序流程图 判断语句 示例 循环结构 While循环结构 break continue 语句 for循环结构 for n in range(1, 100): while n !1: if n%2 0: n /2 else: n 3*n 1 print n, print n, 表示是打印在同一行中。 print format 右对齐 While VS for 循环 函数 缺省参数 有返回值函数 局部变量和全局变量 global def is_leap_year(year): if year %4 0 and year %100 !0 or year % 400 0: return True else: return False def get_num_of_days_in_month(year, month): if month in (1, 3, 5, 7, 8, 10, 12): return 31 elif month in (4, 6, 9, 11): return 30 elif is_leap_year(year): return 29 else: return 28 def get_total_num_of_day(year, month): days 0 for y in range(1800, year): days 366 else: days 365 for m in range(1, month): days get_num_of_days_in_month(year, m) return days def get_start_day(year, month): return 3 get_total_num_of_days(year, month) %7 print get_start_day(2033, 12) 递归函数 字符串 字符串是不可变的 示例 人名游戏 字符串比较 字符串格式化 正则表达式 列表 my_list[2:]#取从2到最后的值。 列表赋值 python中list与array互相转换 u array([[1,2],[3,4]]) m u.tolist() #转换为list m.remove(m[0]) #移除m[0] m np.array(m) #转换为array 查找 时间复杂度 二分法 排序 内建排序函数 嵌套列表 元组 DSU模式DecorateSort and UndecorateDSU模式 字典的生成 dict() 作用dict() 函数用于创建一个字典。返回一个字典。 语法 class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) 参数说明 **kwargs – 关键字 mapping – 元素的容器。 iterable – 可迭代对象 dict() # 创建空字典 {} dict(a’a’, b’b’, t’t’) # 传入关键字 {‘a’: ‘a’, ‘b’: ‘b’, ‘t’: ‘t’} dict(zip([‘one’, ‘two’, ‘three’], [1, 2, 3])) # 映射函数方式来构造字典 {‘three’: 3, ‘two’: 2, ‘one’: 1} dict([(‘one’, 1), (‘two’, 2), (‘three’, 3)]) # 可迭代对象方式来构造字典 {‘three’: 3, ‘two’: 2, ‘one’: 1} 删除字典元素 a {a: 1, b: 2}
for k,v in a.items(): if k b: del a[k]
print a
# {a: 1} a {“a”: “1”, “b”: “2”} f filter(lambda i: i[0] ! “b” , a.items()) print f [(‘a’, ‘1’)] print a {‘a’: ‘1’, ‘b’: ‘2’}
del dict2[‘name’]#删除键为“name”的条目。 dict2.clear()#删除 dict2 中所有的条目 del dict2#删除整个 dict2 字典 dict2.pop(‘name’)#删除并返回键为“name”的条目