Data Types operator python

The first program

print("hello world")

 

Variables: Variables in the program is a container. (Used to hold things, go to the supermarket to buy things when push trolley is variable)

= 100 Score # define a variable, the variable name on the Score, a value stored in it 100 
High 180 = # define a variable, the variable value 180

NOTE: Number of Wells # comment number / multi-line comment '' '' ''

 

 

I. basic data types

Python (data type) There are 6 types of data:

Number (digital), String (String), List (list), Tuple (tuple), Dictionary (dictionary), Set (collection)

Where the number of digital type supports int, float, bool, complex (plural).

 

(1) numbers digital

Int int

a = 1
print (type(a))

>>> 2 ** 100
1267650600228229401496703205376L

 

Boolean boor

True False

print (1 > 2)
print (1 < 2)

 

Float float

a = 1.0
print (type(a))

Complex complex

6.23 + 1.5j -1.23-987j

 

 

(2) the string str

name="wuchuan"
print(type(name))

 

 

(3) list (list) 

Python is a list of the most frequently used data types. List is written between the [] brackets, comma-separated list of elements. Type elements in the list may not be identical, which supports numbers, strings may even contain a list (called nesting).

Features: Supports the list modification / deletion / insertion operating element, in [] is empty or only a single / multiple elements, both as a list.

list_1= []

list_2= [1]

list_3 = ["zhangsan","lisi","wangwu"]

list_4=['guangdong','guangxi',['shenzhen','zhuhai']]
 print (list_1, list_3 ,list_4)

 

 

 

(4) tuple (tuple)

Tuples and a list of similar, except that the elements of the tuple can not be modified; tuple is written in parentheses (), the elements are separated by commas.

Note: the parentheses is empty, it is a null set.

In brackets is only one element of a tuple is not defined by the type of element in brackets

To achieve only one element tuple in parentheses, you need to add a comma behind the element ""

tup_1=()
tup_5=("zhangsan","lisi","wangwu")
tup_2=(1)
tup_3=('ni')
tup_4=([2])

print(type(tup_1))
print(type(tup_5))
print(type(tup_2))
print(type(tup_3))
print(type(tup_4))

 

 

 

(5) Dictionary (dict)

There is a key-value dictionary of the data type thereof; braces {} to represent a dictionary. The key-value dictionary of the format, such as brackets { "key1": "value1", "key2": "value2"}. Support for the dictionary to modify / delete / insert elements operate.

Create an empty dictionary, directly behind a variable name with curly braces {}

dic_1= {"name":"zhangsan","age":28,"sex":"boy"}

dic_2 = {"name1":"zhangsan","age":{"age1":"18","age2":20},"name2":"lisi","name3":"wangwu"}

print (dic_1)

print (type(dic_1))

 

(6) a collection (set)

Collection is written inside the braces {}, or set () function creates a collection; comma separated.

Note: Create an empty set must be set () instead of {}, {} as is used to create an empty dictionary.

 

a = {1,2,3}

print (a)

print (type(a))

 

d={}
print(type(set(d)))

 

 

 

II.   Data type conversion

 

1, with the integer to a string str () function

(Int turn str)

= 123 C 
D = STR (C) # by variable assignments
 Print (type (D))
a = 123
print (str(a))
print (type(str(a)))

2. transformed with integer int () function (str turn int)

a = "123"
b = int(a)
print(type(a))
print(type(b))

 

 3.list () method is used to convert a string or a tuple list.

(Str transfer list)

a="123"
b=list(a)
print(type(a))
print(type(b))

 

 

(tuple转list)

a = (1,2,3)
print (list(a))
print (type(list(a)))

 

(Set transfer list)

a={"shenzhen","foshan","xianggan"}
b=list(a)
print(type(a))print(type(b))

 

4. tuple () may be strings, lists, dictionaries, into a set of tuples;

(Str turn tuple)

a="2343"
b=tuple(a)
print(type(b))

(list转tuple)

a=["shenzhe","dongguang",40,23]
b=tuple(a)
print(type(b))

