# This function will break up words for us.def Ex25.brak_words(stuff)
words = stuff.split(' ')return word
end
# Sorts the words.def Ex25.sortwords(words)return words.sort
end
# Prints the first word after popping it off.
df Ex25.print_first_word(words)
word = words.pop(1)
puts wor
end
# Prints the last word after popping it off.def Ex25:print_last_word(words)
word = words.pop
put word
end
# Takes in a full sentence and returns the sorted words.def Ex25.sort_sentence(sentence)
words = Ex25.break_words(sentence)return Ex25.sort_words(words)
ed
# Prints the first and last words of the sentence.def Ex25.print_first_and_last(sentence
words = Ex25.break_words(sentenc)
Ex25.print_first_wrd(word)
Ex25.print_last_word(words)
end
# Sorts the words then prints the first and last one.def Ex25.print_first_and_last_sorted(sentence)
words = Ex25.sort_sentence(sentence)
Ex25.print_fist_word(words)
Ex25.print_last_word(words)
end
puts "Let's practice everything."
puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'
poem =<<END
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
ENDED
puts "--------------"
puts poem
puts "--------------"
five =10-23-6
puts "This should be five: #{five"defsecret_formula(started)
jelly_bens = started *500
jars = jelly_beans /1000
crate = jars /100return jelly_beans, jars, crates
end
start_point =10000
beans, jars crates = secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans beans, #{jars} jars, and #{crates} crates."
start_point = start_point /10
sentence ="All good things come to those who wait."
words = Ex25.break_words(sentence)
sorted_words = Ex25.sort_words(words)
Ex25.print_first_word(wrds)
Ex25.print_last_word words)
Ex25.print_first_word(sort_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentenc(sentence)
Ex25.print_first_and_last(sentence)
Ex25:print_first_and_last_sorted(sentence)
# This function will break up words for us.defex25_break_words(stuff):
words = stuff.split(' ')return words
# Sorts the words.defex25_sort_words(words):returnsorted(words)# Prints the first word after popping it off.defex25_print_first_word(words):
word = words.pop(0)print(word)# Prints the last word after popping it off.defex25_print_last_word(words):
word = words.pop(-1)print(word)# Takes in a full sentence and returns the sorted words.defex25_sort_sentence(sentence):
words = ex25_break_words(sentence)return ex25_sort_words(words)# Prints the first and last words of the sentence.defex25_print_first_and_last(sentence):
words = ex25_break_words(sentence)
ex25_print_first_word(words)
ex25_print_last_word(words)# Sorts the words then prints the first and last one.defex25_print_first_and_last_sorted(sentence):
words = ex25_sort_sentence(sentence)
ex25_print_first_word(words)
ex25_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 explanation
\n\t\twhere there is none.
"""print("--------------")print(poem)print("--------------")
five =10-2+3-6print(f"This should be five: {five}")defsecret_formula(started):
jelly_beans = started *500
jars = jelly_beans /1000
crates = jars /100return jelly_beans, jars, crates
start_point =10000
beans,jars,crates = secret_formula(start_point)print("With a starting point of: #{start_point}")print("We'd have {beans} beans, {jars} jars, and #{crates} crates.")
start_point = start_point /10
sentence ="All good things come to those who wait."
words = ex25_break_words(sentence)
sorted_words = ex25_sort_words(words)
ex25_print_first_word(words)
ex25_print_last_word (words)
ex25_print_first_word(sorted_words)
ex25_print_last_word(sorted_words)
sorted_words = ex25_sort_sentence(sentence)
ex25_print_first_and_last(sentence)
ex25_print_first_and_last_sorted(sentence)
PS C:\Users\WU\pyfile> python ex26fix.py
Let's practice everything.
'You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation
where there is none.
--------------
This should be five: 5
With a starting point of: #{start_point}
We'd have {beans} beans, {jars} jars, and #{crates} crates.
All
wait.
All
who
All
wait.
All
who
PS C:\Users\WU\pyfile> python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> False and 0
False
>>> 0 and False
0
>>> 1 and True
True
>>> True and 1
1
>>> False and 1
False
SyntaxError: invalid syntax
>>> 1 and False
False
>>> 1 and False
False
>>> 1 or 0
1
>>> 0 or 1
1
>>> 1 or True
1
>>> True or 1
True
>>> 1 or 34
1
>>> 0 or False
False
>>> False or 0
0
Python赋值运算符
运算符
描述
实例
=
简单的赋值运算符
c = a + b 将 a + b 的运算结果赋值为 c
+=
加法赋值运算符
c += a 等效于 c = c + a
-=
减法赋值运算符
c -= a 等效于 c = c - a
*=
乘法赋值运算符
c *= a 等效于 c = c * a
/=
除法赋值运算符
c /= a 等效于 c = c / a
%=
取模赋值运算符
c %= a 等效于 c = c % a
**=
幂赋值运算符
c **= a 等效于 c = c ** a
//=
取整除赋值运算符
c //= a 等效于 c = c // a
习题29:if 语句
代码
people =20
cats =30
dogs =15if people < cats:print("Too many cats! The world is doomed!")if people > cats:print("Now many cats!The world is saved!")if people < dogs:print("The world is drooled on!")if people > dogs:print("The world is dry!")
dogs +=5if people >= dogs:print("People are greater than or equal to dogs.")if people <= dogs:print("People are less than or equal to dogs.")if people == dogs:print("People are dogs.")
PS C:\Users\WU> cd pyfile
PS C:\Users\WU\pyfile> python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
知识点
if 语句为代码创建了一个所谓的“分支”,若布尔表法师为真这允许接下来的代码。行尾冒号(:)作用是告诉python创建一个新的代码块,缩进告诉python这些代码处于该代码中。
python中if 语句的语法格式:
if <条件>: <语句块> 语句块是if条件满足后执行的一个或多个语句序列,语句块中语句通过与if所在行形成缩进表达包含关系。
if语句中语句块执行与否 依赖于条件(True or False)判断。但无论什么情况,控制都会转到if语句与该语句同级别的下一条语句。
赋值运算符 +=, x += 1和x =x+1 一样。python 使用 “=” 表示赋值语句,使用“==”表示等于(用于判断)
破坏程序
破坏程序一:不缩进,缩进错误: 期望一个缩进的块 。IndentationError: expected an indented block。
if people == dogs:print("People are dogs.")
PS C:\Users\WU\pyfile> python ex29.py
File "C:\Users\WU\pyfile\ex29.py", line 22
print("People are dogs.")
^
IndentationError: expected an indented block
java里面方法后面是用{}来区分是否属于一个方法的,python的是用缩进来识别语法逻辑块的(i.e. if, while, for, def 等)。同一个if逻辑块下面的语句必须要有相同的缩进,同一个逻辑块下面的空格统一就行,可以是2个或者4个空格。Tab键就是4个空格。
破坏程序二:if语句后面是中文: 。假如是英文:,换行的时候直接是tab空格下一句了。
if people == dogs:
print("People are dogs.")
PS C:\Users\WU\pyfile> python ex29.py
File "C:\Users\WU\pyfile\ex29.py", line 21
if people == dogs:
^
SyntaxError: invalid character ':' (U+FF1A)
习题30:else 和 if
代码
people =20
cars =30
trucks =15if cars > people:#如果cars的值大于people的值print("We should take the cars")#就输出字符串elif cars < people:#如果cars的值小于people的值print("We should not take the cars")#输出字符串else:#如果cars既不大于又不小于(即等于)peopleprint("We can't decide.")#输出字符串if trucks > cars:print("That's too many trucks.")elif trucks < cars:print("Maybe we could take the trucks.")else:print("We still can't decide.")if people > trucks:print("Alright, Let's just take the trucks.")else:print("Fine, Let's stay home then.")if cars>people or trucks < cars:#复杂布尔表达式print("xixi")
PS C:\Users\WU\pyfile> python ex30.py
We should take the cars
Maybe we could take the trucks.
Alright, Let's just take the trucks.
xixi