In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the sample analysis of Python variables and data types. I hope you will get something after reading this article. Let's discuss it together.
1.Python variables and data types 1.1Python underlying data types
Integer number
Binary numbers are represented only by the numbers 0 and 1, and in Python, binary integers are represented by the prefix 0b, for example: 0b0110j0b1100.
In addition to the ten digits of 0x9, hexadecimal numbers also use a, b, c, d, e, f. In Python, hexadecimal uses the prefix 0x, such as 0x12efme 0xde2431af.
Floating point number
Binary numbers are represented only by the numbers 0 and 1, and in Python, binary integers are represented by the prefix 0b, for example: 0b0110j0b1100.
In addition to the ten digits of 0x9, hexadecimal numbers also use a, b, c, d, e, f. In Python, hexadecimal uses the prefix 0x, such as 0x12efme 0xde2431af.
In Python, replace 10 with e, for example: 1.23x10 ^ 9 is 1.23 billion, or 12.3e8min0.000012 can be written as 1.2e-5
> 0.1cm 0.20.30000000000000004 > 0.1cm 0.2 = = 0.3False > round (0.1x 0.2pm 1) = = 0.3True >
There is an uncertain Mantissa in the operation between points, not Bug
In a computer, all numbers are represented by binary
Strictly speaking, 53-bit binary represents the digital part.
Because there is no strict equivalence between binary and decimal
So 0.1 is an infinite decimal in binary.
The computer can only intercept 53 bits of it infinitely close to 0.1.
The computer will only display 16 bits.
Round (xmeme d): rounding x d is the number of decimal places
String
In Python, a string is any text enclosed in''or "', such as' abc',"xyz" and so on. Note that''or''is only a representation in itself, not part of a string, so the string 'abc' has only three characters: a _ line _ b _ line _ c.
For example, the first Python program you practiced earlier: print ('Hello World'), where Hello World is a string.
Boolean value
In Python, Boolean values can be represented directly in True and False (note case, there is no need to use string symbols around them), or they can be calculated by Boolean operations.
Boolean values can be calculated with * * and, or, and not * * (note that and,or,not is a keyword of the Python language itself).
The and operation is the and operation, and the result is True only if all operations are True,and.
The or operation is an OR operation, as long as one of them is True,or and the result is True.
The not operation is a non-operation. It is a unary operator that turns True into False,False and True.
Null value
A null value is a special value in Python, represented by None. None is a special null value.
In addition, Python provides a variety of data types such as lists and dictionaries, and allows you to create custom data types
Legal variable name of the method in which the variable is defined by 1.2Python
1. The variable name consists of uppercase and lowercase letters, numbers and underscores
two。 Variables cannot start with a number
3. Try not to coincide with the Python keyword (for example, and, or, not), otherwise the original Python keyword may not work.
The definitions of the following variables are legal.
Num, count, _ none, min_value
They all meet the above three conditions.
The definitions of the following variables are illegal.
1num, 666, 1_cd, and define variables
The way to define a variable is simple, and you can define a variable by using the variable name = data.
For example:
A = 1
In this example, an is a variable whose value is an integer 1.
Hello = 'Hello'
In this example, hello is a variable whose value is a string 'Hello'.
In Python, a variable can store many different types of data one after another.
A = 1 # at this time a stores the integer type print (a) a = 'ABC' # at this time a stores the string type print (a)
This is a unique feature of languages such as Python, which we call dynamic languages, corresponding to static languages. Python, Javascript and so on are all dynamic languages, while Java, C, C++ and so on belong to static languages.
Integers and floating point numbers of 1.3Python
Four operations
Integers and floating-point numbers can perform four operations directly.
# addition num1 = 10num2 = 0.5result = num1 + num2print (result) # = > 10.The subtraction result = num1-num2print (result) # = > 9.binary multiplication result = num1 * num2print (result) # = > 5.binary division result = num1 / num2print (result) # = > 20.0
From the above, we can find a rule that after the operation of integers and floating-point numbers, no matter whether there is a value after the decimal point or not, the results become floating-point numbers, which is reasonable. Floating-point numbers can express the results of integers, but integers cannot express the results of floating-point numbers.
Note: using division in Python2 may get different results from Python3
# python2num1 = 10num2 = 3result = num1 / num2print (result) # = > "python3num1 = 10num2 = 3result = num1 / num2print (result) # = > 3.3333333333333335
You can see that in python2, you get the result of an integer, because when both the divisor and the divisor are integers, the result is saved as an integer by default, which is very unscientific, so in python3, this is improved.
Modular operation
Python numbers support modular operation, using the% sign to indicate modular operation.
Print (3% 2) # = > 1print (33% 10) # = > 3print (99% 30) # = > 9
By using the modular operation properly, we can judge whether a number is even or not. when the modular result of a number to 2 is 0, the number is even, otherwise it is odd.
Print (3% 2) # = > 1 so 3 is odd print (33% 2) # = > 1 so 33 is odd print (100% 2) # = > 0 so 100 is even floor division
Python in addition to ordinary division, there is a special division called floor division, for floor division, the result will ignore the pure decimal part, get the integer part, the floor except for the use / /.
10 Universe 4 # = > 210 Placement 2.5 # = > 4.010 Placement 3 # = > 3 decimal places
When using Python to calculate decimal places, you often need to retain a few places after the decimal point, which can be handled by using the round () function. Here, we first understand how to call round, using two parameters. The first is to retain the number of decimal places, and the second is to retain the number of decimal places.
Num = 10 / 3print (num) # = > 3.333333333333 using round to retain two decimal places round (num, 2) # = > Boolean types and operations of 3.331.4Python
The result is True only if both Boolean values are True.
Or operation
As long as one Boolean is True, the result is True.
Non-operation
Change True to False, or False to True.
Python treats 0, empty strings, and None as False, and other numeric and non-empty strings as True. It is important to note that not takes precedence over and and or.
Short circuit calculation
1. When calculating an and b, if an is False, according to the and algorithm, the whole result must be False, so it returns a; if an is True, the whole result must depend on b, so b is returned.
two。 When calculating an or b, if an is True, according to the OR algorithm, the entire result must be True, so it returns a; if an is False, the entire result must depend on b, so b is returned.
String of 1.5Python
What if the string contains both 'and'?
Some special characters in the string need to be "escaped", and the Python string is escaped with\.
To represent the string Bob said "Isimm OK"
Because 'and' can cause ambiguity, we insert a\ before it to indicate that this is an ordinary character and does not represent the beginning of the string, so the string can be expressed as
'Bob said\ "I\ m OK\".
Note: escape characters\ are not counted in the contents of the string.
Other common escape characters are:
\ nindicates line break
\ t represents a tab
\\ represents the\ character itself.
Raw string and multiline string in 1.6Python
To avoid the hassle of escaping each character, we can prefix the string with r to indicate that it is a raw string and that the characters in it do not need to be escaped.
\ (~ _ ~) /\ (~ _ ~) / 'print (d)
If you want to represent a multiline string, you can use''.'' Indicates:
'' Line 1Line 2Line 3 million dollars'
The representation of the above string is exactly the same as the following:
'Line 1\ nLine 2\ nLine 3'
You can also add r to the multiline string to turn the multiline string into a raw string:
R'''Python is created by "Guido". It is free and easy to learn.Let's start learn Python in imooccluded strings for 1.7 Python string format
You need to use format to process strings and output unfixed content.
The string format consists of two parts, the string template and the template data content. Through the curly braces {}, the template data content can be embedded in the corresponding location of the string template.
# string template template = 'Hello {}' # template data content world = 'World'result = template.format (world) print (result) # = > Hello World
If there are more {} in the template, it is easy to get confused, so you can also specify the order of the template data content during format.
# specify the order template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.' result = template.format ('World',' China', 'Beijing',' imooc') print (result) # = > Hello World, Hello China, Hello Beijing, Hello imooc.# adjust order template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.' result = template.format ('World',' China', 'Beijing') 'imooc') print (result) # = > Hello imooc, Hello Beijing, Hello China, Hello World.
In addition to using the order, you can also specify the corresponding name to make the format process clearer.
# specify the name of {} wHello cpene itemplate = 'Hello {w}, Hello {c}, Hello {b}, Hello {I}.' world = 'World'china =' China'beijing = 'Beijing'imooc =' imooc'# 'template data content result = template.format (w = world, c = china, b = beijing, I = imooc) print (result) # = > Hello World, Hello China, Hello Beijing, Hello imooc.1.8Python
Unicode unifies all languages into one set of codes so that there are no more garbled problems.
Unicode usually uses two bytes to represent a character, the original English code from single-byte to double-byte, only need to fill in all the high bytes as 0.
In python3, Chinese strings are no different from English strings.
String slicing of 1.9Python
A string consists of characters, each of which has a unique position. For example, the string 'ABC',' has the first character A, the second character B, and the third character C.
So we can use position to take out characters at a specific position in a string and access it with brackets [] in the way the position takes the string. At this time, we can think of the string as a list (a new data type. We will continue to learn later), but it is important to note that in the world of the program, counting starts at 0 and uses 0 to represent the first one.
S = 'ABC'a = s [0] # first b = s [1] # second c = s [2] # third print (a) # = > Aprint (b) # = > Bprint (c) # = > C
Sometimes, we want to get a part of the string (substring). At this time, we use the method of slicing. The slice needs to fill in two numbers in square brackets [], separated by a colon to indicate the start position and end position of the substring. And this is a half-closed and half-open interval, excluding the last position.
Ab = s [0:2] # take the first character in the string s to the third character, excluding the third character print (ab) # = = > AB
Let's define a longer string and learn more about slicing.
S = 'ABCDEFGHIJK'abcd = s [0:4] # take the first character to the fifth character in the string s, excluding the fifth character print (abcd) # = > ABCDcdef = s [2:6] # take the third character to the seventh character in the string s, excluding the seventh character print (cdef) # = > CDEF after reading this article, I believe you have some understanding of "sample Analysis of Python variables and data types" If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!
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.