Python core programming version 2, 36th page, Chapter 2 exercises-answers to Python core programming-self-developed-

Source: Internet
Author: User
Tags floor division

Python core programming version 2, 36th page, Chapter 2 exercises
The answer here is not from official resources, but from my own exercises, which may be incorrect.

2.21 exercise

2-1.
Variables, print, and string formatting operators. Start the interactive interpreter, assign values to some variables (string, value, etc.), and display their values by entering the variable name. Use the print statement to do the same thing. What is the difference between the two? Also try to use the character string format operator % and do it several times to get familiar with it.
[Answer]
When only variable names are used for a string, the output string is enclosed in single quotes. This is to enable non-string objects to be displayed on the screen as strings, that is, it displays the string representation of the object, not just the string itself. If the print command is used, the output is more friendly.

2-2.
Program output. Read the following Python script.
#! /Usr/bin/env python
1 + 2*4
(A) What do you think this script is used?
(B) What do you think the script will output?
(C) enter the above Code, save it as a script, and then run it. Does it do the same as you expected? Why is the same/different?
(D) How is the code executed separately different from that in the interactive interpreter? Try it and write the result.
(E) How to Improve the script so that it can work as you think?
[Answer]
Used for computing.
Will output 9, if it is in WINDOWS and Ubuntu systems.
If it is only the script of the above Code, there is no output.
Expected result 9 is displayed after execution in the interactive interpreter.
If you need to execute it in the script and get the expected results, you need to change it to print 1 + 2*4.

2-3.
Value and operator. Start the interactive interpreter and use Python to add, subtract, multiply, and divide two values (of any type. Then, the remainder operator is used to obtain the remainder of two numbers, and the multiplication operator is used to calculate the power of B of.
[Answer]
.
Note the division of Python. In versions earlier than 3.0, there are so-called True division and floor division.
When division is performed in the form of x/y, if both x and y are integers, the result is truncated and the integer part of the operation is obtained.
>>> Print 5/3
1
If either x or y is a floating point number, the True division is performed.
>>> Print 5/3.
1.66666666667
The so-called floor Division uses the form of x // y to obtain the maximum integer value not greater than the result. This operation is irrelevant to the operand.
>>> Print-5 // 3
-2
>>> Print-5 // 3.
-2.0

2-4.
Use the raw_input () function to obtain user input.
(A) Create a script using the raw_input () built-in function to get a string from the user input, and then display the string that the user just typed.
(B) Add a similar piece of code, but this time the input is a numerical value. Convert the input data to a numeric object (using int () or other numeric conversion functions) and display the value to the user (note, if you use a version earlier than 1.5, you need to use string. the ato * () function executes this conversion ).
[Answer]
(A) The Code is as follows:
>>> A = raw_input ("Please input a string ...")
Please input a string... 99
>>> Print
99
(B) The Code is as follows:
>>> A = raw_input ("Please input a letter ...")
Please input a letter... B
>>> Print ord ()
98

2-5.
Loop and number. Use while and for to create a loop.
(A) Write a while LOOP, and the output integer is 0 ~ 10 (make sure it is 0 ~ 10, instead of 0 ~ 9 or 1 ~ 10 ).
(B) Do the same thing as (a), but this time we use the range () built-in function.
[Answer]
(A) The for loop code is as follows:
>>> For I in 'abcdefhijk ':
... Print ord (I)-97,
...
0 1 2 3 4 5 6 7 8 9 10
(A) The while LOOP code is as follows:
>>> I = 0
>>> While (I <11 ):
... Print I,
... I = I + 1
...
0 1 2 3 4 5 6 7 8 9 10
(B) The Code is as follows:
>>> For I in range (0, 11 ):
... Print I,
...
0 1 2 3 4 5 6 7 8 9 10

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.