In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Python standard data type example analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python standard data type example analysis" bar!
I. figures
The number types supported in Python3 are:
Integers: such as-1, 0, 1, 888, 0x66, etc.
Floating-point type: such as 1.689, 3.6e-10
Boolean type: a subclass of an integer. True and False correspond to 1 and 0 of the integer.
Plural: for example, 1j, 5-6.66j, etc.
Note: since Python3.6, constant integers are no longer supported. So 45689L is illegal.
In addition, we also need to know some numeric constants, such as numbers with different bases:
Hexadecimal constant: starts with 0x or 0X, such as 0xFF
Octal constant: starts with 0o, such as 00456
Binary constant: starts with 0b, such as 0b1010101
In addition, we also need to be proficient in the operation of numbers, the common conversion functions of numbers, and so on, which will be discussed in a special article later.
II. String
A string is a series of characters. Strings in Python are enclosed in single quotation marks or double quotation marks, with backslashes\ escaping special characters.
For example, the first program we wrote:
Print ("Life is too short, I'll learn Python!")
Among them, the use of "Life is short, I learn Python!" It's just a string.
In the initial string, we need to learn at least two things:
Learn to use some string operators to deal with strings more easily
Learn to use the index to intercept the target segment of a string
(1) operator
1. Use + to concatenate two strings, as follows:
Str1 = "Life is short," str2 = "I learn Python!" print (str1 + str2)
Running result:
2. Use * to copy the current string, as shown below:
Str = "I learn Python!" print (str * 2)
Running result:
(2) string index
The syntax format for the target field of string interception is as follows:
Str [start: end]
It is important to note that the index value always starts at 0, and-1 means at the end.
For example, we want to intercept "Life is too short, I will learn Python!" The first four characters of the
Str = "Life is short, I learn Python!" Print (str [0: 4])
Running result:
Of course, the same result can also be obtained through: str [: 4] or str [- 14:-10].
III. List
List is the most frequently used data type in Python. The data structures of most collection classes can be implemented through lists, so it is critical to learn lists well.
The types of elements in the list can be different, and element values can be modified. It supports numbers, and strings can even contain lists (so-called nesting). A list is a comma-separated list of elements written between square brackets [].
Here are a few simple lists:
List1 = [120,100 "," Python "," Happy ", 78.6] list2 = [Life, 100]
1. Like strings, lists can be concatenated and copied using the + and * operators, respectively.
List1 = [120,100 "Python," Hapi ", 78.6] list2 = [Life", 100] print (list1 + list2) print (list2 * 2)
Running result:
2. like a string, a list can also be indexed and intercepted, and the list is intercepted to return a new list containing the desired elements.
List1 = [120,100 "Python," Hapi ", 78.6] list2 = [Life, 100] print (list1 [2:]) print (list2 [:-1])
The running results are as follows:
In addition, List can also be sliced and supports list [start:step:end] syntax. And List has many built-in methods, such as append (), pop (), and so on, which will be discussed later.
IV. Tuple
A tuple is similar to a list, except that the elements of a tuple cannot be modified. Tuples are written in parentheses (), with elements separated by commas. Element types in tuples can also be different.
Although the elements of tuple are immutable, it can contain mutable objects, such as list lists. It is special to construct tuples with 0 or 1 elements, so there are some additional syntax rules:
Tup1 = () # empty tuple tup2 = (20,) # an element, you need to add a comma after the element
Here is a brief summary of the basic usage of tuples:
Like strings, the elements of tuples cannot be modified.
Tuples can also be indexed and sliced in the same way.
Note the special syntax rules for constructing tuples containing 0 or 1 elements.
Tuples can also be spliced and copied using the + operator.
V. Collection
A set is made up of one or more whole bodies of different sizes, and the things or objects that make up the collection are called elements or members. The collection cannot contain the same elements, so it can be used to delete duplicate elements.
Collections can hold different types of elements, and you can create collections using curly braces {} or the set () function, as follows:
Set1 = {"abc", "python", 12} set2 = set ("abcdefg") print (set1) print (set2)
Running result:
Note: you must use set () instead of {} to create an empty collection, because {} is used to create an empty dictionary.
VI. Dictionary
Dictionary (dictionary) is another very useful built-in data type in Python. A dictionary is a mapping type, identified by {}, which is an unordered collection of keys (key): value. The key must be unique in the same dictionary.
Lists are ordered collections of objects, and dictionaries are unordered collections of objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
Here is a simple example to demonstrate the use of dictionaries:
Dict = {} dict [0] = "Life is short," dict [1] = "I learn Python" dict ["two"] = "!" print (dict) print (dict [0]) print (dict ["two"]) print (dict.keys ()) print (dict.values ())
Running result:
Thank you for your reading, the above is the content of "example Analysis of Python Standard data types". After the study of this article, I believe you have a deeper understanding of the problem of example analysis of Python standard data types, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.