>>>
>>> if ( age < 15 ): print("1")
... else print("2)
File "<stdin>", line 2
else print("2)
^
SyntaxError: invalid syntax
>>> if ( age < 15 ):
... print(1)
... else:
... print(2)
...
2
>>> if ( age < 15 ):
... print("y")
... else:
... print("NNN")
...
y
>>>
注意排版
Learning Note
2018年3月20日 星期二
Tuples
>>> tup
('aaa', 'bbb', 222, 333, 'eee')
>>> tup2 = ( 11, 222, 333, 'dddd', "eeeee" )
>>> tup
('aaa', 'bbb', 222, 333, 'eee')
>>> tup2
(11, 222, 333, 'dddd', 'eeeee')
>>> tup3 = tup + tup2
>>> tup3
('aaa', 'bbb', 222, 333, 'eee', 11, 222, 333, 'dddd', 'eeeee')
>>> tup3[2:5]
(222, 333, 'eee')
>>> tup3 = tup + tup * 3
>>> tup3
('aaa', 'bbb', 222, 333, 'eee', 'aaa', 'bbb', 222, 333, 'eee', 'aaa', 'bbb', 222, 333, 'eee', 'aaa', 'bbb', 222, 333, 'eee')
>>> len(tup3)
20
>>> del tup3
>>>
('aaa', 'bbb', 222, 333, 'eee')
>>> tup2 = ( 11, 222, 333, 'dddd', "eeeee" )
>>> tup
('aaa', 'bbb', 222, 333, 'eee')
>>> tup2
(11, 222, 333, 'dddd', 'eeeee')
>>> tup3 = tup + tup2
>>> tup3
('aaa', 'bbb', 222, 333, 'eee', 11, 222, 333, 'dddd', 'eeeee')
>>> tup3[2:5]
(222, 333, 'eee')
>>> tup3 = tup + tup * 3
>>> tup3
('aaa', 'bbb', 222, 333, 'eee', 'aaa', 'bbb', 222, 333, 'eee', 'aaa', 'bbb', 222, 333, 'eee', 'aaa', 'bbb', 222, 333, 'eee')
>>> len(tup3)
20
>>> del tup3
>>>
2018年3月19日 星期一
Dictionaries
>>> students = { "aaa":11, 'bbb':12, 'ccc':13 }
>>> students
{'aaa': 11, 'bbb': 12, 'ccc': 13}
>>> students['bbb']
12
>>>
>>> students
{'aaa': 11, 'bbb': 12, 'ccc': 13}
>>> students['ccc'] = 33
>>> students
{'aaa': 11, 'bbb': 12, 'ccc': 33}
>>>
>>> students
{'aaa': 11, 'bbb': 12, 'ccc': 13}
>>> students['bbb']
12
>>>
>>> students
{'aaa': 11, 'bbb': 12, 'ccc': 13}
>>> students['ccc'] = 33
>>> students
{'aaa': 11, 'bbb': 12, 'ccc': 33}
>>>
Lists ( Array )
>>> list_a = [ "aaa", "bbb", "ccc", 'dddd' ]
>>> list_a
['aaa', 'bbb', 'ccc', 'dddd']
>>>
>>> list_a[1:2]
['bbb']
>>> list_a[0:2]
['aaa', 'bbb']
>>>
>>> list_a
['aaa', 'bbb', 'ccc', 'dddd']
>>> list_a[0] = 0
>>> list_a
[0, 'bbb', 'ccc', 'dddd']
>>>
>>> del list_a[1]
>>> list_a
[0, 'ccc', 'dddd']
>>>
>>> list_b = [ 'b0', "b1", 2 ]
>>> list_b
['b0', 'b1', 2]
>>>
>>> list_b.append(4)
>>> list_b
['b0', 'b1', 2, 4]
>>>
>>> list_a
[0, 'ccc', 'dddd']
>>> list_b
['b0', 'b1', 2, 4]
>>> list_a + list_b
[0, 'ccc', 'dddd', 'b0', 'b1', 2, 4]
>>> list_a + list_b * 2
[0, 'ccc', 'dddd', 'b0', 'b1', 2, 4, 'b0', 'b1', 2, 4]
>>>
>>> max(list_a)
'dddd'
>>> min(list_b)
2
>>> list_a
['aaa', 'bbb', 'ccc', 'dddd']
>>>
>>> list_a[1:2]
['bbb']
>>> list_a[0:2]
['aaa', 'bbb']
>>>
>>> list_a
['aaa', 'bbb', 'ccc', 'dddd']
>>> list_a[0] = 0
>>> list_a
[0, 'bbb', 'ccc', 'dddd']
>>>
>>> del list_a[1]
>>> list_a
[0, 'ccc', 'dddd']
>>>
>>> list_b = [ 'b0', "b1", 2 ]
>>> list_b
['b0', 'b1', 2]
>>>
>>> list_b.append(4)
>>> list_b
['b0', 'b1', 2, 4]
>>>
>>> list_a
[0, 'ccc', 'dddd']
>>> list_b
['b0', 'b1', 2, 4]
>>> list_a + list_b
[0, 'ccc', 'dddd', 'b0', 'b1', 2, 4]
>>> list_a + list_b * 2
[0, 'ccc', 'dddd', 'b0', 'b1', 2, 4, 'b0', 'b1', 2, 4]
>>>
>>> max(list_a)
'dddd'
>>> min(list_b)
2
Placeholders
>>> sent = "%s is %d years old."
>>> sent%("John", 13)
'John is 13 years old.'
>>> sent
'%s is %d years old.'
>>>
>>>
>>> sent2 = sent%("Ali", 40 )
>>> sent2
'Ali is 40 years old.'
>>>
>>> sent%("John", 13)
'John is 13 years old.'
>>> sent
'%s is %d years old.'
>>>
>>>
>>> sent2 = sent%("Ali", 40 )
>>> sent2
'Ali is 40 years old.'
>>>
2018年3月14日 星期三
Arithmetic Operators
first_name = "first"
last_name = "last"
sa = "1234567890abcdef"
last_name = "last"
- first_name + " " + last_name ==> "first last"
- first_name * 3 ==> "firstfirstfirst"
- first_name[0] ==> 'f'
sa = "1234567890abcdef"
- sa[0:3] ===> "123"
- sa[:3] ===> "123"
- sa[:-5] ===> "1234567890a"
- sa[5:6] =====> "6"
- sa[5:7] =====> "67"
Variables and Multiple Assignment
- age = 20x
- sentence = "My name is xxxx"
- Tom, Bob, Sandy = 15, 18, 22
- Mike = Enya = 18
訂閱:
文章 (Atom)