Python3数据类型-01

2019-12-05  本文已影响0人  snape00

Python3数据类型-01

在学习了100Days教程的前几节之后,我觉得有必要对python的一些基本知识进行重温和复习。我学习了菜鸟教程里面的一些基础内容,简要地对一些重要内容进行如下汇总。

python3变量

Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建

在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型。

用等号(=)给变量赋值,等号(=)运算符左边是一个变量名,等号(=)运算符右边是存储在变量中的值。

a = 15

多个变量赋值:

a = b = c = 3 #三个变量被赋予相同的值。

a, b, c = 2, 5.6, "hello world!" #依次给a,b,c赋2,5.6和“hello world!”。

测试代码如下:

>>> a = b = c = 3
>>> print(a, b, c)
3 3 3
>>> 
>>> a, b, c = 2, 5.6, "hello world!"
>>> print(a)
2
>>> print(b)
5.6
>>> print(c)
hello world!
>>> 

python3数据类型

Python3中标准数据类型共计有以下几种:

不可变数据:Number(数字),String(字符串)和Tuple(元组)

可变数据:List(列表),Set(集合),Dictionary(字典)

Number(数字)

python中数字类型共计有四种:

需要注意的几点是:

String(字符串)

字符串的截取语法格式;

变量[头下标:尾下标:步长]

一个简单的例子:

字符串

代码如下:

Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> str = "hellochirswang"
>>> print(str[0:])
hellochirswang
>>> print(str[:-1])
hellochirswan
>>> print(str[0:-1])
hellochirswan
>>> print(str[2:7])
lloch
>>> print(str[5])
c
>>> print(str[-5:-1])
swan
>>> print(str + "cool")
hellochirswangcool
>>> print(str*3)
hellochirswanghellochirswanghellochirswang
>>> print(str[-1])
g

字符串的截取可以参考下面这张图(引自菜鸟教程):

字符串截取

从图上可以看出来,可以这么理解,无论正向索引还是逆向索引,索引结果都是包括索引头下标对应的内容,不包括索引尾下标对应的内容。

上一篇下一篇

猜你喜欢

热点阅读