欢迎来到今天的讨论,我们将探讨,python自动化运维需要掌握的技能 python自动化运维快速入门 pdf,让我们开始吧!
习题26:恭喜你,现在可以考试了!
笔记:
优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能Python的进阶教程。因此需要去排查检验所有可能出错的地方。今天的任务就是修改一份可能有错的代码,看自己是否能掌握之前的一些基础知识。总结一些常见错误:少括号、少冒号、代码单词拼写错误、符号拼写错误等常见的细小错误。注意:python3之前输出print
可不用加括号;python3之后要求加上括号。
错误代码来源:点此
修改后:
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = (0)
print(word)
def print_last_word(words):
"""Prints the last word after popping it off."""
word = (-1)
print(word)
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
print ("Let's practice everything.")#加了括号
print ('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""
print("--------------")
print(poem)
print("--------------")
six = 10 - 2 + 3 - 5
print("This should be six: %s" % six)
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
jelly_beans, jars, crates = secret_formula(start_point)
print("With a starting point of: %d" % start_point)
print("We'd have %d jeans, %d jars, and %d crates." % (jelly_beans, jars, crates))
start_point = start_point / 10
print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))
#与python脚本交互
import test25
sentence = "All good things come to those who weight."
words = test25.break_words(sentence)
sorted_words = test25.sort_words(words)
print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = test25.sort_sentence(sentence)
print(sorted_words)
print_first_and_last(sentence)
print_first_and_last_sorted(sentence)
结果:
习题27:记住逻辑关系
笔记:
万事开头难!一些重要的概念还是得记住,每天花一小部分时间记一些知识点。逻辑术语:计算机的逻辑就是在程序的某个位置检查这些字符或者变量组合在一起表达的结果是真是假。逻辑术语,记了八百遍了,再复习一下吧。下来就是真值表其实蛮好记得,逻辑清楚即可。
习题28:布尔表达式练习
练习: 结果为False
3 != 4 and not ("testing" != "test" or "Python" == "Python")
//True and not(True or True)
//True and False
//False
习题29:如果(if)
练习1:
people = 30
cars = 40
buses = 15
if(cars > people):
print("cars")
elif(cars < people):
print("people")
elif(buses > people):
print("people")
else:
print("no decision")
练习2: 通过使用语句判断数字是正数、负数或零,也可使用if内嵌语句。
#用户输入数字
num = float(input("输入一个数字:"))
if num > 0:
print("正数")
elif num == 0:
print("零")
else:
print("负数")
#if内嵌语句
num = float(input("输入一个数字:"))
if num >= 0:#大于等于两个情况
if(num == 0):
print("零")
else:
print("正数")
else:#否则就是小于零了
print("负数")
结果测试:
习题30:else和if
笔记:
if对于下一行代码所做的就是if表达式为真才会进入if内部的功能操作,否则就跳过这一段if语句的缩进其实就是表示缩进的这几行是属于if表达式成立时要执行的语句;行尾的冒号作用是告诉python接下来你要创建一个新的代码区段。
原文地址1:https://blog.youkuaiyun.com/weixin_43136158/article/details/105925096
参考资料:python中用turtle画一个圆形 https://blog.youkuaiyun.com/SXIAOYAN_/article/details/140061099