Python Programming: from entry to practice "the second chapter" Exercises

2-1 simple message: a message stored in variables, and then print it out.

message = 'python'
print(message)

2-2 a plurality of simple message: a message stored in variables, print it out; then the value of the variable is modified to a new message and print it out.

message = 'Python'
print(message)
message = 'Python Language'

2-3 personalized message: The user's name stored in a variable, and the user display a message. The message should be very simple, such as "Hello Eric, would you like to learn some Python today?".

name = 'Fiona'
print("Hello " + name +',' + "would you like to learn some Python?")

2-4 adjust the name of the case: a person's name is stored in a variable, and then to lowercase, uppercase and capitalized displayed the names.

name = 'fiONA'
print(name.upper())
print(name.lower())
print(name.title())

2-5 saying: find a celebrity you admire, said the famous words, the names of the celebrity and his famous print. This output should be similar to the following (including the quotation marks): Albert Einstein once said, "A person who never made a mistake never tried anything new."

name = 'Georg Wilhelm Friedrich Hegel'
message = 'What is rational is actual and what is actual is rational.'
print(name+ ',' + '"' + message + '"')

2-6 2 saying: Repeat the exercise 2-5, but will be in the variable famous_person, and then create a message to be displayed celebrity's name is stored, and in the variable message, and then print the message store.

famous_person = 'Georg Wilhelm Friedrich Hegel'
message = 'What is rational is actual and what is actual is rational.'
print(famous_person + '\n' + message)

2-7 Excluding names in the blank: storing a name, and contains a number of blank characters at its beginning and end. Be sure to use a combination of at least character "\ t" and "\ n" each time. Print this person's name to display a blank its beginning and end. Then, removed were used function lstrip (), rstrip () and strip () for processing names, and print the results.

name = ' Fiona '
print(name.lstrip())
print(name.rstrip())
print(name.strip())

2-8 Digital 8: preparation of expression 4, respectively using addition, subtraction, multiplication and division, but the result is the number 8. To use the print statement to display the results, be sure to use these expressions in parentheses, that is to say, you should write four lines similar to the following code: print (5 + 3)

Output should be 4 rows, wherein each row contains only 8 numbers.

print(4 + 4)
print(9 - 1)
print(2 * 4)
print(8 / 1)

2-9 favorite number: your favorite number is stored in a variable, and then use this variable to create a message that your favorite number, then this news printed.

number = 'seven'
message = 'My favourite number is '
print(message + number.title() + '.')

2-10 add a comment: Select two programs you write, you add at least one annotation in each program. If the program is too simple, and nothing should be noted that, in the beginning of the program file with your name and the current date, and then the word describes the functionality of the program.

2-11 Python Zen: Run import this in Python terminal session, and cursory glance at the other guiding principles.

Guess you like

Origin www.cnblogs.com/Polaris-ICE/p/11367146.html