In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what is the method of Python input and output and high-level assignment". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of Python input and output and high-order assignment?"
1. Input, output, and comments 1.1 get user input
Programs often need to interact with the user to obtain the data submitted by the user. Python provides the input function, which accepts user input data and returns a reference to a string.
The input function takes a string as an argument, which is used as text to prompt the user for input, so it is also called a prompt string:
> number = input ('Enter the number of students:') Enter the number of students: 52 > > number'52'
Execute the first line number = input ('Enter the number of students:') in the interactive interpreter, which prints the string "Enter the number of students:" and prompts the user for the appropriate information. Enter 52 here and press enter to get the user's input after the prompt string, which is stored in the number variable. It is important to note that the value returned by the input function is a string type, and if you need to convert this string to another type, you must provide the appropriate type conversion to perform the desired operation:
> score = input ('Enter the total score:') Enter the total score: 4396 > number = input ('Enter the number of students:') Enter the number of students: 52 > average_score = int (score) / int (number) > average_score84.538461538461531.2 formatting output 1.2.1 basic method
We have seen the print function more than once in the above example, which provides a very easy way to print Python output. It accepts zero or more parameters, and by default uses a single space as the delimiter to display the result, which can be modified with the optional parameter sep. In addition, by default, each print ends with a newline character, which can be changed by setting the parameter end:
> print ('Data',' Structure', 'and',' Algorithms') Data Structure and Algorithms > print ('Data',' Structure', 'and',' Algorithms', sep='-') Data-Structure-and-Algorithms > print ('Data',' Structure', 'and',' Algorithms', sep='-', endurance examples)
A format string is a template that contains words or spaces that remain unchanged, as well as placeholders for variables that are inserted later. Using formatted strings, you can change depending on the value of the runtime variable:
Print ("The price of s is d yuan."% (fruit, price))
% is a string operator, which is called the format operator. The left part of the expression is the template (also known as the format string), and the right part is a series of values used to format the string, and the number of values on the right is the same as the number of% in the format string. These values are swapped into the format string from left to right.
The format string can contain one or more conversion declarations. The conversion character tells the format operator what type of value will be inserted into the appropriate position in the string. In the above example,% s declares a string and% d declares an integer.
You can add a format modifier between% and format characters to implement a more complex output format:
> > print ("The price of% s is d yuan."% ('apple', fruits [' apple']) The price of apple is 5 yuan. > print ("The price of% s is d yuan."% ('apple', fruits [' apple']) The price of apple is 5 yuan. > print ("The price of% s is% + 10d yuan."% ('apple') Fruits ['apple']) The price of apple is + 5 yuan. > > print ("The price of% s is%-10d yuan."% (' apple', fruits ['apple']) The price of apple is 5 yuan. > print ("The price of% s is .3f yuan."% (' apple') Fruits ['apple']) The price of apple is 5.000 yuan. > > print ("The price of apple is% (apple) f yuan."% fruits) The price of apple is 5.000000 yuan.1.2.2 format format function
Although the above approach can still be used, another recommended solution is the template string format, which aims to simplify the basic formatting mechanism and combines and strengthens the advantages of the previous approach. When using the format formatting function, each replacement field is enclosed in curly braces, which can contain the variable name, or the replacement field can have no name or use the index as the name::
> > "The price of {} is {} yuan." .format ('apple', 5.0)' The price of apple is 5.0yuan.' > "The price of {fruit} is {price} yuan." .format (fruit='apple', price=price) 'The price of apple is 5.0yuan.' > "The price of {1} is {0} yuan." .format (5.0,' apple')' The price of apple is 5.0yuan.'
As you can see from the above example, the order of indexes and variable names does not matter. In addition, the format specifier (similar to the% operator) is used by combining the colon:
> value = 2.718281828459045 >'{} is approximately {: .2f} '.format (' eBay, value)'e is approximately 2.72'> >'{} is approximately {: + .2f} '.format (' eBay, value)'e is approximately + 2.72'> >'{} is approximately {: 0 > 10.2f} '.format (' e') Value)'e is approximately 00002.72'> > {} is approximately {: 0 > number = 1 > > number + = 4 > print (number) 5 > > number / / = 2 > print (number) 2 > > number * * = 2 > print (number) 4 > > string_1 = 'Hellostones' > > string_1 * = 2 > print (string_1)
This assignment can be used not only for numeric data, but also for other data types (as long as the data type supports the binocular operator used).
2.2 parallel assignment
In addition to assigning values one by one, multiple variables can be assigned simultaneously (in parallel):
> a, b, c, d = 0,1,2,3 > print (a, b, c, d) 0 1 2 3
In this way, you can simply exchange the values of multiple variables:
> b, c = c, b > print (a, b, c, d) 021 32.3 sequence unpacking
Sequence unpacking is to unpack an iterable object and store the resulting value in a series of variables, but the sequence to be unpackaged must contain the same number of elements as the variables listed to the left of the equal sign, otherwise an exception will be thrown:
> > fruit, price = ['apple', 5. 0] > print (fruit) apple > print (price) 5 > > fruit, price, date = (' apple', 5. 0) Traceback (most recent call last): File ", line 1, in ValueError: not enough values to unpack (expected 3, got 2) > > fruit, price = ('apple', 5. 0,' 2021-11-11) Traceback (most recent call last): File", line 1, in ValueError: too many values to unpack (expected 2)
To avoid exception triggering, you can use the asterisk operator * to collect excess values, so there is no need to ensure that the number of values and variables is the same, and the right side of the assignment statement can be any type of sequence. but variables with asterisks always end up with a list:
> > fruits = ['apple',' orange', 'lemon'] > > fruit_a, * rest = fruits > print (rest) [' orange', 'lemon'] > > fruits_a, * rest, fruits_b = fruits > > print (rest) [' orange'] > fruits_a, fruits_b, fruits_c, * rest = fruits > > print (rest) [] 2.4 chain assignment
Chained assignments can associate multiple variables to the same value:
Var_1 = var_2 = value
Equivalent to:
Var_1 = valuevar_2 = var_1 so far, I believe you have a deeper understanding of "what is the method of Python input and output and high-order assignment". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.