There are six standard data types in the Python3: number (numeric) string (string) list (list) Tuple (tuple) Sets (set) Dictionary (dictionary) Python3 Six standard data types: Immutable data (four):
Number (numbers), string (String), Tuple (tuple), Sets (set), variable data (two): List, Dictionary (dictionary). """ImportMathImportRandom Print ("Hello World") Print ("No Line wrap Output 1 2 3") Print ("Use the end keyword for no-line-wrap output") print (1, end=" ") Print (2, end=" ") print (3) # Below is the base data type print ("Output basic data type"# Python3 only one shape # define variables as follows: variable name = variable Parameter # example defines a, B, c three variables so that they all equal 1 # number type means: int, float, bool, complex (plural) a = b = c = 1 # Multiple medium Different variables are also declared myInt, Myfloat, Mybool, Mycomplex, myString, Mytuple, Mysets, MyDictionary, mylist=\ 10, 10.2,True, ten + 2j, \"Custom String", (10, 1,"Tuple", 10.0,True), \
{"Tom","Brush","King"}, {' name ':"Tom",' age ': 13,' Sex ':"Man"},[1,"string",True, 100.00,["Name","Age","Sex"]]""" myInt, Myfloat, Mybool, Mycomplex all belong to number typePython3 supports int, float, bool, complex (plural). in Python 3, there is only one integer type int, expressed as a long integer, without a long in python2. as in most languages, the assignment and calculation of numeric types is straightforward. Integer (Int)-is often referred to as an integral or integer, is a positive or negative integer, with no decimal points. The Python3 integer is not a restricted size and can be used as a long type, so Python3 has no Python2 long type. floating-point (float)-floating-point types are made up of integral and fractional parts, and floating-point types can also be represented by scientific notation (2.5e2 = 2.5 x 102 =)complex numbers ((complex))-complex numbers are composed of the real part and imaginary part, which can be represented by a + BJ, or complex (a,b), and the real part A and imaginary part B of the complex number are floating-point types. The built-in type () function can be used to query the type of object that the variable refers to, in addition to Isinstance to determine:. Note:type () does not consider a subclass to be a parent class type. Isinstance () considers a subclass to be a parent class type. there is no Boolean in Python2, which uses the number 0 to indicate False and 1 to True. To Python3, True and False are defined as keywords, but their values are 1 and 0, and they can be added to the numbers. MyList point to a list of listsThe list is the most frequently used data type in Python. the list can complete the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting). The list is a comma-separated list of elements written between brackets ([]). as with strings, lists can also be indexed and intercepted, and the list is truncated to return a new list of required elements. The syntax format for the list interception is: variable [header subscript: tail subscript]Note: The index value starts with 0, and 1 is the starting position from the end. The plus sign (+) is a list concatenation operator, and an asterisk (*) is a repeat Operation1. The list is written between square brackets and the elements are separated by commas. 2, as with strings, the list can be indexed and sliced. 3, List can use the + operator for stitching. 4, the elements in the list can be changed. Mytuple point to a tuple typetuples (tuple) are similar to lists, except that the elements of a tuple cannot be modified. The tuples are written in parentheses (), and the elements are separated by commas. the element types in the tuple can also be differenttuples, like strings, can be indexed and the subscript index starts at 0, and 1 is the position that starts at the end. can also be intercepted (see above, here no longer repeat). In fact, you can think of a string as a special tupleNote:1, as with strings, the elements of a tuple cannot be modified. 2, the tuple can also be indexed and sliced, the same method. 3. Note the special syntax rules for constructing tuples that contain 0 or 1 elements. 4, the tuple can also use the + operator for stitching. Set Seta collection (set) is a sequence of unordered, distinct elements. The basic function is to conduct member relationship testing and delete duplicate elements. You can use the curly braces {} or the set () function to create a collection, and note that creating an empty collection must be set () instead of {} because {} is used to create an empty dictionary. The Dictionary (dictionary) is another very useful built-in data type in Python. A list is an ordered collection of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys rather than by offsets. A dictionary is a type of mapping in which the dictionary is identified with "{}", which is an unordered key (key): A value pair collection. keys (key) must use immutable types. in the same dictionary, keys (key) must be unique. """ Print"A="A"; b="B"; c=", c) Print ("myInt =", MyInt,";","myfloat =", Myfloat,"Mybool =", Mybool) # Use the reshape variable to convert to a string for output print ("Reshape variable string operation output:"\ na= "+ STR (a) +"; b="+ STR (b) +"; c="+ str (c)) print ("int type subtraction and remainder operations"A = 1 print ("a+1=", A, end=" ;"A = B + a print ("B+a =", A, end=" ;") c = a-b print ("A-b", C, end=" ;") A = b * a print ("B*a=", A, end=" ;") C = b//a print ("gets an integer similar to the divisible in Java: b//a", C, end=" ;") c = b/a print ("Take a number of floating-point types: b/a=", C, end=" ;") C = b% a print ("Get the rest of the Java-like operation: B%a", c) Print ("To clear objects A, B, C by using the DEL keyword after the operation completes")delA,b,c Print ("Numeric conversion type operation") Print (the Int () function converts a float type to an int type, which is to remove the number following the decimal point, such as a myfloat round integer ., int (myfloat),"\ n", myfloat) Print (the float () function converts a numeric type to float, such as Myint float type., float (myInt)) print ("Complex (x) converts x to a complex number, the real part is x, and the imaginary part is divided into 0. such as Myint to do the operation ", complex (MYINT),"\ n", myInt); Print"Complex (x, Y) converts X and Y to a complex number, the real numbers are divided into X, and the imaginary parts are y. X and y are numeric expressions. For example complex (1,2) = ", complex (1,2)) print ("From the above, the value- type operation is only the output of the corresponding number of values, the actual values do not change") Print ("Digital common function operations Note that you need to use the keyword import to introduce math into the dock.") Print ("exponentiation, for example, computes the 2-second square value of a myint:", myint**2) Print ("Power operation to simplify the Operation function Pow (base, second party)", Math.pow (myint,10)) print ("To find the absolute value function abs (evaluated) For example 10 absolute value", ABS ( -10)) print ("The absolute value of using fabs (evaluated) For example to find absolute value of 16", math.fabs) Print ("To find a number of ceil (evaluated) column as a 4.2 of the integration operation", Math.ceil (4.2)) print ("floor (evaluated) for a number of digits, such as an integer operation", Math.floor (4.2)) print ("Logarithmic function") Print ("log (evaluated) Math.log (MATH.E) returns 1.0,math.log (100,10) back to 2.0\ n", Math.log (Math.pow (math.e,2)),"\ n", Math.log (100,10)) print ("LOG10 (evaluated) for logarithm with a base of 10", MATH.LOG10 (1000)) print ("Max (number of sequences) to find the maximum value", Max (1,-1,30,50,80,80.2)) print ("min (number of sequences) to find the least value", Min ( -1,0-13,80,-100,-102)) print ("MODF () separates the float integer and the decimal number and signs consistent", MATH.MODF (4.0)) print (Rounded round (x [, N]) returns the rounded value of the floating-point number x, such as the value of N, which represents the digits rounded to the decimal point. ", round (4.056,2)) print (the square root sqrt (the evaluated value), for example, the square root of 100., math.sqrt) Print ("Several uses of random functions note the need to use the keyword import to import random into the dock.") Print (The choice usage selects an element randomly from the elements of the sequence, such as Random.choice (range (10)), and randomly selects an integer from 0 to 9. "\
+The Choice () method returns a list, a tuple, or a random item of a string.) Print ("Choice (String) returns random characters", Random.choice ("Hello Chuchu, what is Chuchu doing?" ")) Print ("Choic (list)", Random.choice (myList)) print ("Choice (Tuple)", Random.choice (mytuple)) print ("Choice (number)", Random.choice (range ()) print ("Randrange () returns a random number in the specified ascending cardinality set, with a cardinality default of 1 usage random.randrange ([Start,] stop [, step])"\
+Start-Specifies the starting value in the range, contained within the range. Stop--Specifies the end value in the range that is not included in the range. Step--Specifies the incremental cardinality. ") Print ("with cardinality", Random.randrange ( -1,100,3)) print ("Without cardinality", Random.randrange (0,100)) print ("random () randomly generates random numbers from 0 to 1") Print (Random.random ()) Print ("seed ()") Random.seed print (Random.random ()) Print ("Random sort List") print (myList) random.shuffle (myList) print (myList) print ("uniform () randomly generated [x,y] range of real numbers") Print (Random.uniform (0,100)) #字符串的基本操作 print ("Basic operation of Strings") Print ("Intercept string") print (Mystring[0],"\ ninterception of 0 to 2 ", Mystring[0:3],"\ nthe countdown to the first one., Mystring[-1]) print ("Repeat the output string 2 times:", mystring*2) Print ("To determine if a string contains a string there are two ways in which the keyword in contains, not in representation does not contain") Print ("A" inmyString) Print ("A" not inmyString) Print ("Do not escape strings by adding R or R in front of the example to the right:"+R ' \ n ') Print ("stitching string:", end="") myString ="123"Print (myString) print ("Specify location update string: For example, specify INDEX1 start all string updates for easy Pangzuo is handsome these few characters", end=": ") Print (mystring[:1]+"Easy Pangzuo is handsome")delMyString #列表的基本操作 Print ("Basic Actions for lists") Print ("Output complete list", myList) Print ("Outputs elements of the specified position, such as the elements of the angular mark 0", mylist[0]) print ("outputs the specified position to the specified interval element", Mylist[0:2]) print ("Output all elements after the specified location", mylist[2:]) print ("Output repeat list two times", mylist*2) Print ("Output link List", Mylist+[12,"222","333"])delMyList the operation of the #Tuple (tuple) print ("basic operations for tuples") Print ("Output full tuple", mytuple) Print ("Outputs elements of the specified position, such as the elements of the angular mark 0", mytuple[0]) print ("outputs the specified position to the specified interval element", Mytuple[0:2]) print ("Output all elements after the specified location", mytuple[2:]) print ("Output repeats two times tuples", mytuple*2) Print ("Output link tuples", Mytuple+ (12,"222","333"))delThe basic operation of the Mytuple #Set (set) mysets2={"a","B","C","D","a", 1} print ("The elements that are duplicated in the output collection are automatically removed", mySets2) Print ("There are two ways to judge whether a collection contains an element and the code above," one in, another not in ")if("D" inMYSETS2): Print (True)elif("D" not inMYSETS2):p rint (" not in")Else:p Rint (False) Print ("Difference Set", mysets-mysets2) Print ("and set", mysets|mysets2) Print ("intersect", mysets&mysets2) Print ("elements that do not exist in two collections at the same time", Mysets ^ mySets2)delMysets, MySets2 #Dictionary (dictionary) basic Operation print ("Basic operation of the Dictionary (dictionary)") Print ("Output values that are appropriate", mydictionary["Name"]) Print ("Output full dictionary", mydictionary) Print ("All keys for output dictionaries", Mydictionary.keys ()) Print ("All values of the output dictionary", Mydictionary.values ())
"" "#运行结果展示:
Hello World
No line wrap Output 1 2 3
Use the End keyword for line-wrap output
1 2 3
Output basic Data type
A= 1; b= 1; c= 1
MyInt = 10; Myfloat = 10.2 Mybool = True
Shaping variable-string operation output:
A=1;b=1;c=1
int type subtraction and the operation of the remainder
a+1= 2; b+a = 3; a-b 2; b*a= 3; Get an integer similar to Division in Java: b//a 0; Number of a floating-point type: b/a= 0.3333333333333333; get a similar Java: b%a 1
When the operation completes, the objects of A, B, and C are cleared by using the DEL keyword
numeric conversion type operation
the int () function converts a float type to an int type, which is to remove the number following the decimal point, such as Myfloat. 10
10.2
The float () function converts a numeric type to float such as Myint float type 10.0
Complex (x) converts x to a complex number, the real part is x, and the imaginary part is divided into 0. Such operations as Myint (10+0J)
10
Complex (x, Y) converts X and Y to a complex number, and the real numbers are divided into X, and the imaginary parts are y. X and y are numeric expressions. For example complex (1,2) = (1+2J)
From the above operation, the value of the type operation is only the output of the corresponding number of values to be transferred to the actual data does not change
Number of commonly used function operations Note that you need to use the keyword import to spell math into the dock.
Exponentiation, for example, the calculation of a Myint 2-second square value: 100
Power Operation simplified Operation function POW (base, second side) 10000000000.0
Find the absolute value function abs (evaluated) For example 10 absolute value 10
The absolute value of using fabs (evaluated) For example, to find 16 of 16.0
To find a number of ceil (evaluated) column such as 4.2 of the integration Operation 5
The floor (evaluated) for a number, for example, for an integer operation of 4.2.4
Logarithmic function
Log (evaluated) Math.log (MATH.E) return 1.0,math.log (100,10) return 2.0
2.0
2.0
LOG10 (evaluated) logarithm 3.0 based on a 10 base
Max (number of sequences) ask for the maximum value 80.2
Min (number of sequences) to obtain the minimum value-102
MODF () separates the float value integer and the decimal number and the symbol is consistent (0.0, 4.0)
Rounding round (x [, N]) returns the rounded value of the floating-point number x, given the n value, which represents the digits rounded to the decimal point. 4.06
Square root sqrt (evaluated) For example 100 square root 10.0
Several usages of random functions note that you need to use the keyword import to random the port
The choice usage selects an element randomly from the elements of the sequence, such as Random.choice (range (10)), and randomly selects an integer from 0 to 9. The choice () method returns a list of random entries for a tuple or string
Choice (String) returns random characters you
Choic (list) [' Name ', ' age ', ' sex ']
Choice (Tuple) Tuple
Choice (number) 25
Randrange () returns a random number in the specified incremental cardinality collection, with a cardinality default of 1 usage random.randrange ([Start,] stop [, step]) Start--The specified range of start values, contained within the range. Stop--Specifies the end value in the range that is not included in the range. Step--Specifies the incremental cardinality.
With Cardinal 86
Without Cardinal 80
Random () randomly generates random numbers between 0 and 1
0.6021752380108175
Seed ()
0.5714025946899135
Random Sort List
[1, ' string ', True, 100.0, [' Name ', ' age ', ' sex ']]
[' String ', 1, True, [' Name ', ' age ', ' sex '], 100.0]
Uniform () random generation of real numbers in the [X,y] range
20.609823213950172
Basic operation of strings
Intercepting strings
Self -
0 to 2 of intercept customizations
The first intercept string.
Repeat 2 times output string: custom String Custom string
To determine if a string contains a string there are two ways in which the keyword in contains, the not in representation does not contain
False
True
Do not escape strings by adding R or R to the front as the right example: \ n
Stitching string: Custom string 123
Specify location update string: For example specify INDEX1 start all strings update to easy Pangzuo is handsome these few characters: since Ipanzo is handsome
Basic operation of a list
Output complete list [' String ', 1, True, [' Name ', ' age ', ' sex '], 100.0]
An element string that outputs the element at the specified position, such as the angular label 0
Outputs the specified position to the specified interval element [' String ', 1]
Outputs all elements after the specified position [True, [' Name ', ' age ', ' sex '], 100.0]
Output repeats a list of two times [' String ', 1, True, [' Name ', ' age ', ' sex '], 100.0, ' string ', 1, True, [' Name ', ' age ', ' sex '], 100.0]
Output link list [' String ', 1, True, [' Name ', ' age ', ' sex '], 100.0, 12, ' 222 ', ' 333 ']
Basic operations for tuples
Output complete tuple (1, ' tuple ', 10.0, True)
The element 10 that outputs the specified position, such as angle 0.
Outputs the specified position to the specified interval element (10, 1)
Outputs all elements after the specified position (' tuple ', 10.0, True)
Output repeats two times of tuples (1, ' tuple ', 10.0, True, ten, 1, ' tuple ', 10.0, True)
Output link tuples (1, ' tuple ', 10.0, True, 12, ' 222 ', ' 333 ')
The duplicated elements of the output set are automatically removed {1, ' B ', ' A ', ' d ', ' C '}
There are two ways to judge whether a collection contains an element and the preceding code.
Not in
Difference set {' King ', ' Brush ', ' Tom '}
and set {1, ' brush ', ' B ', ' A ', ' Tom ', ' King ', ' d ', ' C '}
Intersection set ()
Elements {1, ' B ', ' A ', ' brush ', ' Tom ', ' King ', ' d ', ' C '}, not present in two sets
Basic operation of Dictionary (dictionary)
Output corresponding to the values Tom
Output full Dictionary {' name ': ' Tom ', ' age ': ' Sex ': ' Mans '}
All key Dict_keys of the output dictionary ([' Name ', ' age ', ' sex '])
Output Dictionary of all values dict_values ([' Tom ', ', ' Man ']) ""