In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the Boolean operation methods of Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Python supports Boolean data. Boolean types have only two values, True and False, but Boolean types have the following operations.
1. And operation: the result is True only if both Boolean values are True.
Example:
True and True # = > TrueTrue and False # = > FalseFalse and True # = > FalseFalse and False # = = > False
2. Or operation: as long as there is a Boolean value of True, the result is True.
Example:
True or True # = > TrueTrue or False # = > TrueFalse or True # = > TrueFalse or False # = = > False
3. Non-arithmetic: change True to False, or False to True.
Example:
Not True # = > Falsenot False # = = > True
Boolean operations are used in computers to make conditional judgments. Depending on the result of the operation as True or False, the computer can automatically execute different subsequent codes.
In Python, Boolean types can also do and, or, and not operations with other data types.
Example:
# Boolean type a = Trueprint (an and'averse T'or 'astatF')
The result of the run is as follows.
Axit
The result is not a Boolean, but the string aquart. Why? Because Python treats 0, empty strings, and None as False, and other numeric and non-empty strings as True, True and 'axiT' evaluates to 'axiT'. Continue to calculate 'averse T' or' a calculated Fitness, so the result is still 'averse T'.
To explain the above results, it also involves an important rule of and and or operation: short circuit operation. The short-circuit operator means that expressions around the operator are evaluated only when they need to be evaluated. For example, x or y Python evaluates from left to right, first tests the truth of the expression x. If the expression x is true, then according to the characteristics of the or operator, no matter what the bool result of the y expression is, the result of the operator is the expression x, and the expression y will not be evaluated.
When calculating an and b, if an is False, according to the and algorithm, the result must be False, so it returns a; if an is True, the whole result must depend on b, so it returns b.
When calculating an or b, if an is True, according to the OR algorithm, the result must be True, so it returns a; if an is False, the result must depend on b, so it returns b.
So when the Python interpreter does the Boolean operation, as long as it can determine the calculation result in advance, it will not calculate it later and return the result directly.
String type
What is a string?
A string is a series of characters. In Python, the contents of single, double, or triple quotes are strings. If the string includes single or double quotation marks, you can use "\" to escape the characters in the string.
Example:
# the text inside the single quotation marks is the string'I am a boy'# double quotation marks are actually the same as the single quotation marks. Generally, it is recommended to use the single quotation mark "Welcome to join the Python actual combat circle" # the string represented by the three quotation marks is usually a very long text # three quotes are usually used to write text notes. The first project in our actual combat circle is "how to get started with Python 7 days". We will arrange learning content every day. It only takes 40 minutes to finish your homework and submit it to "knowledge Planet". At the beginning of the knowledge Planet, slow down some plans to update the content every three days. I hope you can participate in the'# change string (\ n) command = 'Let\' s goggling print ('\ noutput using escape characters:', command)
The result of the run is as follows.
Use escape character output: Let's go!
The basic use of strings
1. Add Blank
In programming, a certain amount of blank output is for ease of reading. The common ways to add white space in Python are tabs (\ t), spaces, or newline characters (\ n). Tabs mean to output text two spaces blank.
Example:
# add blank # Tabs can be combined with print ("Welcome to Python,\ n") print ('\ t what aspects of Python you want to learn, please leave a message.')
The result of the run is as follows.
Welcome to the Python practical circle. What aspects of Python do you want to learn? please leave a message.
2. Concatenate strings
To concatenate a string is to merge two or more strings together. This operation is often used in projects, such as crawlers, the regular expression of the web page (described later) is too long and can be concatenated by splicing, or the strings of two variables can be concatenated into one, and so on. Python uses the plus sign (+) to concatenate strings.
Example:
# concatenation string log_1_str = 'The error is a bug.'log_2_str =' We should fix it.'log_str = log_1_str + log_2_strprint ('\ nThe concatenated string is:', log_str)
The result of the run is as follows.
The concatenated string is The error is a bug. We should fix it.
Common operation of string
1. Modify the case of a string
The two nouns you will often hear in Python are function and method. A function is a separate block of code that can accomplish a specific task alone and can be called; a method is a noun used in an object-oriented programming language. Python is an object-oriented programming language, object-oriented is everything is an object, such as you, I, he, collectively referred to as people, person is an object. People can run (run), running is one way, and together it is people.run ().
Example:
# string case conversion welcome = 'Hello, welcome to Python practical circle'# title (), the first letter of each word is capitalized print ('\ nThe first letter of each word is capitalized:', welcome.title ()) # capitalize (), the first letter of the paragraph is uppercase print (the first letter of the paragraph:', welcome.capitalize ()) # lower (), all letters lowercase print ('\ nAll letters lowercase:', welcome.lower ()) # upper () All letters uppercase print ('\ nAll letters uppercase:', welcome.upper ()) # uppercase to lowercase, lowercase to uppercase print ('\ nuppercase to lowercase, lowercase to uppercase:', welcome.swapcase ()) # String.isalnum (), determine whether the string is all numeric or English, return True if it matches, and False if it contains special characters such as symbols or spaces Then Falseprint ('\ njudge whether the string is all numeric or English:', welcome.isalnum ()) # String.isdigit (), determine whether all the strings are integers print ('\ n determine whether the strings are all integers:', welcome.isdigit ())
The result of the run is as follows.
Uppercase: Hello, Welcome To Python Practical Circle paragraph uppercase: Hello, welcome to python practical circle all letters lowercase: hello, welcome to python practical circle all letters uppercase: HELLO, WELCOME TO PYTHON PRACTICAL CIRCLE uppercase to lowercase, lowercase to uppercase: hELLO, WELCOME TO pYTHON PRACTICAL CIRCLE determines whether the string is all numeric or English: False determines whether the string is all integers: False
2. Delete the white space at both ends of the string
Removing whitespace at both ends of a string is often used in data cleaning. A common operation is to remove spaces at both ends or at one end.
Example:
# delete white space love_Python = 'Hello, Python Practical Circle' # delete blank print at both ends of the string ('delete white space on both ends of the string', love_Python.strip ()) # delete blank print on the right side of the string ('delete white space on the right side of the string', love_Python.rstrip ()) # delete the blank print on the left side of the string ('delete the white space on the left side of the string', love_Python.lstrip ())
The result of the run is as follows.
Delete blank Hello at both ends of the string, Python Practical Circle delete blank Hello on the right side of the string, and Python Practical Circle delete blank Hello, Python Practical Circle on the left side of the string
3. Other precautions
There are many string operations in Python, and only some of the common operations are listed above. It is important to note that strings in Python do not allow values to be modified, only overrides. That is, strings can only be reassigned.
Slicing of a string
Slice operation is a frequently used operation in Python. The slicing of a string is to get a substring (part of a string) from a string. We use a square bracket, the start offset (start), the end offset (end), and the optional step size (step) to define a slice.
Syntax: [start:end:step] [:] extract the entire string from the beginning (default position 0) to the end (default position-1) [start:] from start to end [: end] from beginning to end- 1 [start:end] from start to end- 1 [start:end:step] from start to end-1 For every step character, the position / offset of the first character on the left is 0, and the position / offset of the last character on the right is-1
Example:
# string slice word = 'Python'print (word [1:2]) print (word [- 2:]) print (word [:: 2]) print (word [::-1])
The result of the run is as follows.
YonPtonohtyP
Conversion between various types
In Python, various data types can be converted to each other, and you can use the type () function to see the type of a variable.
Syntax: type (variable name) is used to view the data type of the variable
The type () function is often used in real-world projects, because only if you know what type the variable is, for example, the dictionary type and the list type have different operations. Type conversion is also often used in project practice, for example, the monthly sales of a supermarket is a character type, which can be converted to a numeric type, such as calculating the average, and the specific conversion syntax is shown below.
Syntax:
Float (a) converts variable a to floating point number int (b) converts variable b to integer str (c) converts variable c to string where a, b, c are any variable type
Example:
'' conversion between various data types''print ('\ nconversion of various numeric types') number = 10 conversion number the data type is an integer Print is represented by int (the data type of 'number is:') print (type (number)) # converts an integer to a floating point float_number = float (number) print (the data type of'\ nfloat_number is:') print (type (float_number)) # converts an integer to a string print ('\ nnumber to string type') str_number = str (number) print (the data type of 'str_number is:') print (type (str_number)) # convert strings to integer int () or floating point float () print ('\ nstr_number convert to numeric type') int_str_number = int (str_number) float_str_number = float (str_number) print ('int_str_number data type is:') print (type (int_str_number)) print ('float_str_number data type is:') print (type (float_str_number))
The result of the run is as follows.
The data type of each numeric type conversion number is: the data type of float_number is: the data type of number is converted into string type str_number is: the data type of str_number is converted into numeric type int_str_number is: the data type of float_str_number is: "what is the Boolean type operation method of Python" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.