Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the six basic data types and common functions of python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the six basic data types and common functions of python. It is very detailed and has certain reference value. If you are interested, you must finish reading it.

Number (digital)

Four basic types: int (integer), float (floating point), bool, and complex (plural type), which are used to store values

Type conversion: int (), float ()...

The built-in function type () is used to query the type of variable

Mathematical operations: +, -, * are all the same as C language / do division (the result must be a floating point number) / / do division (the result only retains the integer part, remove the decimal part)% means to do the remainder operation * * means to do the power operation (pay attention to the order of operation)

The function name function commonly used in the Number type explains that the round (x [, n]) x to the y power of pow (x, n) x floating-point number 4 rounding n retains the decimal place max (parameter 1, parameter 2, parameter n) the maximum parameter that returns a given parameter can be the sequence min (parameter 1). Parameter 2, parameter n) returns the minimum value of a given parameter. The parameter can carry an integer math.floor (float) for the sequence math.ceil (float), pick an integer random number, import random function name function description choice () returns a list. The random item randrange (start,end,step) start- of a tuple or string the start value within the specified range includes the end value within the range specified by end- and does not include the incremented cardinality range specified by step- (start,end Step) the start value within the specified range of start- contains the end value within the specified range of end- and does not contain the cardinality of the specified increment of step- (list) returns a randomly disturbed list acting on the list itself randint (start,end) returns a random integer containing start and end itself the value function name function description choice () returns a list The random item randrange (start,end,step) start- of a tuple or string the start value within the specified range includes the end value within the range specified by end- and does not include the incremented cardinality range specified by step- (start,end Step) the start value within the specified range of start- contains the end value within the specified range of end- and does not contain the cardinality of the specified increment of step- (list) returns a randomly disturbed list acting on the list itself randint (start,end) returns a random integer containing the values of start and end itself string (string)

Strings need to be enclosed in single quotation marks''or double quotation marks ""-comment strings are also a special tuple. Cannot change the value of an element in a string

String type operation: + concatenate multiple strings * repeat output string n times the string name [:] intercepts part of the string (the leftmost index value is 0, the rightmost index value is-1, and the penultimate index value is-2. ) string name [] get a single character in a string by index in/not in to determine whether a single character is / not in a string rUnip R treats special characters such as escape characters as ordinary characters

Basic operations: indexing, slicing, multiplication-multiple output, membership check, length len (), maximum, minimum

The description of the function name function commonly used in strings isnumeric () determines whether it is a numeric string (only a pure numeric string cannot contain other characters) True Falsejoin () uses the specified string as a delimiter before merging into a new string center (width,fillchar) uses the original string as the center to fill the fixed width widthljust (width) Fillchar) returns a left alignment of a string using fillchar to fill to a fixed width widthrjust (width,fillchar) returns a right alignment of a string using fillchar to fill to a fixed width widthlstrip () / rstrip () / strip () removes specified characters on the left and right sides of the string or removes white space characters chr () by default converts an integer to the corresponding character ord () returns an integer (ASCII value) replace (old) New [, max]) replaces one character with another in a string max does not write to replace all, otherwise the maximum number of replacements is max times find (str Beg=0) find the location of a character by default from the first bit, return the index value, not return-1rfind (str) find the location of a character, by default, return the index value from the last bit, not return-1split (sep [ Maxsplit]) split the current string with a character return list upper () change the lowercase letters in the string to uppercase splitlines ([True]) to split the newline character True optional whether to keep the newline character list (list)

List data items can be of different types

Each element of list can be changed.

List uses [] square brackets to contain individual data items

The List list type is the most frequently used data type in Python

Syntax: a list is a list of elements separated by commas in the middle of []

Value syntax format: list [header subscript: tail subscript: step size]

Multiple lists are merged into one list * the list is output repeatedly in whether an item is in the list

List items can be added, modified or deleted

List function name function description shallow copy of copy () list (can only copy the first-tier list multi-tier list uses references) copy.deepcopy () Deep copy function list.count () calculates the number of times a value appears in the list list.count () calculates the number of times a value appears in the list list.append () appends a value list.pop ([I]) to the last in the list A value pops up and returns that if given an index value I will pop up the corresponding value of the index list.extend () like append multiple values list.insert (index) to the list Obj) insert a new value before the value of an index list.remove () remove the first match of a value in the list reverse list.clear () list reverse list.clear () empty list list.sort () list sort set (collection)

Set is a sequence of unordered non-repeating elements

Create a collection using curly braces {} or the set () function

Create an empty collection with set ()

The weight can be removed by using set

A set is an unordered sequence that is not repeated.

Basic functions: conduct member testing and remove repetitive elements

Definition: use curly braces {} to define a collection or a set definition collection

Note: defining an empty collection requires the use of the set () type conversion function, otherwise the defined empty dictionary

Tuple (tuple)

Tuple uses () parentheses to contain individual data items

The only difference between tuple and list is that the elements of tuple cannot be modified, while those of list can be modified.

Tuple definition: elements are written between () and multiple elements are separated by commas

MyTuple = (element 1, element 2...)

Tuples operate like strings

Note: you need to add a comma when the tuple is a value

Dictionary (dictionary)

Each element of the dictionary is a key-value pair, an unordered collection of objects

The dictionary is a variable container model and can store objects of any type.

Dictionaries can be referenced by keys, which must be unique and the key name must be immutable (that is, the key name must be one of the three types of Number, String, tuple), but the value does not have to

The dictionary uses {} curly braces to contain key-value pairs

Create an empty dictionary using {}

Dictionaries often use function name functions to explain that myDict.items () returns a list of key-value pairs that can be traversed. Dict () can construct a paired list into a dictionary myDict.clear () empty dictionary myDict.keys () get all the keys myDict.values () get all values myDict.pop (key) pop-up value myDict.popitem () delete the last key-value pair in the dictionary copy () shallow copy copy.deepcopy () deeply copy the description of the function name function int (x) converts x to an integer float () to floating-point bool () to boolstr () to a string dict () to a dictionary list () to a list tuple () to a tuple set () to a collection eval (str) to evaluate a valid expression in a string (its own type is returned) above are six basic data All the contents of the article "what are the types and common functions?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report