Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

4. Python 基本数据类型

Python数据类型用于定义变量的类型。之前我们学习了Python中的语句和注释。如果你想的话,你可以从Python注释和语句.

目录

Python数据类型

有不同类型的python数据类型。一些内置的python数据类型包括:

Python数据类型和数字

Python numeric数据类型用于保存以下数值:;

  1. int–;包含长度不限的有符号整数。
  2. long-保存长整数(存在于python2.x中,在python3.x中不推荐使用)。
  3. float-保存浮动精度数字,精度高达小数点后15位。
  4. 复数-保存复数。

在Python中,我们不需要声明数据类型,而声明一个变量,比如C或C++。我们可以简单地在变量中赋值。但是如果我们想知道它现在持有什么类型的数值,我们可以使用类型(),如下所示:




#create a variable with integer value.

a=100

print("The type of variable having value", a, " is ", type(a))



#create a variable with float value.

b=10.2345

print("The type of variable having value", b, " is ", type(b))



#create a variable with complex value.

c=100+3j

print("The type of variable having value", c, " is ", type(c))

如果您运行上面的代码,您将看到如下图所示的输出。

Python数据类型和字符串

字符串是一个字符序列。Python支持Unicode字符。通常,字符串由单引号或双引号表示。




a = "string in a double quote"

b= "string in a single quote"

print(a)

print(b)



# using "," to concatenate the two or several strings

print(a,"concatenated with",b)



#using "+" to concate the two or several strings

print(a+" concated with "+b)

上面的代码产生的输出如下图所示-

Python数据类型和列表

list是Python中唯一的通用数据类型。从某种意义上讲,它与C/C++中的数组是一样的。但是Python中列表的有趣之处在于它可以同时保存不同类型的数据。正式列表是用方括号([])和逗号(,)写入的一些数据的有序序列。




#list of having only integers

a= [1,2,3,4,5,6]

print(a)



#list of having only strings

b=["hello","john","reese"]

print(b)



#list of having both integers and strings

c= ["hey","you",1,2,3,"go"]

print(c)



#index are 0 based. this will print a single character

print(c[1]) #this will print "you" in list c

上面的代码将产生如下输出-

 

Python数据类型和元组

元组是另一种数据类型,它是类似于list的数据序列。但它是不变的。这意味着元组中的数据是写保护的。元组中的数据是用括号和逗号写的。




#tuple having only integer type of data.

a=(1,2,3,4)

print(a) #prints the whole tuple



#tuple having multiple type of data.

b=("hello", 1,2,3,"go")

print(b) #prints the whole tuple



#index of tuples are also 0 based.



print(b[4]) #this prints a single element in a tuple, in this case "go"

上面这个python数据类型元组示例代码的输出如下所示。

字典

Python字典是键-值对形式的无序数据序列。它类似于哈希表类型。字典是用大括号写成的key:value. It is very useful to retrieve data in an optimized way among a large amount of data.




#a sample dictionary variable



a = {1:"first name",2:"last name", "age":33}



#print value having key=1

print(a[1])

#print value having key=2

print(a[2])

#print value having key="age"

print(a["age"])

如果运行这个python字典数据类型示例代码,输出将如下图所示。

因此,这就是今天关于python数据类型的全部内容。别忘了在自己的机器上运行每一段代码。也不要粘贴。试着自己写几行代码。#happy_编码*

参考文献:数据类型的Python文档