python basic tutorial details five data types

  • 2020-05-24 05:43:12
  • OfStack

Python 5 data types

In the process of learning a language, the first step must be to get in touch with the data types it has. Python has five main data types. The following is my understanding and ideas on these five data types.

Number 1.

There are four main types of Numbers in Python: int (integer), float (floating point), long (long integer), and complex (plural).

The main special feature is that there is a function round() for Numbers of type float: round(a,b): round(a,b) for Numbers of type float: a for Numbers of type float, b significant digits after the decimal point, 4 rounded 5, default to 1.

The complex type is also special.

2. The string

For example, s= 'string' s=' string' s1=' string', these three effects are 1, in Python, quotation marks, double quotation marks, and 3 quotation marks are the correct use of a string.

Strings in Python can be added directly: s+s1 # returns a new string 'stringstring'

Now we can slice a string, which is the same thing as slicing a loaf of bread. For example, if we want to take the second to fifth character in the s string, which is more troublesome in other languages, we can easily do this in Python.

Example: s[a:b:c] a indicates the starting position of the slice, index from left to right when 0 or positive (default starts at 0), index from right to left when negative (default starts at -1)
b refers to the position where the slice ends, but does not include the position where the slice ends. By default, by default until the end of the index.
c represents the step size, which defaults to 1 and is truncated from right to left when negative.

Normal index operation without the colon: s[0] #s

By default, c is 1: s[1:5] #trin.
s[-3::] : start with the 3rd character on the right (there is no 0th character!!). , intercept to the right until you're done #ing
s[-3::-1]: from the third character on the right, intercept to the left, b default: until the end of #irts

Now that we have an understanding of the simple slicing operation, let's talk about a few common functions (there are actually a lot of operation functions, but some of them are not often used, you can learn more if you need to).

len() : returns the length of the string. len(s) #pytnon is not the same as C. You do not need to add 1 to the length of the string. So this is 6.

replace(a,b) : replace a string with b string.

3. List (List)

Direct example: s=[' string','python',2001,52.5], s1=[2002,5658]

Python contains the List type in square brackets, which can contain the string type and the number type, separated by a comma.

The access operation in List: s[1] # returns an python string. This is analogous to the string type

List also has update and delete operations: the first element in s[1]=2002 # list s (starting at 0) 'python' string is replaced with 2002.

The first element of del s[1] # list s has been deleted

Briefly introduce the functions and methods of several operations:

1. append() # appends the element after the list
2. extend() example: a.extend (b) # adds an element from the b list to the end of the a list
3. pop() # brings up the last element of the list

1. sort() # sorts the list, but seems to specify the rules for sorting itself.
2. count() # counts the number of times an element appears
3. index() # is the element of i at the index

4. Tuples (Tuple)

Example: s=('string','python',2001) s1='string','python',2001
The tuples in python are interesting. It is correct to put parentheses on them or not. Separated by a comma, the default is a tuple.

Tuples have a special rule: elements in tuples are not allowed to be modified.

Access operations can also be similar to the string type.

Examples of tuples and lists are given below:

1, (1,2,3)+(1,2,3) #(1,2,3,1,2,3) add
2, [1]*3 #[1,1,1] times operation
3, 1 in [1,2,3] #true judgment operation
4. for i in (1,2,3)
print i #1
#2
Loop #3

5. Dictionary (Dictionary)

Above example: dict={'abc': 123, 'ji': 'kp', (1,2):
5}

For typical key-value type data, note that the value of key must be only 1, but the value of value may not be only 1. Use curly braces for inclusion. Close the curly braces with a semicolon.

Access: dict['abc'] #123
Modify: dict['abc']=153 # modify 'abc' corresponding to 123
Delete: deldict [' abc]

Special point:

1. The same key cannot appear twice. In case of multiple assignments, the one that appears later shall prevail
2, the key must be immutable, available Numbers, strings, tuples act as, but not a list!

Method introduction:

1, clear() # clear dictionary
2. get() # example: get('abc') # returns 123 get('ashudya ') # returns none
3. keys() # returns a list of all the keys in the dictionary.
4. value()# returns a list of all the value values in the dictionary.
5, fromkeys() # put a list in the dictionary as key.

fromkeys ([1, 2, 3], 0) # 0 for value

The dictionary can be expressed as: {1:[0],2:[0],3:[0]}

Five data types are the cornerstone of learning Python, which is not very difficult to master. Start with the simple ones, and further study if necessary!

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: