< one >Python data type

<One > variable, character encoding.

1:Declare variables:

 

#_*_coding:utf-8_*_
#__author__:"shikai"

name="shikai"
#Variable name: value of name variable: Shikai

Define variable rules:

✪Variable names can only be any combination of letters, digits, or underscores.

✪The first character of a variable name cannot be a number.

✪Keyword cannot be declared as variable name.

[‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

 

2:Character coding

1K=1000B    1B=8bit

UTF-8Coding: an English character is equal to one byte, and a Chinese (including traditional) is equal to three bytes. Chinese punctuation takes three bytes, and English punctuation takes one byte.
UnicodeCoding: one English is equal to two bytes, one Chinese (including traditional) is two bytes. Chinese punctuation takes two bytes, and English punctuation takes two bytes.

Set the default encoding format and tell the Python interpreter.

Note: ASCII code can not express Chinese.

#!_*_coding:utf-8_*_
#__author__:"shikai"

name="shikai"

Notes:

  Watch the line: the content of the note is annotated.

     Multi line annotation: “” “note” “” “

User input:

 

#!_*_coding:utf-8_*_
#__author__:"shikai"

#User inputName=input ("your name:")Print (name)

getpassThe module does not allow the input password to be visible: (Note: CMD will not display the password under Windows).

#!_*_coding:utf-8_*_
#__author__:"shikai"

import getpass

# Assign user input content to name variablePWD = getpass.getpass ("pwd:")Print input contentPrint (PWD)

<Two > data type

 1:number

int(Integer):

  For example, 1,2,3

  On 32-bit machines, the number of integers is 32 bits, ranging from – 2 ** 31 to 2 ** 31-1, or – 2147483648 to 2147483647
  On 64-bit systems, the number of integers is 64 bits, ranging from – 2 ** 63 to 2 ** 63-1, or – 9223372036854775808 to 9223372036854775807

float(Floating point):

  Such as: 3.14,5.2

  Floating point numbers are used to deal with real numbers, that is, decimal numbers.

long(Long integer)
  Unlike C, Python’s long integers don’t have a specified bit width, that is, Python doesn’t limit the size of long integer values, but in fact, because of the limited machine memory, we can’t use an infinite number of long integers.
  Note that since Python 2.2, Python automatically converts integer data to long integers if an integer overflow occurs, so no L after long integer data today would have serious consequences.

complex(Plural)
  The complex number is composed of the real part and the imaginary part. The general form is x + yj, where x is the real part of the complex number, y is the imaginary part of the complex number, where x and y are real numbers.

2:Boolean value:True Or False

3:Character string

Each time a string is created, a continuous blank space needs to be created in memory, and once the string needs to be modified, space needs to be opened up again. Each occurrence of the + sign of string splicing opens up a new space in the memory.

Operation:

Note: before and after the index is taken after ignoring the past.

#!_*_coding:utf-8_*_
#__author__:"shikai"

a,b="shikai","AAA"
print(a+b)      #Results: shikaiAAAPrint (a*2): results: shikaishikaiPrint (a[1:4]): results: hikPrint ("s" in a) result: TruePrint("AAA" not in a) result: True

String formatting:

print("{} is student,his num is {}".format("shikai",7))

String built in function:

 

a,b="shikai","AAA"
#:①:find()  Returns the index of the checked string, if not, returns: -1Print (a.find ("I")) results: 2Index: (find) is the same as that in a single file. If the value is not in the string, it will be reported wrong.Print (a.indEx ("I")) results: 2Capitalize () capitalize the first character of a string.Print (a.capitalize ()) result: ShikaiStatistics: number of occurrences of count statisticsPrint (a.count ("I")) results: 2Replace (a, b) replace a with BPrint (a.replace (a, b)) result: AAASPLIt is divided into a list according to the principle of string, and the number behind is only partitioned.Print (a.split ("I")) result: ['sh','ka', ''.Print (a.split ("I", 1))Results: ['sh','kai']String.swapcase () flipped the size in string.String.upper: (string) conversion of small writing inThe mother is a capitalizationJoin: connect 2 strings.STR = "-"Seq = ("a", "B", "C") string string sequencePrint (str.join (SEQ)) results: a-b-c

 4:list


List script operation:

List function:

Increase: append, extend, insert, +, *

list1=[1,2,"shikai"]
list2=["AAA","BBB","CCC"]
#①:list.append(obj) Add new objects at the end of the listList1.append ("AAA")Print (LIST1): [1, 2,'shikai','AAA']List2: add the value in the list to LIST1LIst1.extend (List2) results: [1, 2,'shikai','AAA','AAA','BBB','CCC']List.insert (index, OB)J) insert values in index index bit.List1.insert (1, "Eric") results: [1,'eric', 2,'shikai','AAA']A=list1+list2Results: [1,'eric', 2,'shikai','AAA','AAA','BBB','CCC']Results: b=list1*2, [1,'eric', 2,'shIkai','AAA', 1,'eric', 2,'shikai','AAA']

Delete: del, remove, pop

#_*_coding:utf-8_*_
#__author__:"shikai"

list1=[1,2,"shikai"]
list2=["AAA","BBB","CCC"]

#①:del  Direct deletionDel LIST1Remove: Specifies the deletion of an element.List1.remove ("Shikai") results: [1, 2]Pop () function is used to remove one of the lists.Element (default last element)List1.pop () results: [1, 2]The index location is removed by list1.pop (1). Results: [1,'shikai']

Check: count, index bits, index, in

list2=["AAA","BBB","CCC",1,"AAA",3,"BBB",1]
#①:count The number of times an element appears.Print (list2.count ("AAA")) results: 2(2): index position selectionA=[1,2,3,4,5,6]Print (a[1:4]) results: [2, 34]Print (a[2:]): [3, 4, 5, 6]The second elements of print (a[-2]) are reciprocal: 5
print(a[::2]) #A value of 2 septum: [3,6]Index index bit of an element in the query list.Print (list1.index ("Shikai")) results: 2To judge whether elements exist.Print (2 I)N [3,2,1]) results: True
#④:

Change: list, reverse, sort

#①Change the ancestor to a listA= (1, "Shikai", 3,4)B=list (a)Print (b): [1,'shikai', 3, 4]Direct assignment modifies list1[1]= "A"AA "Reverse flip listList1=[1,2, "Shikai"]List1.reverse ()Print (LIST1) results: ['shikai','AAA',1]The # 4: sort () function is used to sort the original list, and if a parameter is specified, the comparison function specified by the comparison function is used.Reverse -- collation rule, reverse = True descending order, reVerse = False ascending (default).A=[3,8,1,2]Descending order of a.sort (reverse=True)Print (a)

5:tuple

The Yuan Dynasty could not change it, only count and index.

#_*_coding:utf-8_*_
#__author__:"shikai"

name=("shikai","AAA","BBB","AAA")
print(name.count("AAA"))          #Results: 2Print (name.index ("AAA")) results: 1

6:Dictionaries

Dictionary a data type of key – value.

The characteristics of dictionaries are:

  • dictIt’s unordered.
  • keyIt must be unique.

Increase:dict1[“school”]=”CDUT”

#_*_coding:utf-8_*_
#__author__:"shikai"

#①Direct increaseDict1={'name':'shikai','age': 8,'num': 7}Dict1["school" = = "CDUT"Print (dict1): results: {'namE':'shikai','age': 8,'num': 7,'school':'CDUT'}

Delete: pop, popitem, del, clear

dict1={'name':'shikai','age':8,'num':7}
#①:pop Specified deletingDict1.pop ("num") results: {'name':'shikai','age': 8}Del deleteDel dict1["age"]Results: {'name':'shikai','num': 7}Popitem method randomly returns and deletes a pair of keys and values in the dictionary.Dict1.popitem (): results: {'name':'shikai','age': 8}Elimination of dictionariesDict1.clear () result: {}

Check: in, get, keys, values, items

dict1={'name':'shikai','age':8,'num':7}

#① in
print("age" in dict1)     #True

#②:get
print(dict1.get("name"))         #Results: ShikaiPrint: dict1[(name)] key does not exist and reports errors. Results: ShikaiKeys returns all keysPrint (dict1.keys).Results: dict_keys (['name','age','num'])Values: returns all valuesPrint (dict1.values ())Results: Print (dict1.values ())Items: returns a traversable (key, value) tuple array with a list.Print (dict1.items ()) result: dict_items (['naMe','shikai'), ('age', 8), ('num', 7)]Circular queryFor index, keys in enumerate (dict1.items ()):PRInt (index, keys)The result0 ('name','shikai')1 ('age', 8)2 ('num', 7)

Change: update, direct assignment, copy

dict1={'name':'shikai','age':8,'num':7}
dict2={"school":"CDUT"}

dict1["name"]="AAA"     #Results: {'name':'AAA','age': 8,'num': 7}Update:Dict1.update (dict2)Results: {'name':'shikai','aGe': 8,'num': 7,'school':'CDUT'}

#③:copy Shallow copy
dict3=dict1.copy()   
#Results dict3:{'name':'shikai','age': 8,'num': 7}

  

7:aggregate

A set is an unordered, non-repetitive combination of data, a relational test that tests the intersection, difference, union, and other relationships before two sets of data

s = set([3,5,9,10])      #Create a numeric setT = set ("Hello") to create a collection of unique characters.A = t, s, t and S.B = t &amIntersection of P, s, t and SC = t – s s (set of items in T, but not in s).D = T ^ s.T or s, but not in the two party at the same time.Basic operation:Add one item to t.add ('x')S.update ([10,37,42])Add a number of SUsing remove (), you can delete one item:T.remove ('H')Len (s)Length of setX in STest XIs it a member of S?X not in STest whether x is not a member of S.S.issubset (T)S < = tTest whether every element in S is in t.S.issuperset (T)S > = tTest whether every element in t is in S.S.union (T)S tReturns a new set containing s and t.Every element in itS.intersection (T)S & tReturns a new set that contains common elements in S and t.S.difference (T)S - tReturns a new set that contains elements in s but not in t.S.symmetric_difference (T)S ^ tReturns a new set that contains not duplicated elements in S and t.elementS.copy ()Returns a shallow copy of set "s".

  

 

Leave a Reply

Your email address will not be published. Required fields are marked *