Wu Python canon --Python basic data types & basic operators (Chapter II)

Welcome to the Wu's blog, if any, please add QQ group contact: 1055524279

1.1 What is a data type?

  We humans can easily distinguish the difference between numbers and characters, but the computer does not die, although very powerful computer, but in some ways and silly point of view, unless you explicitly tell it, 1 is a number, "Han" is the text, otherwise it is the difference and 'Chinese' tell of 1, therefore, in each programming language will have a data type of stuff is called, in fact, a variety of commonly used data types are clearly divided, you want the computer numerical calculation, you can transfer it to digital, you want him to word processing, you pass a string type to him. There are a variety of commonly used data types Python, as follows:

 Integer (int), a string (str), Boolean (bool), list (list), tuple (tuple), the dictionary (dict), a collection (set).

 int. Digital: mainly used for operations. 1, 2, 3 ...

 bool. Judge true or false: True, False.

 str. Simple small amount of stored data and the corresponding operation. name = 'laowu',

 tuple. Read-only, can not be changed. (1, 'laowu') 

 list: a number of sequences of data, [1, 'sex', True, [1,2,3], { 'name': 'laowu'}]

 dict: large amounts of data, and is a relatively strong correlation data { 'name': 'laowu', 'age': 25, 'name_list': [ 'John Doe', 'John Doe']}

1.11 Python numeric types introduced

Integer: int type, examples: integer 6.

Age, grade, ID number,: the role of integer numbers associated qq number, etc.

Is defined:      Age = 10 # nature age = int (10)

Float: float type, example: 2.1 for the float.

Float float    functions: payroll, height, weight, body mass and other parameters related to floating-point

salary = 36.666 # nature salary = float (6.66)

 

"" "
Plastic surgery: int
Status: grade, identity number, age
Age = 10
Print (of the type (Age))
" ""

"" "
Float: float
Status: salary, height, weight,
salary = 36.666 # = float essence salary (6.33)
salary = 36.33 # = float essence salary (6.33)
Print (salary,)

" ""

2.1   Boolean type bool

python in a boolean constants True and False to represent; attention to the case

  Comparison operators <> == return type and the like is bool type; Boolean type are commonly used in the if and while statements

Note: The value of non-boolean type, that is really a non-zero, non-empty both true

 

Code:

#布尔数据类型
print(True or False) #True
print(1 or False) #1
print(False or False) #False
print(True  or True ) #True
print(bool("")) #False

 

3.1 basic operators:

Int multiplier subtraction type is mainly used and other basic operators

Arithmetic Operators

description

Examples

plus

Two objects are added a + b output 5

Less

To obtain a negative number or another number by subtracting a - b output 3

Multiply

Multiplying two numbers or returns the string 6 outputs a * b be repeated several times in a

except

x is divided by y a / b output 1.5

Take divisible

Take providers, 3 // 2 to obtain 1; supplier returns the integer part of the output 3 1 // 2.

Remainder

Returns the remainder of the division a% b 1 output

power

Returns the power of y x a ** b 2 to the power of 3, the output 9

  

Reference Code:

# Underlying operator
A =. 3
B = 2
Print (+ 2. 3). 5 #       adding
print (3 - 2) # 1       subtraction
print (3 * 2) # 6       multiplies
print (3/2) # 1.5     divided
print (3% 2) # 1       take the remainder
print (3 // 2) # 1       takes integer's
print (3 ** 2) # 9 3 of 2 power

3.1.1   Compare operation

    Suppose the following variables: A =. 3 , B = 2

 

 

 

Figure:

 

 

 

 

Code:

. 3 = A
B = 2
Print (. 3> 2)
Print (. 3 <2)
Print (== 2. 3)
Print (. 3! = 2)
Print (. 3> = 2)
Print (. 3 <= 2)
Print (2 < 4 <3)
# <> indicates not equal now abandoned

 

3.1.2   Assignment Operators

 

 

 

 

3.1.3   member operator

 

Code:

#成员运算符
# 如果在指定的序列中没有找到值返回 True,否则返回 False
a = 2
mylist = [1,2,3,4,5]
c = a in mylist
# c = a not in mylist
print(c)

 

3.1.4  逻辑运算

 

 

 

 

# and  从左往右都成立才成立
# or 或  从左往右只有一个成立则成立

# not 返回相反的值

 

 

 

 

 

代码:

# 逻辑运算符
print(not(3 > 5))             #返回相反的值
print(not(3 < 5))
print((5 > 3)and (9 > 6))     #同真才为真
print((5 > 3)and (9 < 6))
print((5 > 3)or (9 > 6))      #一真即为真
print((5 > 3)or (9 < 6))

 

3.1.5  复合赋值运算符

运算符

描述

实例

+=

加法赋值运算符

c += a 等效于 c = c + a

-=

减法赋值运算符

c -= a 等效于 c = c - a

*=

乘法赋值运算符

c *= a 等效于 c = c * a

/=

除法赋值运算符

c /= a 等效于 c = c / a

%=

取模赋值运算符

c %= a 等效于 c = c % a

**=

幂赋值运算符

c **= a 等效于 c = c ** a

//=

取整除赋值运算符

c //= a 等效于 c = c // a

案例代码:

a = 5
b = 2
c = a + b
print("c的值为:", c)
b += a
print("b的值为:", b)
b *= a
print("b 的值为:", b)
b /= a
print("b的值为:", b)
b %= a
print("b 的值为:", b)
b **= a
print("b 的值为:", b)
b //= a
print("b 的值为:", b)

3.1.6  身份运算符

 

 

 

 

is 是比较两个引用是否指向了同一个对象(东西)(引用比较)。

== 是比较两个对象(值)是否相等。

 

 

 

 

扩展案例:

 

 

 

 

注意,因为python对小整数在内存中直接创建了一份,不会回收,所有创建的小整数变量直接从对象池中引用他即可。但是注意Python仅仅对比较小的整数对象进行缓存(范围为范围[-5, 256])缓存起来,而并非是所有整数对象。也就说只有在这个[-5,256]范围内创建的变量值使用is比较时候才会成立。

 

Guess you like

Origin www.cnblogs.com/wusirtpc/p/12311427.html