(dict转tuple)

= {A " name " : " Shenzhe " , " Age " : 40 } 
B = tuple (A) Print (B) # The tuple fields to keep the first key time! 
Print (of the type (b))
 

(Set turn tuple)

a={"shenzhen","foshan","xianggan"}
b=tuple(a)print(type(b))

5. set () may be a word list, tuple, set into

(List turn set)

= A [1,2,4,4 ] 
B = SET (A) # SET () function has deduplication function, remove the duplicate elements 
Print (type (B))

(Tuple turn set)

a=('huawei','sanxing','apple')
b=set(a)
print(type(b))

(Dict turn set)

a={"shenzhen":"nanshan","foshan":"nanhai"}
b=set(a)print(b)#取键
print(type(b))

 

 

 

 

Third, the operator

1. The arithmetic operators

(1) plus (+)

Note: You can not be between the string and integer addition, the need to convert data type str () or int ()

Integer and integer add

a="3"
b=4
b=str(b)
print(a+b)

 

Floating-point and floating-point sum

"" "Binary storage precision errors" ""  
A = 3.6 B = 4.44 Print (A + B)

 

Integer and floating-point sum

 

Between the string concatenation

a="hello"
b="shenzhen"
print(a+b)

 

Adding the positive and negative

 

 

 

(2) minus (-)

    Note: You can not subtract integers and strings between string

Integer and Integer subtract

>>> 10 - 5
5

And subtracting negative negative

>>> -3 - -4

1

Positive and negative subtraction

Floating-point subtraction of floating point

Integer and floating-point subtract

 

 

(3) multiply (*)

    Note: You can not be multiplied between the string and the string

 

Integer integer multiplied

>>> 3 * 2

6

 

 

Floating-point and floating-point numbers multiplied

Integer and floating-point numbers multiplied

Positive and negative numbers multiplied

Negative with negative numbers multiplied

 

 

 

(4) Subject (/)

    Note: You can not be divided between the strings and the strings and integers

 

 
 
"" "Integer division, the result is an integer." ""
Integer integer division python2.x
 >>> 6/3 
2

 

 

"" "Integers and floating-point numbers." "" 

Python3.x integer and the integer division
>>> 6/3 2.0

 

Floating-point and floating-point division

Integer and floating-point division

 

(5) exponentiation (**)

The x ** y -> y x to the power returns

>>> 3 ** 3

27

 

>>> 10 ** 2

100

 

 

Remainder> Returns division - (6) modulo (%)

>>> 5 % 3
2

"""-a % b = b - (a % b)"""

>>> -304 % 100

96

 

