一、Python 变量类型

变量存储在内存中的值。这就意味着在创建变量时会在内存中开辟一个空间。

基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。

因此,变量可以指定不同的数据类型,这些变量可以存储整数,小数或字符。

(1)变量的命名:

变量名的长度不受限制,但其中的字符必须是字母、数字、或者下划线(_),而不能使用空格、连字符、标点符号、引号或其他字符。

变量名的第一个字符不能是数字,而必须是字母或下划线。

Python区分大小写。

不能将Python关键字用作变量名。

例如: a a1 _a

(2)变量的赋值:

是变量的声明和定义的过程。

a = 123

In [1]: a = 123

In [2]: a

Out[2]: 123

In [3]: id(a)

Out[3]: 7891024

In [4]: a = 456

In [5]: id(a)

Out[5]: 19127624

(3)运算符和表达式:

赋值运算符 (=、+=、-=、*=、/=、%=)

算术运算符(+、-、*、/、%)

关系运算符(>、<、>=、<=、!=)

逻辑运算符(&& 、||、not)

表达式:

将不同的数据(包括变量、函数)用运算符号按一定的规则连接起来的一种式子。

1)赋值运算符

In [68]: a = 3

In [69]: a

Out[69]: 3

In [70]: a+=3

In [71]: a

Out[71]: 6

In [72]: a-=4

In [73]: a

Out[73]: 2

In [76]: a*=3

In [77]: a

Out[77]: 6

In [78]: a/=2

In [79]: a

Out[79]: 3

In [80]: a%=3

In [81]: a

Out[81]: 0

In [82]:

2)算术运算符

In [82]: 1 + 2

Out[82]: 3

In [83]: 2 - 1

Out[83]: 1

In [84]: 2 * 2

Out[84]: 4

In [85]: 6 / 2

Out[85]: 3

In [86]: 6 % 2

Out[86]: 0

In [88]: 3.999999 / 2

Out[88]: 1.9999995

In [89]: 3.999999 // 2

Out[89]: 1.0

In [90]: 3 ** 2

Out[90]: 9

3)关系运算符:

In [91]: 1 > 2

Out[91]: False

In [92]: 2 < 3

Out[92]: True

In [93]: 2 >= 1

Out[93]: True

In [94]: 3 <= 56

Out[94]: True

In [95]: 3 == 3

Out[95]: True

In [96]: 2 != 34

Out[96]: True

In [97]:

4)逻辑运算符:

In [97]: 1 < 2 and 2 > 0

Out[97]: True

In [98]: 1 == 1 and 2 < 1

Out[98]: False

In [99]: 1 == 1 or 2 < 1

Out[99]: True

In [100]: not 1 > 2

Out[100]: True

二、Python标准数据类型

在内存中存储的数据可以有多种类型。

例如,一个人的年龄可以用数字来存储,他的名字可以用字符来存储。

Python 定义了一些标准类型,用于存储各种类型的数据。

Python有五个标准的数据类型:

Numbers(数字)

String(字符串)

List(列表)

Tuple(元组)

Dictionary(字典)

1.Python数字(Numbers)

Python支持四种不同的数字类型:

int(有符号整型)

long(长整型[也可以代表八进制和十六进制])

float(浮点型)

complex(复数)

整型

In [6]: a = 123

In [7]: type(a)

Out[7]: int

In [8]:

长整型

In [8]: a = 199999999999999999999999999999

In [9]: a

Out[10]: 199999999999999999999999999999L

In [11]: type(a)

Out[12]: long

In [13]:

浮点型

0.0, 12.0 -18.8 3e+7等

科学计数法是浮点型

In [11]: 3e+7

Out[11]: 30000000.0

In [12]: type(3e+7)

Out[12]: float

In [13]: 3.0/2

Out[13]: 1.5

In [14]: type(3.0/2)

Out[14]: float

In [15]:

复数型

python对复数提供内嵌支持,这是大部分软件没有的。

In [8]: a = 3.14j

In [9]: a

Out[9]: 3.14j

In [10]: type(a)

Out[10]: complex

2.Python字符串(string)

字符串或串(String)是由数字、字母、下划线组成的一串字符。

>>> str="hello fengxiaoqing!"

>>> print(str*2)

hello fengxiaoqing!hello fengxiaoqing!

>>> print(str+" Very Good!")

hello fengxiaoqing! Very Good!

In [12]: a = 'abc'

In [13]: a

Out[13]: 'abc'

In [14]: type(a)

Out[14]: str

In [15]:

三重引号还可以做注释:.

In [28]: a = 'hello\nworld'

In [29]: a

Out[29]: 'hello\nworld'

In [30]: a = "hello\nworld"

In [31]: a

