close

1.if迴圈

EX1:

if 8 > 9:
    print "I don't get printed!"
elif 8 < 9:
    print "I get printed!"
else:
    print "I also don't get printed!"

 

注意!在if、elif、else後面都有":"號

EX2:

def greater_less_equal_5(answer):
if answer>5:
return 1
elif answer<5:
return -1
else:
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

 

2.使用者輸入

variable_name = raw_input('Prompt')

使用者輸入後按ENTER

 

EX:

name = raw_input("What's your name?")
print name

3.確認字串

.isalpha()能確認字串中都是字母,而沒有數字及符號等

EX:

x = "J123"
x.isalpha()  # False

 

4.字串裡挑出字

EX:

s = "Charlie"

print s[0]
# will print "C"

print s[1:4]
# will print "har"

 

s[1:]=s[1:len(s)]

 

 5.function

 

def hello_world():
    """Prints 'Hello World!' to the console."""
    print "Hello World!"

function開頭為def

 

 

arrow
arrow
    全站熱搜

    Joseph Lin 發表在 痞客邦 留言(0) 人氣()