# 2. 无门槛学会数据类型与输入、输出函数，滚雪球学 Python python 入门教程非常详细

## 已完成的文章

https://dream.blog.csdn.net/article/details/109586142

@

&#x672C;&#x7CFB;&#x5217;&#x6587;&#x7AE0;&#x5C06;&#x5728;2021&#x5E74;&#x6625;&#x8282;&#x524D;&#x5B8C;&#x6210;&#xFF0C;&#x6B22;&#x8FCE;&#x5173;&#x6CE8;&#xFF0C;&#x70B9;&#x8D5E;&#xFF0C;&#x8BC4;&#x8BBA; --- &#x68A6;&#x60F3;&#x6A61;&#x76AE;&#x64E6;

# 二、Python 基本数据类型与输入、输出函数

## 2.1 基本数据类型

1. 数值数据类型，常见的是整数和浮点数
2. 布尔值数据类型，就是常说的真和假
3. 字符串数据类型

x = 10
print(type(x))





x = 10.1
print(type(x))






### 2.1.1 数值数据类型

x = 10.1
y = 10
z = x + y
print(type(z))


### 2.1.2 整数的不同形式表示

[En]

Note that integers will also be displayed as octal and hexadecimal in a moment. Here we deal with integers, that is, those with no decimal point, such as 1, 2, 3, 4, etc., so don’t consider floating point.

x = 10
print(bin(x))


0b1010


0x 开头的数字为十六进制格式展示，转换用函数为 hex

### 2.1.3 数据类型强制转换

[En]

This place is just the tip of the iceberg of type casting. Why you are learning is also because you have mastered integers and floating-point numbers, and probably have a glimmer of understanding of their previous differences. In fact, they can also be converted to each other before, but the conversion you have to take some risks, why, look down.

x = 10.5
print(int(x))


int 其实也是 Python 的一个内置函数，它会尝试将任何数据类型的变量转换成整数，注意是任何数据类型的变量。

### 2.1.4 扩展几个数值数据类型常用的函数

[En]

Here are a few examples of functions commonly used in numerical data types, which will be studied in detail later.

• abs() 计算绝对值
• pow() 次方运算
• round() 四舍五入
• max() 取最大值
• min() 去最小值

# abs() 计算绝对值
a = -1
print(abs(a))
# pow() 次方运算
x = 2
c = 3
print(pow(x,c))
# round() 四舍五入

d = 34.6
print(round(d))
# max()  取最大值
print(max(1,2,3))
# min() 去最小值
print(min(9,10,6))



### 2.1.5 布尔值数据类型

x = True
print(int(x))
print(int(False))


[En]

Boolean value will be often used in condition judgment in the future, belonging to the knowledge points that must be mastered, and we will encounter it later.

### 2.1.6 字符串数据类型

my_name = "梦想橡皮擦"
print(my_name)

my_name = '梦想橡皮擦'
print(my_name)

my_name = '梦想"橡皮擦'
print(my_name)
print(type(my_name))


[En]

The following sentence to remember that single and double quotation marks are in pairs, double quotation marks should be nested within single quotation marks, such as the third paragraph of code above, double quotation marks should be nested inside single quotation marks.

a = "my"
b = "name"

c = a+b
print(c)


a = 123
b = "name"
# str(a) 将整数变量a转换成字符串
c = str(a) + b
print(c)



[En]

In the previous article, I have learned that three quotes can be used for annotations, but the real use of three quotes is still a string scene, representing a multiline string.

my_str = """

"""
print(my_str)


[En]

There are some special characters in a string that require special treatment. for example, if you want to use single quotation marks in single quotation marks, you need to write in the following format:

a = 'I\'m a girl '
print(a)



，字符串前面增加特殊字符有很多，今天只学习这一个。例如下述代码。

[En]

There are many special characters added before the string, and this is the only one we will learn today. For example, the following code.

a = r"I\nm a girl"
print(a)


I\nm a girl


Python 中有一个独特的小技巧，可以快速复制字符串，使用的是数学符号 *，例如下述代码，将快速复制一堆 # 号。

print("#"*100)


## 2.2 输入与输出

help(print)


print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)


• value 表示要输出的数据，可以多个，用逗号 , 分隔
• sep 输出多个数据时，分隔符号，默认是空格
• end 输出一行末尾输出的符号
• file 输出位置，默认输出控制台，就是黑窗口中，可以设置到具体文件
• flush 是否清除数据流缓冲区，默认为 False（该内容先不做涉及）

# 输出多个数据
print("hello","dream")

# 多个数据输出的分隔符
print("i","love",sep="#")


### 2.2.1 格式化 print 输出

[En]

In many textbooks, this part of the content will take up more time, in fact, it is not necessary, because things are too miscellaneous, but forget, it is better to simply learn to leave a “um, there is this thing” traces in the brain.

• %d 整数输出
• %f 浮点数输出
• %x 十六进制输出
• %o 八进制
• %s 字符串输出，其实会这一个就行了

name = "橡皮擦"
age = 18
score = 100

# 格式化一个变量输出
print("我是 %s" % name)
# 格式化多个变量输出
print("我是 %s 今年 %d岁了，我考试得了%d分" % (name,age,score))



name = "橡皮擦"
age = 18
score = 100

print("我是 %s" % name)
print("我是 %s 今年 %d岁了，我考试得了%d分" % (name,"nnn",score))



name = "橡皮擦"
age = 18
score = 100

print("我是 %s" % name)
print("我是 %s 今年 %s岁了，我考试得了%d分" % (name,18,score))


### 2.2.2 format 函数

print("我是{}，今年{}，考试得了{}".format(name,age,score))


### 2.2.3 input 数据输入

input 执行的和 print 恰好想法，通过 help 查看一下：

name = input("请输入你的姓名：")
print("输入的姓名为{}".format(name))


input 比较简单，本文先介绍这些。

## 2.3 这篇博客的总结

&#x597D;&#x597D;&#x5B66;&#x4E0B;&#x53BB; &#x6BCF;&#x5929;&#x90FD;&#x6709;&#x65B0;&#x6253;&#x51FB; O(∩_∩)O 哈哈~

&#x60F3;&#x5B66;Python&#x722C;&#x866B;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA2;&#x9605;&#x6A61;&#x76AE;&#x64E6;&#x4E13;&#x680F;&#x54E6;~
https://dream.blog.csdn.net/article/details/86106916

Original: https://www.cnblogs.com/happymeng/p/pythonjiaocheng.html
Author: 梦想橡皮擦
Title: 2. 无门槛学会数据类型与输入、输出函数，滚雪球学 Python python 入门教程非常详细

(0)