Public account: You and the cabin by: Peter Editor: Peter
Hello, I’m Peter
This article summarizes six common data-type operations in Python:
- Numbers
- String (String)
- List (List)
- Tuple (Tuple)
- The Dictionary
- Set
Numbers, Numbers
Numbers are common, such as 1,2,100,999, etc., and two common conversion functions: int and float. Common operations on numeric data:
1.1 Arithmetic Operation
The arithmetic operation returns a specific value:
- Add: +
- Cut: –
- Passenger: *
- In addition: /
- Power: * *
- Find the remainder: %
- Dealer: / /
1.2 Comparison Operation
The comparison returns a Boolean value: True or False
- More than: >
- Less than:”
- Is equal to: = =
- Less than or equal to: <=
- Greater than or equal to: >=
- Does not equal: <> or! =
1.3 Common Functions
- Take the absolute value: ABS
- Round: round
- Integer: int
- To float: float
String
Strings are one of the common data types in Python, and you can use the STR function to force other types of data into character types
2.1 Keyboard Input
Anything entered in the terminal through the input function is string data
2.2 Three methods of generation
String data is generated in three ways:
- Single quotes: ‘python’
- Double quotes: “python”
- “””I am learning Python…” “”
2.3 Index and Slice
1. Index:
The index function allows you to view the index value
2. About slices:
- Standard form:
start:stop:step
- Start without tail: contains the start part, but does not contain the stop part
- When slicing, the left side of the index starts at 0 and the right side starts at -1
- Step size can be positive or negative
2.4 Common Functions
- Find the length: len
- Return the maximum value: Max, min
- View the encoding of the character: ord
- Look at the encoding (numeric) corresponding character: CHR
- Member judge: In
- The string repeats: *
- Join: + or join
2.5 Common Operations
- Check whether all letters are: isalpha
- Cutting: the split
- Remove Spaces:
- Strip () : Spaces at both ends
- Lstrip () : left space
- Rstrip () : space on the right
- Case conversion:
- Upper () : uppercase letters
- Lower () : Lower all letters to lowercase
- Isupper () : checks whether all letters are uppercase
- Islower () : checks whether all letters are converted to lower case
- Capitalize () : Capitalize all the first letters
- Title () : uppercase the first letter of all words in the string and lowercase the rest
- Istitle () : checks whether it is in title mode. That is, all words in the string are capitalized and other words are lowercase
- String format
- The placeholder %
- The format function
- f-string
This is a List of books
Lists are arguably the most active data type in Python, with extremely frequent use and many operations:
3.1 create
- Through the list function
- Square brackets
[]
To create a
3.2 Common Functions
- Find the length: len
- Merge multiple lists: +
- Member judge: In
- The list element repeats: *
- Returns the maximum value in the list (comparing ASCII codes) : Max, min
3.3 Common Operations
- Indexing and slicing operations (analogous to strings)
- Append: Appends the whole to the end of the list
- Extend: Each element in the list is merged to form a large list
- Index: Displays the index of an element
- Insert: Inserts elements at the specified location
- Pop: Remove top element (pop top element on the stack)
- Remove: removes the first occurrence of the element; An error is reported if the element does not exist
- Reverse: Reverses the order of the elements in a list; Analogy reversed
- Sort: Sort of list elements; Analogy sorted
4. Tuple
A tuple can be thought of as a special “list” that cannot be modified.
4.1 create
It is created by parentheses () or the tuple function. Even if there is only one element, the element in a tuple must have a comma at the end
t1 = (1.2.3)
t2 = (4.)# single element
t3 = tuple(("python"."go"."html")
Copy the code
4.2 Common Operations
- Find the length: len
- Tuple element repetition: *
- Tuple stitching: +
- View the maximum value: Max, min
- Member judge: In
- Iterate over tuple elements: for loop
- Index and slice
The Dictionary is a Dictionary
Strings, lists, and tuples are all ordered data types, while dictionaries are unordered data types, which are mainly used to store data with certain mapping relationships.
In dictionaries, keys are different, repetitive, immutable data types, and values can be of any data type
5.1 create
There are two ways to create a dictionary:
- through
{}
To create a - Create it using the dict function
d1 = {"name":"Wang"."sex":"male"}
d2 = dict(["name"."Wang"], ["sex"."male"])
Copy the code
5.2 Common Operations
- Value: df1[“name”]
- Df1 [“address”] = “Beijing”
- Delete key-value pairs by key: del df1[“sex”]
- Df1 [“name”] = “red”
- Determines whether the specified key-value pair exists: “birth” in df1
5.3 Common Methods
- Clear: clear
- Get value: get by key
- Update the value of the key-value pair: update
- Delete a key value pair: pop
- Pick a random key-value pair: popItem
- Get value by key (default can be set) : setdefault
- Create a default dictionary (with a value of None, which can be specified) : fromKeys
- Dictionary traversal, the default traversal keys: keys, values, items
A Set of
Collections cannot have repeating elements and can be considered a “hybrid” of lists and dictionaries.
6.1 create
- Create by set (empty collection only)
- through
{}
create - Create frozenset: frozenset
6.2 Common Methods
- Add element: add
- Update collection: update
- Randomly remove element: POP
- To specify element removal (error if element does not exist) : remove
- Specifies the element to be removed (nonexistent, no error reported) : discard
- Clear the collection: clear
6.3 Set Operation
-
Member judge: In
-
Subset and superset: ISSubset, Issuperset
-
Intersection: intersection computes
-
And set: the union
-
Difference set: differencee
-
Symmetric difference set: symmetric_difference