一些简单的练习

《Python编程 从入门到实践》

动手试一试2.1~2.11

编写环境为vs Code, python版本为3.6

1.简单信息:将一条信息存储到变量中,再将其打印出来。

 message = "Hello Python!"
 print(message)

2. 多条简单信息:将一条信息存储到变量中,将其打印出来;再将变量的值修改为一条新信息,并将其打印出来。

    message = "hello Python!"
    print(message)
    message = "Hello world!"
    print(message)

3. 个性化消息:将用用户的姓名存到一个变量中,并向该用户显示一条信息。显示的消息应非常简单,如“Hello Eric, would you like to learn some Python today?”。

    userName = "John"
    message = "Hello " + userName + ", would you like to play some chicken today?"
    print(message)

4. 调整名字的大小写:将一个人名存储到一个变量中,再以小写,大写和首字母大写的方式显示这个人名。

    name = "Joney"
    print(name.lower())
    print(name.upper())
    print(name.title())

5.名言:找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似下面这样(包括引号):

Alert Einstein once said, “A person who never made a mistake never tried anything new.”

    quote = "We're here to put a dent in the universe. Otherwise why else even be here?"
    name = "steve jobs"
    print(name.title() + ' once said,"' + quote + '"')

6. 略。(与上一题重复)
7.剔除人名中的空白:存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合’\t’和’\n’各一次。
打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip(),rstrip()和strip()对人名进行处理,并将结果打印出来。

    name = "\t\t   \n\tsteve jobs\t\t  \t \n"
    print(name)
    print(name.lstrip())
    print(name.rstrip())
    print(name.strip())

8.数字8:编写四个表达式,他们分别使用加法、减法、乘法和除法运算,但结果都是8。为使用print语句来显示结果,务必将这些表达式用括号括起来,也就是说,你应该编写4行类似于下面的代码

print(5 + 3)

输出应为4行,其中每行都只包含数字8。

print(4 + 4)
print(16 - 8)
print(2 * 4)
print(16 / 2)

9.最喜欢的数字:将你最喜欢的数字存储在一个变量中,在使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条信息打印出来。

    num = 9
    message = 'I like ' + str(num) + ' best'
    print(message)

10.添加注释:选择你编写的两个程序吗,在每个程序中都至少添加一条注释。如果程序太简单,实在没有什么需要说明的,就在程序文件开头加上你的姓名和当前日期,再用一句话阐述程序的功能。

#1
```
    Author: Darren
    Date: 2018.3.10
    This function print 1 to n recursively.
```
def func1(n):
    if n == 1:
        print(n)
    else:
        func1(n - 1)
        print(n)
#2
```
    Author: Darren
    Date: 2018.3.10
    This function print 1 to n with a loop
```
def func2(n):
    i = 1
    while i <= n:
        print(i)
        i += 1

```

11.Python之禅:在python终端会话中执行命令imort this,并粗略浏览一下其他的指导原则

    import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

猜你喜欢

转载自blog.csdn.net/qq_36183810/article/details/79507513