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

课程总链接:

中国大学MOOC

B站

本节课链接

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

  1. Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
  2. Type "help", "copyright", "credits" or "license" for more information.
  3. >>> 33+7
  4. 40
  5. >>> 33-7
  6. 26
  7. >>> 33*7
  8. 231
  9. >>> 33/7
  10. 4.714285714285714
  11. >>> 33//7
  12. 4
  13. >>> 33%7
  14. 5
  15. >>> 33**7
  16. 42618442977
  17. >>> 7**33
  18. 7730993719707444524137094407
  19. >>> 33**33
  20. 129110040087761027839616029934664535539337183380513
  21. >>> hex(33)
  22. '0x21'
  23. >>> hex(7)
  24. '0x7'
  25. >>> oct(7)
  26. '0o7'
  27. >>> oct(33)
  28. '0o41'
  29. >>> bin(33)
  30. '0b100001'
  31. >>> bin(7)
  32. '0b111'

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

  1. >>> str(1)
  2. ''
  3. >>> str(0)
  4. ''
  5. >>> bool(1)
  6. True
  7. >>> bool(0)
  8. False
  9. >>> bool('abc')
  10. True
  11. >>> int('abc')
  12. Traceback (most recent call last):
  13. File "<stdin>", line 1, in <module>
  14. ValueError: invalid literal for int() with base 10: 'abc'
  15. >>> int('a')
  16. Traceback (most recent call last):
  17. File "<stdin>", line 1, in <module>
  18. ValueError: invalid literal for int() with base 10: 'a'
  19. >>> float('abc')
  20. Traceback (most recent call last):
  21. File "<stdin>", line 1, in <module>
  22. ValueError: could not convert string to float: 'abc'
  23. >>> float(1)
  24. 1.0
  25. >>> str(None)
  26. 'None'
  27. >>> int(None)
  28. Traceback (most recent call last):
  29. File "<stdin>", line 1, in <module>
  30. TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
  31. >>> int('None')
  32. Traceback (most recent call last):
  33. File "<stdin>", line 1, in <module>
  34. ValueError: invalid literal for int() with base 10: 'None'
  35. >>> int(1.2)
  36. 1
  37. >>> int(False)
  38. 0
  39. >>> int(True)
  40. 1
  41. >>> float('')
  42. Traceback (most recent call last):
  43. File "<stdin>", line 1, in <module>
  44. ValueError: could not convert string to float:
  45. >>> bool('')
  46. False
  47. >>> 1 is None
  48. False
  49. >>> 0 is None
  50. False
  51. >>> '' is None
  52. False
  53. >>> 1==1.2
  54. False
  55. >>> False is None
  56. False
  57. >>> True is None
  58. False

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

  1. >>> a='Congratulations'
  2. >>> b='misunderstandings'
  3. >>> a+b
  4. 'Congratulationsmisunderstandings'
  5. >>> a+' '+b
  6. 'Congratulations misunderstandings'
  7. >>> len(a)
  8. 15
  9. >>> len(b)
  10. 17
  11. >>> c in a
  12. Traceback (most recent call last):
  13. File "<stdin>", line 1, in <module>
  14. NameError: name 'c' is not defined
  15. >>> 'c' in a
  16. False
  17. >>> 's' in b
  18. True
  19. >>> 'C' in a
  20. True
  21. >>> [a]
  22. ['Congratulations']
  23. >>> ord('a')
  24. 97
  25. >>> chr(86)
  26. 'V'
  27. >>> ord(a)
  28. Traceback (most recent call last):
  29. File "<stdin>", line 1, in <module>
  30. TypeError: ord() expected a character, but string of length 15 found
  31. >>> c='你好'
  32. >>> d='国'
  33. >>> len(c)
  34. 2
  35. >>> len(d)
  36. 1
  37. >>> ord(d)
  38. 22269
  39. >>> chr(83475)
  40. '
  41. Python语言基础与应用 (P16)上机练习:基本数据类型的更多相关文章

      1. Python语言基础与应用 (P23)上机练习:容器类型操作(未完待续)
      1. 上机练习:容器类型操作〉 列表.元组基本操作+, *, len(), [], in Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 ...

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

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

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

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

      1. Python语言基础-语法特点、保留字与标识符、变量、基本数据类型、运算符、基本输入输出、Python2.X与Python3.X区别
      1. Python语言基础 1.Python语法特点 注释: 单行注释:# #注释单行注释分为两种情况,例:第一种#用于计算bim数值bim=weight/(height*height)第二种:bim=we ...

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

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

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

    1.  
    2. 随机推荐

        1. 十九、React UI框架Antd(Ant Design)的使用——及react Antd的使用 button组件 Icon组件 Layout组件 DatePicker日期组件
        1. 一.Antd(Ant Design)的使用:引入全部Css样式 1.1 antd官网: https://ant.design/docs/react/introduce-cn 1.2 React中使用A ...

        1. 吴裕雄--天生自然java开发常用类库学习笔记:Arrays
        1. import java.util.* ; public class ArraysDemo{ public static void main(String arg[]){ int temp[] = {3 ...

        1. nginx安全参数
        1. Strict-Transport-Security Strict-Transport-Security,简称 HSTS.该响应头用于标识浏览器用 HTTPS 替代 HTTP 的方式去访问目标站点. 我 ...

        1. UVALive - 7752 Free Figurines
        1. 题意:有n个娃娃,如果大娃娃j直接套小娃娃i,则fa[i] = j.若fa[i] = 0,则该娃娃自由.给出每个娃娃初始的父亲,和改变后的父亲,在满足以下合法操作的条件下,问最少需要多少次变换. 1. ...

        1. GNS3 模拟Arp命令1
        1. R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 end R2: conf t int f0/0 no shutdow ...

        1. [LeetCode] 931. Minimum Falling Path Sum 下降路径最小和
        1. Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

        1. Codeforces Round #616 (Div. 2) 题解
        1. A. Even But Not Even 题意: 定义一个数所有位置的和为偶数它本身不为偶数的数为ebne,现在给你一个数字字符串,你可以删除任意位置上的数字使其变为ebne输出任意改变后的结果,如果 ...

        1. JavaWeb学习记录
        1. 服务器端跳转(请求重定向): 1.jsp内跳转 : <jsp:forward page="page_scope_03.jsp"/> 客户端跳转(请求转发): 1.通过超 ...

        1. 禁止ViewPager的左右滑动
        1. 参考 思路:重写android.support.v4.view.ViewPager中的ViewPager 写一个NoScrollViewPager继承ViewPager   然后用NoScrollVi ...

        1. yarn storm spark
        1. 单机zookeeper http://coolxing.iteye.com/blog/1871009 storm http://os.51cto.com/art/201309/411003_2.htm ...