Automatic operation and maintenance [Python] ------ python data types in [integers, floating point, string, bool type], and data type conversion

1. Integer

In Python, available forInteger execution plus (+) decrease (-), multiply (*) divide (/) operator

>>> a = 1 # 定义变量a是整形

After the definitions can be directlyView print variable

>>> a
1
>>> print(a)
1

To see the type of data you can usetype () function

>>> type(a) # 查看 a 的数据类型
<type 'int'>

2. Float

Python digital decimal point are called floating-point numbers.

>>> b = 1.2
>>> type(b)  # 查看 b 的数据类型
<type 'float'>

3. String

String: is a series of characters. In Python,Is a string enclosed in quotation marks in, Which may be a single quotation marks, and may be in double quotes.

>>> c = 'westos'# 可以使用单引号
>>> c
'westos'
>>> type(c)
<type 'str'>

Note : where theMay be a single quotation marks, it may be a double quotesThis flexibility allows you to be able toQuotes and string comprising an apostrophe

Single quoted strings containing only single quotes

>>> c = 'what's' # 如果一串字符中有单引号就不能直接使用单引号标示字符
  File "<stdin>", line 1
    c = 'what's'
              ^
SyntaxError: invalid syntax

We can useEscape character, Which tells the interpreter single quote character.

>>> c = 'what\'s'# 你可以使用转义字符解释其中的单引号
>>> c
"what's"

orDirect use double quotes

>>> c = "what's" # 也可以直接使用双引号
>>> c
"what's"

4.bool type

Only two values

  • True
  • False
>>> a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> bool('')#空字符
False
>>> bool(' ')#有空格
True

Note : As long as the air is not 0 and False,Space is a character

The value type of conversion

5.1 integer to a float

Integer can be directly converted to float

>>> a = 1
>>> type(a)
<type 'int'>
>>> float(a)# 整形可以转换成浮点型,但是这只是显示,如果要引用转换后的值
1.0

Plastic can be converted into a floating-point type, but this just shows that ifTo reference value after the conversion, you need to define it as a variable

>>> a
1
>>> b = float(a) # 直接把值定义为一个新的变量
>>> b
1.0
>>> type(b)
<type 'float'>

This direct reference to the converted.

5.2 Float to Integer

When all of the floating point to integer conversion, directTaking the integer part, becomes an integer

>>> b = 2.0 #整浮点型也可以转换为整形
>>> type(b)
>>> b = 2.2 #如果大于整数,直接取整数部分
>>> int(b)
2
>>> b = 2.8
>>> int(b)
2

5.3 String conversion look at the situation

5.3.1 alphabetic string

Alphabetic numeric string does not

>>> c = 'song'# 字母型字符串不可以转换成为数值型
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'song'

5.3.2 numeric string

It can be converted to numeric strings of numeric

>>> c = '123' # 但是数值型的字符串可以转换
>>> int(c)
123

6. Delete variables

When we end the program after the implementation, the variable is invalid, but in interactive mode, we do not need this variable, do not want to exit the interactive mode, you need toDelete variablesA.

>>> a = 1
>>> del a # 使用del关键字可以删除内存中的关键字
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

After the deleting a memory, there is no point to, It is no longer for some variables.

Published 10 original articles · won praise 0 · Views 238

Guess you like

Origin blog.csdn.net/mango_kid/article/details/104773580