Python core programming-exercises-Chapter 2, Chapter 2

Source: Internet
Author: User

Python core programming-exercises-Chapter 2, Chapter 2

PS: PDF online address: http://bcmi.sjtu.edu.cn /~ Zhaohai/ptm2012/data/Python-kernel.programming.v2.pdf

2-1 variables, print and string formatting operators. Start the interactive interpreter, assign values (strings, values, and so on) to some variables, and display their values by entering the variable names. Use the print statement to do the same thing. What is the difference between the two? Also try to use the string format operator %, do more times, slowly become familiar.

#-*-Coding: UTF-8-*-> a = 123 >>> B = '000000' >>> a123 >>> B '000000' >>> print a123 >>> print b123>> print 'Although print statements output the same a and B, but they are still different. a is % d, which is an integer; B is % s, which is a string. '% (A, B) Although print statements output the same a and B, they are still different. a is 123, which is an integer; B is 123, which is a string.

 

It can be seen that in the interactive interpreter, the value displayed by entering the variable name directly displays the value of each variable containing the variable type, that is, the string with the string flag ''quotation marks; using the print statement, then the string quotation mark ''is removed.

2-2 program output

(A) This script is used to calculate the value of 1 + 2*4.

(B) This script will not output any items after it is saved and then run.

(C) After saving as a script, there is no output, as expected. The reason is that the interactive interpreter and script run differently.

(D) execute this Code separately without any output. Input 1 + 2*4 in the interactive interpreter and output "9" as follows:

#!/usr/bin/env python1 + 2 * 4

(E) Use the print statement.

2-3

2-4 ()

1 # -*- coding:utf-8 -*-2 string = raw_input("Please enter something:")3 print string

Output:

Please enter something:Python is number one!Python is number one!

(B)

# -*- coding:utf-8 -*-string = raw_input("Please enter a number:")print "Your enter is %d" % int(string)

Output:

Please enter a number:123Your enter is 123

2-5 ()

i = 0while i <= 10:    print i    i += 1

(B)

for i in range(11):    print i

2-6 (1)

#-*-Coding: UTF-8-*-num = 10if num> 0: print u "it is a positive number! "Elif num <0: print u" It is a negative number! "Else: print u" It is 0"

(2)

#-*-Coding: UTF-8-*-num = int (raw_input ("enter a number :". decode ("UTF-8 "). encode ("gbk") if num> 0: print u "it is a positive number! "Elif num <0: print u" It is a negative number! "Else: print u" It is 0"

2-7 (1) while

strings = raw_input("Please enter something:")i = 0while i < len(strings):    print strings[i],'(%d)' % i    i += 1

(2)

strings = raw_input("Please enter something:")for i,string in enumerate(strings):    print string,"(%d)" % i

2-8

(1)

list1 = [123,45,678,91,124]i = 0sum = 0while i < len(list1):    sum = sum + list1[i]    i += 1print sum sum = 0for num in list1:    sum = sum + num print sum 

(2)

num_one = int(raw_input("Please enter the first number:"))num_two = int(raw_input("Please enter the second numebr:"))num_three = int(raw_input("Please enter the third number:"))num_four = int(raw_input("Please enter the fourth number:"))num_five = int(raw_input("Please enter the fifth number:"))list1 = [num_one,num_two,num_three,num_four,num_five]i = 0sum = 0while i < len(list1):    sum = sum + list1[i]    i += 1print sum sum = 0for num in list1:    sum = sum + num print sum 

2-9

number = [12,345,67,890,123]sum = 0for num in number:    sum = sum + num average = float(sum) / 5print "%f is the average of these five numbers." % average

2-10

while True:    num = int(raw_input("Please enter a number bewteen 1 and 100:"))    if num >= 1 and num <= 100:        print "Well Done.You done it."        break    else:        print "Man,you are wrong!Please learn to read the prompt!!!"        continue 

2-11

#-*-Coding: UTF-8-*-def calc_sum (): global sum = 0 I = 0 while I <5: num = int (raw_input ("Please input five numbers in sequence :". decode ("UTF-8 "). encode ("gbk") sum = sum + num I + = 1 return sum while True: choice = raw_input ("1 sum, 2 calculate the mean, X exit, enter your choice :". decode ("UTF-8 "). encode ("gbk") if choice = 'X': print u "program will exit" break elif choice. isdigit () and int (choice) = 1:
# Isdigit () is used to determine whether an object is a number. If it is a number, True is returned. Otherwise, False is returned. For example 123. isdigit () returns True print u. The sum of the five numbers and "calc_sum () print u" is: ", sum elif choice. isdigit () and int (choice) = 2: print u "calculate the average value of the five" calc_sum () print u ":", float (sum) /5 else: print u "Please input again"

2-12

(A) dir () displays all global variables

(B) dir returns <built-in function dir> without parentheses. Basic Introduction to dir

(C) type (dir) returns <type 'builtin _ function_or_method '>, which indicates a built-in function or method.

2-13

2-14

Original statement:

print -2 * 4 + 3 ** 2

Rewrite:

print -2 * 4 + (3 ** 2)

2-15/2-16 suspect, to be supplemented

 

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.