In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge points of Python variable type analysis using examples. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Foreword:
The value of the variable stored in memory, which means that a space is opened up in memory when the variable is created.
Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.
Therefore, variables can specify different data types, and these variables can store integers, decimals, or characters.
1. Variable assignment
Variable assignments in Python do not require a type declaration.
Each variable is created in memory and includes information about the identity, name, and data of the variable.
Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
The equal sign = is used to assign values to variables.
To the left of the equal sign = operator is a variable name, and to the right of the equal sign = operator is the value stored in the variable. For example:
Instance (Python 2.0 +)
Counter = 100 # assign integer variable miles = 1000.0 # floating point name = "John" # string print counterprint milesprint name
Running result:
one hundred
1000.0
John
In the above example, 100mem1000.0 and "John" are assigned to the counter,miles,name variable, respectively.
two。 Multiple variable assignment
Python allows you to assign values to multiple variables at the same time.
For example:
A = b = c = 1
In the above example, an integer object with a value of 1 is created, and three variables are allocated to the same memory space.
You can also specify multiple variables for multiple objects.
For example:
A, b, c = 1,2, "john"
In the above example, two integer objects 1 and 2 are assigned to variables an and b, and the string object "john" is assigned to variable c.
3. Standard data type
There can be many types of data stored in memory.
For example, a person's age can be stored in numbers, and his name can be stored in characters.
Python defines some standard types for storing various types of data.
Python has five standard data types:
Numbers (digital)
String (string)
List (list)
Tuple (tuple)
Dictionary (dictionary)
4.Python digit
Numeric data types are used to store numeric values.
They are immutable data types, which means that changing the digital data type allocates a new object.
When you specify a value, the Number object is created:
Var1 = 1var2 = 10
You can also use the de statement to remove references to some objects.
The syntax of the del statement is:
Del var1 [, var2 [, var3 [...., varN]
We can delete references to single or multiple objects by using the de statement. For example:
Del vardel var_a, var_b
Python supports four different number types:
Int (signed integer)
Long (long integer, which can also represent octal and hexadecimal)
Float (floating point)
Complex (plural)
Examples of some numeric types:
Intlongfloatcomplex1051924361L0.03.14j100-0x19323L15.2045.j-7860122L-21.99.322e-36j0800xDEFABCECBDAECBFBAEl32.3e+18.876j-0490535633629843L-90.-.6545+0J-0x260-052318172735L-32.54e1003e+26J0x69-4721885298529L70.2E-124.53e-7j
Long integers can also use lowercase l, but it is recommended that you use uppercase L to avoid confusion with the number 1. Python uses L to display long integers.
Python also supports complex numbers, which are made up of real and imaginary parts, which can be represented by a + bj, or complex (aforce b). Both the real part an and the imaginary part b of the complex number are floating point.
Note: the long type only exists in the Python2.X version, and in versions later than 2.2, the int type data is automatically converted to the long type when the data is overflowed. In the Python3.X version, the long type is removed and int is used instead.
5.Python string
A string or string (String) is a string of characters consisting of numbers, letters, and underscores.
It is generally recorded as follows:
S = "a1a2 ·an" # n > = 0
It is the data type that represents text in a programming language.
The string list of python has two values:
The default index from left to right starts at 0, and the maximum range is 1 less than the length of the string.
The index from right to left starts with default-1, and the maximum range is at the beginning of the string.
If you want to get a substring from a string, you can use [header subscript: trailing subscript] to intercept the corresponding string, where the subscript is calculated from 0 and can be positive or negative, and the subscript can be empty to the beginning or tail.
The substring obtained by [header subscript: trailing subscript] contains the characters of the header subscript, but not the characters of the trailing subscript.
For example:
> s = 'abcdef' > s [1:5]' bcde'
When using a colon-delimited string, python returns a new object that contains contiguous content identified by this pair of offsets, with the start on the left containing the lower boundary.
The above result contains the value b of s [1], and the maximum range does not include the tail subscript, which is the value f of s [5].
The plus sign (+) is the string concatenation operator, and the asterisk (*) is a repetitive operation.
The following is an example:
Str = 'Hello Worldwide' Print str # output the first character in the full string print str [0] # output string print str [2:5] # string between the third and sixth in the output string print str [2:] # output string print str * 2 # output string twice print str + "TEST" # output concatenation
The output result of the above example:
Hello World!
H
Llo
Llo World!
Hello World!Hello World!
Hello World!TEST
Ython list interception can receive the third parameter, which acts as the intercept step. The following example intercepts the string at the position from index 1 to index 4 and sets the step size to 2 (one position interval):
6.Python list
List (list) is the most frequently used data type in Python.
Lists can complete the data structure implementation of most collection classes. It supports characters, numbers, strings and even lists (that is, nesting).
The list is identified by [] and is the most common compound data type in python.
The cutting of the values in the list can also use the variable [header subscript: trailing subscript] to intercept the corresponding list, starting with the default 0 of the left-to-right index and-1 from the right-left index, and the subscript can be empty to indicate the beginning or tail.
The plus sign + is the list concatenation operator and the asterisk * is a repetitive operation.
The following is an example:
List = ['runoob', 786,2.23,' john', 70.2] tinylist = [123 'john'] print list # output full list print list [0] # first element of output list print list [1:3] # output second to third element print list [2:] # output all elements from the third to the end of the list print tinylist * 2 # output list twice print list + tinylist # print combination list
The output result of the above example:
['runoob', 786,2.23,' john', 70.2]
Runoob
[786, 2.23]
[2.23, 'john', 70.2]
[123,' john', 123, 'john']
['runoob', 786,2.23,' john', 70.2,123, 'john']
7.ython tuple
A tuple is another data type, similar to List (list).
Tuples are identified by (). Internal elements are separated by commas. But tuples cannot be assigned twice, which is equivalent to a read-only list.
Tuple = ('runoob', 786,2.23,' john', 70.2) tinytuple = (123) 'john') print tuple # output complete tuple print tuple [0] # output first element of tuple print tuple [1:3] # output second to fourth (excluding) element print tuple [2:] # output all elements from the third to the end of the list print tinytuple * 2 # output element Group two tuples of print tuple + tinytuple # print combination
The output result of the above example:
('runoob', 786,2.23,' john', 70.2) runoob (786,2.23) (2.23, 'john', 70.2) (123,' john', 123,' john') (' runoob', 786,2.23, 'john', 70.2,123,' john')
The tuple is invalid because updates to tuples are not allowed. The list is allowed to be updated:
Tuple = ('runoob', 786,2.23,' john', 70.2) list = [' runoob', 786,2.23,' john', 70.2] tuple [2] = 1000 # illegal application in tuple list [2] = 1000 # legal application 8..Python dictionary in list
Dictionaries (dictionary) are the most flexible built-in data structure types in python besides lists. 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.
Dictionaries are identified with "{}". The dictionary consists of an index (key) and its corresponding value value.
Dict = {} dict ['one'] = "This is one" dict [2] = "This is two" tinydict = {' name': 'runoob','code':6734 'dept':' sales'} print dict ['one'] # output key is' one' value print dict [2] # output key value 2 print tinydict # output complete dictionary print tinydict.keys () # output all keys print tinydict.values () # output all values
The output is as follows:
This is one
This is two
{'dept':' sales', 'code': 6734,' name': 'runoob'}
['dept',' code', 'name']
['sales', 6734,' runoob']
9.Python data type conversion
Sometimes, we need to convert the built-in type of the data, the conversion of the data type, you just need to use the data type as the function name.
The following built-in functions perform conversions between data types. These functions return a new object that represents the converted value.
Function description
Int (x [, base])
Convert x to an integer
Long (x [, base])
Convert x to a long integer
Float (x)
Convert x to a floating point number
Complex (real [, imag])
Create a plural
Str (x)
Convert object x to a string
Repr (x)
Convert object x to an expression string
Eval (str)
Used to evaluate a valid Python expression in a string and return an object
Tuple (s)
Convert sequence s to a tuple
List (s)
Convert sequence s to a list
Set (s)
Convert to a variable set
Dict (d)
Create a dictionary. D must be a sequence (key,value) tuple.
Frozenset (s)
Convert to an immutable set
Chr (x)
Convert an integer to a character
Unichr (x)
Convert an integer to a Unicode character
Ord (x)
Convert a character to its integer value
Hex (x)
Convert an integer to a hexadecimal string
Oct (x)
Convert an integer to an octal string
The above is all the content of the article "Python variable type use example analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
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.