(7) Method divisible (//) -> left rounding

Positive number (positive number MukaiRei ToSei)

>>> 9.0 // 2
4.0

 

Negative (negative take a small number)

>>> -9.0 // 2
-5.0

>>> -9 // 2
-5

 

note:

1. After the first addition and subtraction, multiplication and division
2. exponentiation highest priority

 

 

 

2. assignment operator

 (1) = equal sign is the primary assignment operator

Variable use and naming

 

  Variable names can only contain letters, numbers and underscores, may begin with a letter or an underscore, but can not begin with a number

  Variables can not contain spaces

  Do not python keywords (reserved character) as a variable

  Variable names should be short and descriptive

  Variables are case sensitive

name = "Alex Li"
name2 = name
print(name,name2)
name = "Jack"
print(name)

python garbage collection mechanism is as follows:

Variable equivalent house number, if not the numbers that have no reference to the function call, the memory of the data will be cleared.

There is a timer within python, regular refreshes data in memory if it is found not to be quoted, it will be recycled, this is the memory recall mechanism.

 

 

(2) the addition assignment operator + =

a += 1   --> a = a + 1

a=3
a +=1
print(a)

  

 

(3) - = subtraction assignment operator

a -= 1   --> a = a – 1

a=10
a -=1
a=a-1
print(a)

 

(4) * = multiplication assignment operator

   a *= 1   --> a = a * 1

 

(5) / division assignment operator =

   a /= 2   --> a = a / 2

 

(6)% = modulo assignment operator

   a %= 3  --> a = a % 3

 

(7) ** exponentiation assignment operator =

    c ** = a is equivalent to c = c ** a

 

(8) = // assignment operator takes divisible

   c // = a is equivalent to c = c // a

 

 

 

3. comparison operators (Boolean return value Boolean value ----)

 

(1) == equal - equality comparison

>>> "name" == "name"
True
>>> 1 == 1 True >>> 1 == 2 False

! (2) = or <> Not equal - compare two objects are not equal 

>>> 1 != 2
True

>>> 1 <> 1
False

>>> "name" != "name1"
True

(3)> greater than

>>> 2 > 1
True

>>> 5 > 8
False

>>> "shenzhen">"shenzhe"
True

(4) <Less than

>>> 1 < 2
True

>>> 3 < 2
False

(5)> = Greater than or equal to

>>> 2 >= 2
True

(6) <= Less than or equal

>>> 2 <= 2
True

 

Note: The bit string comparison, ascii code two strings of characters who first big, big strings, no later comparison; the first character of the same than the second string, and so on, Note that the ascii code space 32 is empty (null) ascii code is 0, uppercase and lowercase letters are different ASCII 

 

4. Identity operators (objects used to compare two memory cells - memory address)

 (1) is two identifiers are determined is not a memory address referenced from

the y-IS the X- , similar to the id (x) == id (y ), if the reference is to the same object returns True, otherwise False

>>> 1 is 1
True

>>> 1 is 2
False

 

 (2) is not the two identifiers are determined from different reference objects is not

IS Not Y X , similar id (a)! = id ( b). If the reference is not the result of the same object returns True, otherwise it returns False

>>> 1 is not 1
False

>>> 1 is not 2
True

note:

Object pooling small integers: Python in the course of implementation, in order to save space, to help us create good small integer object pooling, [--5 to 256], are fixed address, whatever you do, there will be.
For example, a = 5, b = 5 , id (a) and id (b) small integer addresses are already fixed address pool, so equal
if, a = 1000, b = 1000 , this time will an address a, b will also give an address, but they are not equal. 

 

 

The logical operators

(1) logical AND (and): while both sides of the expression is true, the result will be true.

a=10
b=30
print(a==9 and b==30)
False
print(a==10 and b==30)
True

 

(2) logical OR (or): both sides of the expression is true, the result will be true. It is true, the result is true .

a=10
b=30
print(a==9 or b==30)
print(a==10 or b==30)
print(a==11 or b==31)

 

(3) non-logic (not): used to invert the expression becomes true false, false becomes true

b=30
print(not b==310)
print(not b==30)

 

Priority logical operators: from left to right :()> not> and> or

计算:1>3 or 3>2 and 1=0 and not(7>2 or 3>5)

From left to right, first calculate (), the 7> 2 is True, then the parentheses is True, look outside the parentheses not, then this part is false.
And then to calculate the left and, 3> 2 to True, See Boolean value 1 = 0, 1 = 0 to False, 3> 2 and 1 = 0 to False.
recalculation 3> 2 and 1 = 0 and not (7> 2 or 3> 5), known 3> 2 and 1 = 0 is False, not (7> 2 or 3> 5) to False, 3> 2 and 1 = 0 and not (7> 2 or 3> 5) to False.
Finally, see 1> 3 False, 3> 2 and 1 = 0 and not (7> 2 or 3> 5) False, the whole False.

 

 

 

6. member operator

(1) in: determining a value in another variable

As follows: a b Return True In sequence, otherwise False

 

a=10
b=[1,2,20,10]
c=(1,2,10)
print(a in b)
print(a in c)

 

 

(2) not in determining the value of a variable is not in the other

 

As follows: a b sequence not return True, otherwise return False

. 1 = A 
B = [1,2,20,10 ] 
C = (3,2,10 )
 Print (A Not  in B)
 Print (A Not  in C) 

Results:

false
True

 

 

Guess you like

Origin www.cnblogs.com/wuya666/p/12592295.html