B station horse soldier python entry basic version detailed notes (2)

1. The definition and use of variables

Variables consist of three parts: variable name, operation expression, value

name='楚留香'
print(name,id(name))
print(name,type(name))
print(name)

In fact, the id value is stored in the name. This id value is stored in both the name and'Chu Liuxiang', which are the same. When print (name), the ID corresponding to the value in the name will be found "Chu Liuxiang," Then output, under normal circumstances, only the id, type, and value are stored in the variable memory you named. The variable name is equivalent to a pointer. If you assign multiple times, the id value corresponding to the pointer will change, and it will point to the id value corresponding to different values.

name='楚留香'
name='张三'
print(name,id(name))
print(name,type(name))
print(name)

Two, data type

1. Integer : int

2. Floating point type : float

After adding floating-point variables, there may be inaccurate values. You can consider introducing the decimal module. Decimal is actually a data type. It is a decimal data type. In fact, it is a forced type conversion of the flaot type. If it is not allowed to add floating-point numbers, there will be no error in adding floating-point numbers.

For the decimal module, you can use its own getcontext().prec attribute to change its precision. Assuming the precision is 1, it is equivalent to only one number, and so on, his default precision is 28 digits.

The difference between it and the floating-point number type is that it is more accurate, so when you need to express the number very accurately, you must use decimal. The floating-point number can easily represent a large range of numbers, but it is not very accurate, and they are approximate , So generally use decimal to represent the exact value

import decimal
decimal.getcontext().prec=28
n1=1.1
print(n1,type(n1))
n2=2.2
print(n1+n2)
#print(Decimal('n1')+Decimal('n2'))         这样是不行的,因为他是数据类型,你不可以吧变量名放进去让他类型转换,你只可以放变量的值进去进行类型转换
print(decimal.Decimal('1.1')+decimal.Decimal('2.2'))
print(decimal.getcontext().prec)

m1=decimal.Decimal('1.1')
m2=decimal.Decimal('2.2')
print(m1+m2)

3. Boolean type: There
are only two values: true and false.
Unlike other languages, it can be directly calculated as an integer. True defaults to 1, False defaults to 0, because python is strictly case-sensitive, so true The T must be uppercase, and the false F must also be uppercase

m1=True
m2=False
print(m1,type(m1))
print(m2,type(m2))
print(m1+1)                                     #结果是2
print(m2-1)                                     #结果是-1

4. str data type
String definition can use single quotation marks, double quotation marks, or triple quotation marks. The difference between triple quotation marks and other quotation marks is that the value of its string can be input or displayed in two lines

str1='人生苦短,我用python'
str2="人生苦短,我用python"
str3='''人生苦短,我用python'''
str4="""人生苦短,我用python"""
str5='''人生苦短,
我用python'''
str6=""""人生苦短,
我用python
"""

print(str1)
print(str2)
print(str3)
print(str4)
print(str5)
print(str6)

5. Str type conversion is
generally used in the case of linkers, because the linker requires that the data types before and after the connection must be consistent, so type conversion is used.

name='张三'
age=20
#print('我的名字是:'+name+',我的年龄是多大:'+age)            #因为连接符前后数据类型不一样,所以报错
print('我的名字是'+name+',我的年龄是:'+str(age))

6. Int type conversion
Generally, only the value of the string type can be converted to an integer, and the decimal must be discarded when converting with a floating-point number. When converting a Boolean type, the conversion can be done normally.

s1='98'
s2='98.8'
s3='tom'
f1=98.8
b1=True
b2=False
print(s1,s2,s3,f1,b1,b2)
print(int(s1))
#print(int(s2))                不是整型,不能转换
 print(int(s3))                不是整型,不能转换
print(int(f1))                  #转完之后直接取整
print(int(b1))
print(int(b2))

7. Floating point number type conversion: float()
can convert everything except the part of the string that cannot be converted. In addition, when the converted value is an integer, it will automatically fill in zeros, no matter what type of the integer is of

s1='1980'
s2='198.59'
s3='hello'
i1=95
b1=True
b2=False
print(float(s1))                #他转换为1980.0,因为是整数,所以带0
print(float(s2))                #转换为198.59
#print(float(s3))              #无法转换,因为是文字
print(float(i1))                #他转换为95.0,因为是整数,所以带0
print(float(b1))                #他转换为1.0,因为是整数,所以带0
print(float(b2))                #他转换为0.0,因为是整数,所以带0

8. Comment mode
There are three comment modes:
#1. The first is very common # to comment
2. The second is to comment with triple quotation marks, because the string represented by triple quotation marks can wrap, so you can express comments, for example

''''嘿嘿嘿,
我是一个
注释哦
'''

3. The third type is to specify the opening method of the python file in the first line of the pyth file. For example, the first line of the python file indicates what format he opened

#coding:utf-8
print('hello world')

Guess you like

Origin blog.csdn.net/qq_43511094/article/details/113063076