PYTHON之路(一)

1. python3 installation

download: https://www.python.org/downloads/

linux install:
$sudo apt-get install build-essential checkinstall
$sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
$ cd /usr/src
$ wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
$ tar xzf Python-3.4.3.tgz
$ cd Python-3.4.3
$ sudo ./configure
$ sudo make altinstall
make altinstall is used to prevent replacing the default python binary file /usr/bin/python.

$python3.4 -V

Python 3.4.3





windows install: double click *.msi
after installation, set the environment variable




2. pycharm installation

pycharm install and register:
download: http://www.jetbrains.com/pycharm/download/index.html#section=windows
register: 注册方法:    在 注册时选择 License server ,填 http://idea.lanyus.com  然后点击OK

3. 字符串拼接: 少用+ 拼接,每一串字符串都要开辟一个内存空间,用+拼接需要开辟多个内存空间。

#!/usr/bin/env python
#_*_ coding:utf8 _*_

print 'hello world'
name = input("name: ")
age = int(input("age: "))
job = input("job: ")
msg = '''
Information of %s:
Name: %s
Age: %d
Job: %s
'''%(name,name,age,job)
print (msg)
print ("Information of %s:\nName: %s\nAge: %d\nJob: %s" %(name,name,age,job))
print ("Information of " + name + " \nName: " + name + "\nAge: " + "\nJob: " + job)


4. 列表的一些简单方法

>>> name_list = list(['orse','eric','alex'])
>>> name_list
['orse', 'eric', 'alex']
>>> name_list[0]
'orse'
>>> dir(name_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli
ce__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getit
em__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__r
educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__'
, '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a
ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
]
>>> name_list.append(name_list[0])
>>> name_list
['orse', 'eric', 'alex', 'orse']
>>> name_list.count('orse')
2
>>> name_list.index('orse')
0
>>> name_list.index('orse')
0
>>> help(name_list.insert)
Help on built-in function insert:

insert(...)
L.insert(index, object) -- insert object before index

>>> name_list.insert(3,'ggod')
>>> name_list
['orse', 'eric', 'alex', 'ggod', 'orse']
>>> name_list.pop()
'orse'
>>> name_list.pop('ggod')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> name_list.pop(3)
'ggod'
>>> name_list.remove(name_list[1])
>>> name_list
['orse', 'alex']
>>> name_list.reverse()
>>> name_list
['alex', 'orse']
>>> name_list.sort()
>>> name_list
['alex', 'orse']
>>> name_list.extend(name_list)
>>>name_list
['alex','orse','alex','orse']
>>>name_list.extend('og')
>>>name_list
['alex','orse','alex','orse','o','g']

extend and 切片

>>> a=['jack','rose','david']
>>> a
['jack', 'rose', 'david']
>>> a+a
['jack', 'rose', 'david', 'jack', 'rose', 'david']
>>> a
['jack', 'rose', 'david']
>>> a.extend(a)
>>> a
['jack', 'rose', 'david', 'jack', 'rose', 'david']
>>> a[0:3]
['jack', 'rose', 'david']
>>>


5. 元组

>>> type(a)
<class 'list'>
>>> b=tuple(a)
>>> b
('jack', 'rose', 'david', 'jack', 'rose', 'david')
>>> type(b)
<class 'tuple'>
>>> 

posted on 2016-01-19 19:16  251744647  阅读(136)  评论(0编辑  收藏  举报

导航