Out[31]: 'hello\nworld'

In [39]: a = '''hello\nworld'''

In [40]: a

Out[40]: 'hello\nworld'

In [41]: print a

hello

world

In [42]:

In [43]: type(a)

Out[44]: str

序列索引:

In [42]: a = 'abcde'

In [43]: a[0]

Out[43]: 'a'

In [44]: a[1]

Out[44]: 'b'

In [45]: a[-1]

Out[45]: 'e'

In [46]: a[-2]

Out[46]: 'd'

序列切片:

In [42]: a = 'abcde'

In [43]: a[0]

Out[43]: 'a'

In [44]: a[1]

Out[44]: 'b'

In [45]: a[-1]

Out[45]: 'e'

In [46]: a[-2]

Out[46]: 'd'

In [47]: a[0:2]

Out[47]: 'ab'

In [48]: a[0:4]

Out[48]: 'abcd'

In [49]: a[0:3]

Out[49]: 'abc'

In [50]: a[1:3]

Out[50]: 'bc'

In [56]: a[0] + a[1]

Out[56]: 'ab'

In [57]: a[:2]

Out[57]: 'ab'

In [58]: a[:]

Out[58]: 'abcde'

In [59]: a[:-1]

Out[59]: 'abcd'

In [60]: a[::-1]

Out[60]: 'edcba'

In [61]: a[::1]

Out[61]: 'abcde'

In [62]: a[:3:1]

Out[62]: 'abc'

In [63]: a[::2]

Out[63]: 'ace'

In [64]: a

Out[64]: 'abcde'

In [65]: a[-4::-2]

Out[65]: 'b'

In [66]: a[-4:-2]

Out[66]: 'bc'

In [67]: a[-2:-4:-1]

Out[67]: 'dc'

In [68]:

字符串方法:

(1)find() 查找字符

>>> str="hello fengxiaoqing!"

>>> str.find("o")

4

(2)replace() 替换字符

>>> str="hello fengxiaoqing!"

>>> str.replace("h","H")

'Hello fengxiaoqing!'

(3)strip() 字符串过滤首尾空格

>>> str=" hello fengxiaoqing! "

>>> str.strip()

'hello fengxiaoqing!'

(4)format()字符串格式化

>>> name="fengxiaoqing"

>>> age=20

>>> sex="man"

>>> "姓名:{0} ,年龄:{1},性别:{2}".format(name,age,sex)

'姓名:fengxiaoqing ,年龄:20,性别:man'

(5)startswith() 以某字符开头

>>> str="hello fengxiaoqing!"

>>> str.startswith("helo")

False

>>> str.startswith("h")

True

(6)endswith()以某字符结尾

>>> str="hello fengxiaoqing!"

>>> str.endswith("qing!")

True

(7)split()字符串分割

>>> str="hello fengxiaoqing!"

>>> str.split(" ")

['hello', 'fengxiaoqing!']

>>> str.split("g")

['hello fen', 'xiaoqin', '!']

(8)join()字符串连接

>>> str.join("abc")

'ahello fengxiaoqing!bhello fengxiaoqing!c'

>>> "+".join("abcde")

'a+b+c+d+e'

3.Python列表(list)

List(列表) 是 Python 中使用最频繁的数据类型。

列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。

列表用 [ ] 标识,是 python 最通用的复合数据类型。

列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。

加号 + 是列表连接运算符,星号 * 是重复操作。

举例:

>>> list=[1,3,5,"a","b","c"]

>>> list

[1, 3, 5, 'a', 'b', 'c']

>>> type(list)

列表中的方法:

(1)insert()插入

>>> list=[1,3,5,"a","b","c"]

>>> list.insert(2,"88")

>>> list

[1, 3, '88', 5, 'a', 'b', 'c']

(2)remove()删除

>>>list=[1,3,"88",5,"a","b","c"]

>>>list.remove("88")

>>>list

[1, 3, 5, 'a', 'b', 'c']

(3)sort()排序

>>> list=[1,3,5,6,7,34,7,6734,77,3,34]

>>> list.sort()

>>> list

[1, 3, 3, 5, 6, 7, 7, 34, 34, 77, 6734]

>>> list=["a","d","b","c"]

>>> list.sort()

>>> list

['a', 'b', 'c', 'd']

(4)reverse()反序

>>> list=[1,3,5,6,7,34,7,6734,77,3,34]

>>> list.reverse()

>>> list

[34, 3, 77, 6734, 7, 34, 7, 6, 5, 3, 1]

>>> list=["a","d","b","c"]

>>> list.reverse()

>>> list

['c', 'b', 'd', 'a']

(5)append()从末尾添加一个元素

>>> list=[1,3,5,6,7,34,7,6734,77,3,34]

