python基础五

(1)对象类型


{
   
 "cells": [
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "# 1. Python 核心数据类型  \n",
    "\n",
    "**内置对象**  \n",
    "\n",
    "对象类型|例子 常量/创建\n",
    "---|---\n",
    "数字|1234, 3.1415, 3+4j, 0b111, Decimal(), Fraction()\n",
    "字符串|'spam', \"guido's\", b'a\\xolc', u'sp\\xc4m'\n",
    "列表|[1, [2, 'three'], 4], list(range(10))\n",
    "字典|{'food': 'spam', 'taste': 'yum'}, dict(hours=10)\n",
    "元组|(1, 'spam', 4, 'U'), tuple('spam'), namedtuple\n",
    "文件|open('eggs.txt'), open(r'C:\\ham.bin', 'wb')\n",
    "集合|set('abc'), {'a', 'b', 'c'}\n",
    "其他类型|类型、None、布尔型\n",
    "编程单元类型|函数、模块、类\n",
    "与实现相关的类型|编译的代码,堆栈跟踪"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "# 2. 变量"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
   },
   "outputs": [
    {
   
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "message = \"Hello World\"\n",
    "print(message)"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "这里message为一个变量,与字符串“Hello World”关联在一起。\n",
    "- 变量名只能包含字母、数字和下划线,变量名可以字母或下划线开头,但不能以数字开头。\n",
    "- 变量名不能包含空格,但可以使用下划线分隔其中单词。\n",
    "- 不能将Python关键字和函数名用作函数名。\n",
    "- 变量名应既简短又具有描述性。\n",
    "- 慎用小写字母l和大写字母O。  \n",
    "\n",
    "\n",
    "\n",
    "Python中有三个主要类型(以及操作)的分类:  \n",
    "- 数字(整数、浮点数、二进制、分数等)  \n",
    "    支持加法和乘法等。  \n",
    "- 序列(字符串、列表、元组)  \n",
    "    支持索引、分片和合并等。  \n",
    "- 映射(字典)  \n",
    "    支持通过键的索引等。  \n",
    "\n",
    "Python中主要核心类型划分为如下两类:  \n",
    "- **不可变类型**:数字、字符串、元组、不可变集合  \n",
    "- **可变类型**:列表、字典、可变集合"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
   
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
   
  "kernelspec": {
   
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   
   "codemirror_mode": {
   
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

(2)python基础 数字类型




{
   
 "cells": [
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "# 1. Python 的数字类型\n",
    "Python 数字类型的完整工具包括:\n",
    "- 整数和浮点数\n",
    "- 复数\n",
    "- 固定精度的十进制数\n",
    "- 有理分数\n",
    "- 集合\n",
    "- 布尔类型\n",
    "- 无穷的整数精度\n",
    "- 各种数字内置函数和模块"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "## 1.1 数字常量(Literals)\n",
    "**基本数字常量**\n",
    "\n",
    "常量|解释\n",
    "---|---\n",
    "1234, -24, 0, 99999999|整数(无穷大小)\n",
    "1.23, 1., 3.14e-10, 4E210, 4.0e+210|浮点数\n",
    "0o177, 0x9ff, 0b101010|八进制、十六进制和二进制常量\n",
    "3+4j, 3.0+4.0j, 3j|复数常量\n",
    "set('spam'), {1, 2, 3, 4}|集合\n",
    "Decimal('1.0'), Fraction(1, 3)|小数和分数扩展类型\n",
    "bool(X), True, False|布尔类型和常数"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "## 1.2 Python 表达式操作符\n",
    "**Python 表达式操作符和程序**\n",
    "\n",
    "操作符|描述\n",
    "---|---\n",
    "yield x|生成器函数发送协议\n",
    "lambda args: expression|生成匿名函数\n",
    "x if y else z|三元选择表达式\n",
    "x or y|逻辑或(只有 x 为假,才会计算 y)\n",
    "x and y|逻辑与(只有 x 为真,才会计算 y)\n",
    "not x|逻辑非\n",
    "x in y, x not in y|成员关系(可迭代对象、集合)\n",
    "x is y, x is not y|对象实体测试\n",
    "x < y, x <= y, x > y, x >= y|大小比较,集合子集和超集\n",
    "x == y, x != y|值相等性操作符\n",
    "x \\| y|位或,集合并集\n",
    "x ^ y|位异或,集合对称差\n",
    "x & y|位与,集合交集\n",
    "x << y, x >> y|左移或右移 y 位\n",
    "x + y|加法,合并\n",
    "x - y|减法,集合差集\n",
    "x * y|乘法,重复\n",
    "x % y|余数,格式化\n",
    "x / y, x // y|除法:真除法或 floor 除法\n",
    "-x, +x|一元减法,识别\n",
    "~x|按位求补(取反)\n",
    "x ** y|幂运算\n",
    "x[i]|索引(序列、映射及其他)\n",
    "x[i:j:k]|分片\n",
    "x(...)|调用(函数、方法、类及其他可调用的)\n",
    "x.attr|属性引用\n",
    "(...)|元组,表达式,生成器表达式\n",
    "[...]|列表,列表解析\n",
    "{...}|字典、集合、集合和字典解析\n",
    "\n",
    "上表中操作符越靠后的优先级越高。  \n",
    "\n",
    "在混合类型的表达式中,Python 首先将被操作的对象转换成其中最复杂的操作对象的类型,然后再对相同类型的操作对象进行数学运算。整数比浮点数简单,浮点数比复数简单。  "
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "# 2. 在实际应用中的数字\n",
    "## 2.1 变量和基本的表达式\n",
    "\n",
    "在Python中:  \n",
    "- 变量在它第一次赋值时创建。\n",
    "- 变量在表达式中使用将被替换为它们的值。\n",
    "- 变量在表达式中使用以前必须已赋值。\n",
    "- 变量像对象一样不需要在一开始进行声明。\n",
    "\n",
    "在 Python 中,变量并不需要预先声明,但是在使用之前,至少要赋一次值。"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
   },
   "outputs": [],
   "source": [
    "a = 3             # 赋值会让变量 a 和 b 自动生成\n",
    "b = 4"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "(4, 12)"
      ]
     },
     "execution_count": 12,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + 1, b * 3"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "## 2.2 比较:一般的和连续的"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 < 2 "
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2.0 >= 1     # 大于等于:混合类型 1 转换为 1.0"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2.0 == 2.0"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
   
    "scrolled": true
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2.0 != 2.0"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "Python 允许我们把多个比较连续起来执行范围测试:"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = 2              \n",
    "Y = 4\n",
    "Z = 6\n",
    "X < Y < Z           # 连续比较,相当于 X < Y and Y < Z"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
   
    "scrolled": true
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X < Y > Z           # 相当于 X < Y and Y > Z"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "## 2.3 除法:Floor 除法和真除法\n",
    "\n",
    "**X / Y**  \n",
    "真除法,无论任何类型都会保持小数部分。  \n",
    "**X // Y**  \n",
    "Floor 除法,不考虑操作对象的类型,总会省略掉结果的小数部分。  "
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
   
    "scrolled": true
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 18,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 / 4"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 // 4"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
   
    "scrolled": false
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 20,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 // 4.0"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       "-3"
      ]
     },
     "execution_count": 22,
     "metadata": {
   },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 // -2      # 向下舍入"
   ]
  },
  {
   
   "cell_type": "markdown",
   "metadata": {
   },
   "source": [
    "## 2.4 复数"
   ]
  },
  {
   
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
   },
   "outputs": [
    {
   
     "data": {
   
      "text/plain": [
       
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值