Python起步

今天Python正式起步:

C:\Users\April_Chou>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> #根据给定的年月日以数字形式打印出日期
... months = [
... 'January',
... 'February',
... 'March',
... 'April',
... 'May',
... 'June',
... 'August',
... 'September',
... 'October',
... 'November',
... 'December'
... ]
>>> 以1~31的数字作为结尾的列表
File "<stdin>", line 1
以1~31的数字作为结尾的列表
^
SyntaxError: invalid syntax
>>> #以1~31的数字作为结尾的列表
... endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
... + ['st', 'nd', 'rd'] + 7 * ['th'] \
... + ['st']
>>> year = raw_input('Year: ')
Year: 1974
>>> month = raw_input('Month(1-12): ')
Month(1-12): 8
>>> day = raw_input('Day(1-31): ')
Day(1-31): 16
>>> month_number = int(month)
>>> day_number = int(day)
>>> month_name = months[month_number - 1]
>>> ordinal = day + endings[day_number - 1]
>>> print month_name + ' ' + ordinal + ', ' + year
September 16th, 1974
>>> permissions = 'rw'
>>> 'w' in permission
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'permission' is not defined
>>> 'w' in permissions
True
>>> 'x' in permissions
False
>>> users = ['mlh', 'foo', 'bar']
>>> raw_input('Enter your user name: ') in users
Enter your user name: mlh
True
>>> subject = '$$$ Get rich now!!! $$$'
>>> '$$$' in subject
True
>>> #检查用户名和PIN码
... database = raw_input('User name: ')
User name: dd
>>> #检查用户名和PIN码
... database = [['albert', '1234'],['dilbert', '4242'],['smith', '7524'],['jones', '9843']]
>>> username = raw_input('User name: ')
User name: jones
>>> pin = raw_input('PIN code: ')
PIN code: 9843
>>> if [uysername , pin ] in database: print 'Acdess granted'
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'uysername' is not defined
>>> if [username , pin ] in database: print 'Acdess granted'
...
Acdess granted
>>> if [username , pin ] in database: print 'Acdess granted'
...
Acdess granted
>>> numbers = [100, 34, 678]
>>> len(numbers)
3
>>> max(numbers)
678
>>> min(numbers)
34
>>> list('Hello')
['H', 'e', 'l', 'l', 'o']
>>> names = ['a','b','c','d','e']
>>> del names[2]
>>> names
['a', 'b', 'd', 'e']
>>> lst = [1,2,3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> b
[4, 5, 6]
>>> a.pop
<built-in method pop of list object at 0x000000000352A688>
>>> a.pop()
6
>>> a.pop(0)
1
>>> s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> a
[2, 3, 4, 5]
>>> x = [9,0,7,99]
>>> x.sort()
>>> x
[0, 7, 9, 99]
>>> y = x
>>> y
[0, 7, 9, 99]
>>> x = [99,999,87,0,123]
>>> y = x.sorted(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'sorted'
>>> y = x.sorted()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'sorted'
>>> y = sorted(x)
>>> y
[0, 87, 99, 123, 999]
>>> cmp(42, 30)
1
>>> cmp(42, 99)
-1
>>> cmp(42, 42)
0
>>> numbers = [5,2,9,7]
>>> numbers.sort(cmp)
>>> numbers
[2, 5, 7, 9]
>>> x = 1,2,3
>>>
>>> x
(1, 2, 3)
>>> del x[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>>

posted @ 2017-05-27 00:10  M_派森  阅读(248)  评论(0编辑  收藏  举报