python第2集——Branching and iteration

string:

  • sequence of characters, 包括letters, special characters, spaces, digits.
  • 用单引号或者双引号表示。
  • two operations on strings: 1) concatenate strings.

concatenate is just a fancy word for using this plus operator, which means put the strings together.

注:concatenate的意思:to connect separate units or items into a linked system.

举例:

hi = “hello there”

name = “ana”

greet = hi + name—-output:hello thereana

greeting = hi + ” ” + name—–output: hello there ana

greeting = hi + (” ” + name) * 3 —– output: hello there ana ana ana

print ():

x = 1

x_str = str(x)

print (“my fav num is “, x, “.”, “x=”, x)

print (“my fav num is ” + x_str + “. ” + “x = ” + x_str)

以上两个print显示内容是一样的。显示内容为:

my fav num is 1 . x= 1

注意:

第一,第一个print里面,用逗号连接,内容之间自动加空格。如果不需要空格的话,就需要用第二个print里面的加号(花名:concatenation operation)。

第二,逗号可以连接不同种类的type,第一个print里面是string+int。缺点是到处都是空格。如果用加号,只限于strings,优点是要不要空格是你可以控制的。

input(“”):

  • print whatever is in the quotes
  • user types in something and hits enter
  • binds that value to a variable:

text = input (“Type anything… “)

print (5*text)

注意:这里的变量text,最终绑定的value是用户输入的值。

  • input gives you a string so must cast if working with numbers, 即默认类型为string。

num = int (input (“Type a number… “))

print (5*number)

注意:前面print (5*text),因为text的默认类型是string,因此显示的是重复五遍的text。print (5*text)和print (text*5)是一样的。而这里的print (5*number),显示的是数值。

Comparison operators on int, float, string

i and j are variable names. Comparison below evaluate to a Boolean.

i>j

i>=j

i<j

i<=j

i==j—–equality test, True if i is the same as j.

i!=j—–inequality test, True if i is not the same as j.

你可以比较ints with ints/floats, strings with strings。但string和number不能比较。

string和string的比较,是根据字母表的排序来比较,排在后面的字母>前面的字母。而不是根据string的长度或者其它。

Logic operators on bools:

a and b are variable names (with Boolean values):

not a —-True if a is False.

a and b —– True if both are True.

a or b —— True if either or both are True.

Control flow – Branching

1)

if <condition>:

<expression>

<expression>

2)

if <condition>:

<expression>

<expression>

else:

<expression>

<expression>

3)

if <condition>:

<expression>

<expression>

elif <condition>:
<expression>

<expression>

else:

<expression>

<expression>

解读:

  • <condition>has a value True or False
  • evaluate expression in that block if <condition> is True。如果<condition>为False,则不执行。
  • code block用indented表示,一般是四个空格。
  • So the way this works is if more than one condition is true, you’re actually just going to enter the very first one that’s true. You’re never going to enter more than one of these code blocks.

Control flow: while loops:

while <condition>:

<expression>

<expression>.

  • <condition> evaluates to a Boolean
  • if <condition> is True, do all the septs inside the while code block
  • check <condition> again
  • repeat until <condition> is False

举例:

n = input (“You are in the Lost Forest. Go left or right?”)

while n == “right”:

n = input (“You are in the Lost Forest. Go left or right?”)

print (“You are out of the Lost Forest. “)

Control flow: while and for loops

iterate through numbers in a sequence

n = 0

while n<5:

print (n)

n = n+1

# shortcut with for loop:

for n in range (5):

print (n)

注:the sequence is going to be 0, 1, 2, 3 and 4.

两个程序的显示结果是一样的。

for <variable> in range (<some_num>):

<expression>

<expression>

解读:

  • each time through the loop, <variable> takes a value
  • first time, <variable> starts at the smallest value.
  • next time, <variable> gets the pre value +1
  • etc.

range (start, stop, step):

  • default values are start = 0 and step =1
  • loop until value is stop – 1

break statement:

  • immediately exits whatever loop it is in
  • skips remaining expressions in code block
  • exits only innermost loop!

点赞