本文是笔者在学习MOOC课程《Python语言基础与应用》 (北京大学-陈斌)中根据上机课时的要求写下在代码

课程总链接:

中国大学MOOC

B站

本节课链接

数值基本运算: 33和7
+, -, *, /, //, %, **
hex(), oct(), bin()

 Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 33+7
40
>>> 33-7
26
>>> 33*7
231
>>> 33/7
4.714285714285714
>>> 33//7
4
>>> 33%7
5
>>> 33**7
42618442977
>>> 7**33
7730993719707444524137094407
>>> 33**33
129110040087761027839616029934664535539337183380513
>>> hex(33)
'0x21'
>>> hex(7)
'0x7'
>>> oct(7)
'0o7'
>>> oct(33)
'0o41'
>>> bin(33)
'0b100001'
>>> bin(7)
'0b111'

类型转换
1, 0, 'abc', None, 1.2, False, ''
str(), bool(), int(), float()
is None, ==, !=

 >>> str(1)
''
>>> str(0)
''
>>> bool(1)
True
>>> bool(0)
False
>>> bool('abc')
True
>>> int('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
>>> int('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
>>> float('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'abc'
>>> float(1)
1.0
>>> str(None)
'None'
>>> int(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
>>> int('None')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'None'
>>> int(1.2)
1
>>> int(False)
0
>>> int(True)
1
>>> float('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
>>> bool('')
False
>>> 1 is None
False
>>> 0 is None
False
>>> '' is None
False
>>> 1==1.2
False
>>> False is None
False
>>> True is None
False

字符串基本操作
+, *, len(), [], in
ord(), chr()
含有中文的字符串

 >>> a='Congratulations'
>>> b='misunderstandings'
>>> a+b
'Congratulationsmisunderstandings'
>>> a+' '+b
'Congratulations misunderstandings'
>>> len(a)
15
>>> len(b)
17
>>> c in a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined
>>> 'c' in a
False
>>> 's' in b
True
>>> 'C' in a
True
>>> [a]
['Congratulations']
>>> ord('a')
97
>>> chr(86)
'V'
>>> ord(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 15 found
>>> c='你好'
>>> d='国'
>>> len(c)
2
>>> len(d)
1
>>> ord(d)
22269
>>> chr(83475)
'

Python语言基础与应用 (P16)上机练习:基本数据类型的更多相关文章

  1. Python语言基础与应用 (P23)上机练习:容器类型操作(未完待续)

    上机练习:容器类型操作〉 列表.元组基本操作+, *, len(), [], in Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 ...

  2. 零基础学Python--------第2章 Python语言基础

    第2章  Python语言基础 2.1 Python语法特点 2.11注释 在Python中,通常包括3种类型的注释,分别是单行注释.多行注释和中文编码声明注释. 1.单行注释 在Python中,使用 ...

  3. ArcPy开发教程1-面向ArcGIS的Python语言基础

    ArcPy开发教程1-面向ArcGIS的Python语言基础 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 第一节课 时间2019年2月26日 上午第一节 讲解:A ...

  4. 2.3 Python语言基础

    2.3 Python语言基础 1 语言语义(Language Semantics) 缩进,而不是括号 Python使用空格(tabs or spaces)来组织代码结构,而不是像R,C++,Java那 ...

  5. Python 语言基础

    Python 语言基础 Python 开发环境 计算机组成 编程语言(计算机语言)是人们为了控制计算机,而设计的一种符号和文字的组合,从而实现向计算机发出指令. 形式是符号和文字的组合 目的是为了控制 ...

  6. Python语言基础-语法特点、保留字与标识符、变量、基本数据类型、运算符、基本输入输出、Python2.X与Python3.X区别

    Python语言基础 1.Python语法特点 注释: 单行注释:# #注释单行注释分为两种情况,例:第一种#用于计算bim数值bim=weight/(height*height)第二种:bim=we ...

  7. [Python学习笔记1]Python语言基础 数学运算符 字符串 列表

    这个系列是我在学习Python语言的过程中记录的笔记,主要是一些知识点汇总,而非学习教程,可供有一定编程基础者参考.文中偏见和不足难以避免,仅供参考,欢迎批评指正. 本系列笔记主要参考文献是官网文档: ...

  8. python(一):python语言基础

    一.python语言基本的8个要素 Python语言的8个要素:数据类型.对象引用.组合数据类型.逻辑操作符.运算操作符.控制流语句.输入/输出.函数的创建与引用.除此之外还有一个非常重要且无处不在的 ...

  9. 【Python笔记】Python语言基础

    Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...

随机推荐

  1. 内存寻址能力与CPU的位宽有关系吗?

    答案是:没有关系.CPU的寻址能力与它的地址总线位宽有关,而我们通常说的CPU位宽指的是数据总线位宽,它和地址总线位宽半毛钱关系也没有,自然也与寻址能力无关. 简单的说,CPU位宽指的是一个时钟周期内 ...

  2. REST接口

    全名是Representational State Transfer REST是设计风格而不是标准 建议将JSON格式作为标准响应格式 -------------------------------- ...

  3. 七十四、SAP中内表的4中删除方法

    一.代码如下 二.效果如下 *&---------------------------------------------------------------------* *& Re ...

  4. mysql多表连接查询

    新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键.) ...

  5. PhotoView 实现与图片进行简单的交互

    本文的category是根据VIPhotoView来做参考,在此基础上添加个加载网络图片. 此category主要功能是与图片进行交互,双击放大图片,捏合等操作. 感谢vitoziv ! VIPhot ...

  6. C++ do while无限循环~

    #include<iostream> using namespace std; #include<Windows.h> int main() { ; ; system(&quo ...

  7. skLearn 支持向量机

    ## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...

  8. elasticsearch下载与安装

    目录 安装之前 下载 安装 测试 安装之前 必须注意的是:安装路径不允许有中文及空格和非法字符,尤其是中文 下载 打开elasticsearch官网.选择免费试用. 选择对应产品与版本(选择6.5.4 ...

  9. SHELL学习笔记三

    SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...

  10. 51nod 1022 石子归并 环形+四边形优化

    1022 石子归并 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 N堆石子摆成一个环.现要将石子有次序地合并成一堆.规定每次只能选相邻的2 ...