python基础入门(2)

目录

一、Python数据类型

1)置数据类型

2)获取数据类型

3)设置数据类型

4)设置特定数据类型

6)练习题

二、python数字-

1)整数

2)浮点数

3)虚数

4)类型转换

5)随机数

6)练习题

 三、python指定变量类型


一、Python数据类型

1)置数据类型

默认情况下,Python 具有以下内置数据类型,在这些类别中:

文本类型:	str
数字类型:	int, float, complex
序列类型:	list, tuple, range
映射类型:	dict
套装类型:	set, frozenset
布尔类型:	bool
二进制类型:	bytes, bytearray, memoryview

此时你大可不必知道这些类型到底是什么,在后续的深入中,你一定会用得到,所以是需要记住的。



2)获取数据类型

您可使用以下type()函数获取任何对象的数据类型。
例如,打印变量 x 的数据类型:

x = 6
print(type(x))

我们可以看到返回为:int类型

3)设置数据类型

在 Python 中,数据类型是在为变量赋值时设置的。
例如以下例子。
str字符串:

x = "Hello World"
print(x)
print(type(x)) 

int整形:

x1 = 6
print(type(x1))

float浮点类型:

x2 = 6.5
print(type(x2))

complex复数类型:

x3 = 2j
print(x3)
print(type(x3))

list列表类型:

x4 = ["apple", "banana", "cherry"]
print(x4)
print(type(x4))

tuple元祖类型:

x5 = ("apple", "banana", "cherry")
print(x5)
print(type(x5)) 

后面还有其它一些类型,我就不以完整代码形式演示了,直接以例子形式让大家看看什么样子是什么类型,当然如果你能亲自动手像我上面的例子一样进行操作打印看一看就再好不过了。
range范围类型

x = range(6

dict字典类型

x = {"name" : "John", "age" : 36}	

set集合类型:

x = {"apple", "banana", "cherry"}

不常用的冻结集类型:

x = frozenset({"apple", "banana", "cherry"})

bool布尔类型:

x = True

不常用byte字节类型:

x = b"Hello"

不常用bytearray字节数组类型:

x = bytearray(5)

更有冷门到爆的memoryview内存试图类型

x = memoryview(bytes(5))

4)设置特定数据类型

我会举一些例子说明,尽量很全,大可不必担心。先举一个完整例子,后面的是一样的打印就不演示了。
强调特定x为字符串:

x = str("Hello World")
print(x)
print(type(x))

返回为:

强调x为整形:

x = int(20)

强调x为浮点:

x = float(20.5)

强调x为复数:

x = complex(1j)

强调为列表

x = list(("apple", "banana", "cherry"))

强调为元祖

x = tuple(("apple", "banana", "cherry"))

强调为范围

x = range(6)

强调为字典

x = dict(name="John", age=36)

强调为集合

x = set(("apple", "banana", "cherry"))

强调冻结集(没啥用的类型)

x = frozenset(("apple", "banana", "cherry"))

强调布尔类型

x = bool(5)

强调字节类型

x = bytes(5)

强调字节组类型

x = bytearray(5)

强调内存试图类型(又是没啥用的类型)

x = memoryview(bytes(5))

6)练习题

回答下面的问题结果为什么类型?
1-

x = 5
print(type(x))

2-

x = "Hello World"
print(type(x))

3-

x = 20.5
print(type(x))

4-

x = ["apple", "banana", "cherry"]
print(type(x))

5-

x = ("apple", "banana", "cherry")
print(type(x))

6-

x = {"name" : "John", "age" : 36}
print(type(x))

7-

x = True
print(type(x))



二、python数字-

Python 共有三种数字类型:

  • int
  • float
  • complex

三种类型分别对应如下例子: 

x = 1    # int
y = 2.8  # float
z = 1j   # complex

要验证 Python 中任何对象的类型,请使用以下type()函数:

print(type(x))
print(type(y))
print(type(z))

因此你可以运行如下代码:

x = 1    # int
y = 2.8  # float
z = 1j   # complex
print(type(x))
print(type(y))
print(type(z))



1)整数

Int 或 integer,是一个整数,正负,不带小数,长度不限。
例如:

x = 1
y = 3562254887
z = -35522
print(type(x))
print(type(y))
print(type(z))



2)浮点数

浮点数或“浮点数”是包含一位或多位小数的正数或负数。
例如:

x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))

浮点数也可以是带有“e”的科学数字,表示 10 的幂。
例如:

x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))

3)虚数

复数写有“j”作为虚部。

x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))



4)类型转换

比如你可以从一种类型转变成另一种同int(), float()和complex()方法。
例如:(你可以亲自运行一下)

x = 1    # int
y = 2.8  # float
z = 1j   # complex


a = float(x)


b = int(y)


c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))



5)随机数

Python 有一个内置模块 random可以用来生成随机数。
示例:导入 random 模块,并显示 1 到 10之间的一个随机数:

import random
print(random.randrange(1, 11))



6)练习题

1-插入正确的语法将 x 转换为浮点数。

x = 5
x = _(x)

2-插入正确的语法以将 x 转换为整数。

x = 5.5
x = _(x)

3-插入正确的语法以将 x 转换为复数。

x = 5
x = _(x)

 三、python指定变量类型

python 中的转换是使用构造函数完成的:

  • int() - 从整数文字、浮点文字(通过删除所有小数)或字符串文字(提供字符串表示整数)构造整数
  • float() - 从整数文字、浮点文字或字符串文字构造浮点数(提供字符串表示浮点数或整数)
  • str() - 从多种数据类型构造一个字符串,包括字符串、整数文字和浮点文字

我将每一个类型都举例子说明。
整数

x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

浮点

x2 = float(1)     # x will be 1.0
y2 = float(2.8)   # y will be 2.8
z2 = float("3")   # z will be 3.0
w2 = float("4.2") # w will be 4.2

字符串

x1 = str("s1") # x will be 's1'
y1 = str(2)    # y will be '2'
z1 = str(3.0)  # z will be '3.0'

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值