>>> list.append(9999)

>>> list

[1, 3, 5, 6, 7, 34, 7, 6734, 77, 3, 34, 9999]

(6)pop()从末尾删除一个元素

>>> list=[1,3,5,6,7,34,7,6734,77,3,34]

>>> list.pop()

34

>>> list

[1, 3, 5, 6, 7, 34, 7, 6734, 77, 3]

(7)conunt()统计列表中对象有数量

>>> list=[1,3,5,6,7,34,7,6734,77,3,34]

>>> list.count(7)

2

python123数值运算代码_Python中的变量、数据类型(数值、列表)操作实例相关推荐

  1. python dataframe 列_python pandas库中DataFrame对行和列的操作实例讲解

    用pandas中的DataFrame时选取行或列: import numpy as np import pandas as pd from pandas import Sereis, DataFram ...

  2. mysql resulttype map_Mybatis中的resultType和resultMap查询操作实例详解

    resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题.比 ...

  3. python变量的作用_Python中的变量

    Python中的变量是用来表示一个值的标识符.变量代表了计算机内存中的一个地址.变量允许在程序中访问其他对象,调用函数或执行其他运算. 1.变量命名规则 变量是Python中的标识符,它应该遵循标识符 ...

  4. python布尔型变量错误的赋值_Python中布尔变量的值为( )

    [单选题]x 的 y 次方(xy) 以下表达式正确的是________ [多选题]以下关于 Python 字符串的描述中,正确的是( ) [多选题]下列表达式的值为False的是( ) [其它]返回 ...

  5. python特殊方法大全_python中星号变量的几种特殊用法

    在Python中星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法"在变量前添加单个星号或两个星号",实现多参数的传入或变量的拆解,本文将详细介绍"星号参数" ...

  6. python中的变量_Python中的变量

    python中的变量 This lesson deals with variables. Those who already know some programming must be familia ...

  7. python变量类型函数_python中的变量和数据类型

    一.变量定义:变量是计算机内存中的一块区域,存储规定范围内的值,值 可以改变,通俗的说变量就是给数据起个名字. 二.变量命名规则: 1. 变量名由字母.数字.下划线组成 2. 数字不能开头 3. 不可 ...

  8. python中的常量_Python中的变量和常量

    本文主要介绍Python中的变量和常量,包括变量的命名规范,使用注意事项 -------------- 完美的分割线 --------------- 1.变量 1.1.变量理解 1)什么是变量 变量即 ...

  9. python类中变量作用域_Python中的变量作用域

    1.块级作用域 1 if 1 == 1:2 name = "lzl" 3 4 print(name) //输出lzl5 6 7 for i in range(10):8 age = ...

最新文章

  1. 【转】PendingIntent的总结
  2. SAP PM 初级系列26 - 设备功能位置的Document
  3. python读取数据的函数详解_你了解文件缓存机制吗?磁盘文件如何读写?Python中open函数详解...
  4. python软件包自带的集成开发环境-Python: 内置的集成开发环境-IDLE
  5. python中reduce是什么意思,python中的map和reduce有什么不同
  6. TFS 解除独占锁定
  7. [转]模拟电路设计经典教材推荐
  8. EasyUI文档学习心得
  9. 一段平平无奇的秋招经历
  10. fckeditor for php 下载,FCKeditor 的配置和使用方法(for PHP)
  11. mbedtls基础及其应用
  12. Java修改图片大小尺寸图片缩放
  13. 电脑重启bootmgr_解决电脑出现bootmgr is missing如何解决
  14. 小论文之旅(2)——introduction与related work
  15. MATLAB数字图像处理(二)直方图
  16. 商机无限!在政府门户网站升级改造中掘金
  17. 激光三角测量物体高度
  18. 60.大数据之旅——电信日志项目03
  19. 搭建树莓派 4B + intel movidius 神经元计算棒2代深度学习环境
  20. Win10安装Tomcat服务器与配置环境变量

热门文章

  1. 如何在 SAP Commerce Cloud Portal 构建和部署 SAP Spartacus Storefront
  2. Angular的_zone.onMicrotaskEmpty最终会通过changeDetect重新刷新视图
  3. SAP Spartacus的产品搜索API
  4. Angular Component的加载触发时机
  5. What is the difference between “def” and “val” to define a function
  6. SAP CRM WebClient UI recent object的后台存储实现
  7. 把自定义url配置到SAP Fiori Launchpad上打开
  8. SAP FSM 学习笔记(二) : SAP FSM的微信接入
  9. SAP UI5 why failed to load 'sap/cus/crm/lib/reuse/library.js' from resources/sap/cus/crm
  10. How to include html native content to UI5 page - 直接在xml view里添加html namespace