2014年11月13日

Python Study (7) – Statements


Introduction

Programming statements are used to expresse action or assign a variable in program.Each statement  has own syntax and purpose. We can combine these statements to achieve specific purpose.

Assignment Statements

Assingment statement is used to set reference of object or value stored in monery address by a variable name.

(1) normal assignment : fruit = “apple”

(2) sequence assignment:  a,b = 'Jack', 'John'

(3) List assignment :

(4) Augmented assignment:
     >>>a =1
     >>>a+=1

(5) Sequence assignment

  • Sequence one to one
>>>seq = [1, 2, 3, 4]
>>> a,b,c,d = seq
>>> a,b = seq  #seq have 4 elements, but assignemnt elements just have 2
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    a,b = seq
ValueError: too many values to unpack (expected 2)

>>> a,b,c,d = seq
>>>a
1
  • Sequence one and rest
>>>seq = [1, 2, 3, 4]
>>> e, *f = seq[0:3]    #e get first element, other element assign f
>>> e
1
>>> f
[2, 3]

>>> *g, h = seq[0:3]  #
>>> g
[1, 2]
>>> h
3

>>> i, *j, k = seq[0:4]
>>> i
1
>>> j
[2, 3]
>>> k
4

  • Boundary cases
>>>seq = [1, 2, 3, 4]
>>>

 

(6) Mulitple-target assignment
     There has many assignment variable.

>>> a = b =0
>>> b = b + 1
>>> a,b
(0, 1)

>>> a =b =[]
>>> b.append(10)
([10], [10])

>>> a=[]
>>> b= []
>>> a.append(21)
([],[42])




 

Control Statements

1.if/elif/else statment

  • Syntax

if expression :
      statement1
elif:
       statement2
else:
      statement3


  • Example
    (1) There are a fruit list , we check apple if it is in the list.
fruit = ["apple","banana"]
if "apple" in fruit:
    print("found it!)
else:
    print("don’t found it!")
--------------------------------------
found it !

       (2) Jack

grade = {"Jack":90, "Joe":60, "Bryant":50,  "Hacker":30}
if grade["Hacker"]>=90:
    print("perfect!")
elif grade["Hacker"]<=90 and grade["Hacker"] >=60 :
    print("great")
elif grade["Hacker"]<60:
    print("hard")

--------------------------------------
hard

2. for/else

  • Syntax

for var inm sequence:
    statment1

       (1) 

list1 = {1,2,3,4,5}
for a in list1 :
    print(a)

--------------------------------------
1
2
3
4
5

3.while/else

  • Syntax

while( expression):
     statment1

  • Exception

while True:
    inputNumebr = input("Enter a numebr:")
    if inputNumebr== "stop":break
        print(int(inputNumebr))
-----------------------------------------------------------------------------------------Enter a numebr:1234
1234
Enter a numebr:stop

Note : you input a number 


Handling Errors


In python when the programe is executed how to handle it and throw message to notify operator.

  • Syntax

try:
    statement01
except:
    statement02

  • Exception

while True:
    inputNumebr = input("Enter a numebr:")
    if inputNumebr== "stop":break
    try:
        print(int(inputNumebr))
    except:
        print("Please input a number!")
-----------------------------------------------------------------------------------------Enter a numebr:1234
1234
Enter a numebr:stop

Note : When  input is not a  number , it will enter into except section and    show “Please input a number”
 




 

 

沒有留言:

張貼留言