python 1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 1相关的知识,希望对你有一定的参考价值。



python 1

支持tab键补全

# vim /usr/local/bin/tab.py

  1 import readline
  2 import rlcompleter
   3
  4 readline.parse_and_bind(‘tab: complete‘)

# vim ~/.bash_profile

 14 PYTHONSTARTUP=/usr/local/bin/tab.py
 15 export PATH PYTHONSTARTUP

# source ~/.bash_profile

# python
>>> p  (两下tab)
pass       pow(       print      print(     property(



>>> if 3 > 0:   (顶语句)
...  print ‘no‘  (缩进)
...
no



>>> if 10 > 5:
...  print "ok"
...  print "yes"   (这两个都是上面的子语句,缩进要一样)
...
ok
yes

>>> if  ‘hello world!‘:
...  print 2
...
2



print 后面字母一定要用引号

>>> print "tom‘s a pet "
tom‘s a pet

>>> print ‘hello world!‘
hello world!
>>> print ‘hello    world!‘
hello    world!
>>> print ‘hello, world!‘
hello, world!
>>> print ‘hello‘, ‘world!‘
hello world!
>>> print ‘hello‘,   ‘world!‘
hello world!
>>> print ‘hello‘+ ‘world!‘
helloworld!





>>> username=raw_input("username:")     
username:xixi
>>> print username  (输出显示)
xixi
>>> username    (输出的是变量字符)
‘xixi‘


>>> 3 + 4
7
>>> ‘3‘ + "4"
‘34‘
>>> number=raw_input("number:")   记住:raw_input里面都是字符
number:10
>>> number
‘10‘
>>> number + 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate ‘str‘ and ‘int‘ objects
>>> number + ‘5‘
‘105‘
>>> int(number) + 5
15
>>> int(number)+5
15
>>> 3+5
8




[[email protected] python]# cat day01.py
#-*- coding: utf8 -*-
username=raw_input(‘username:‘)
print ‘Welocme‘, username
print ‘Welcome ‘+ username
print ‘你好‘ +  username

[[email protected] python]# python day01.py
username:tom
Welocme tom
Welcome tom
你好tom





————————————————————————————————————————————
(调用模块)

#vim /root/star.py
hi = ‘hello world‘

def pstar():                           函数
      print ‘*‘ * 20


#pyhton     (/root下)
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import star
>>> star.hi
‘hello world‘
>>> star.pstar()
********************



[[email protected] 桌面]# cat yy.py
hi = ‘hello ‘

def ps():
   print 1 * 20
[[email protected] 桌面]# python

>>> import yy
>>> yy.hi
‘hello ‘
>>> yy.ps()
20

——————————————————————————————————————————————
___________________________________________________________________________________________________

编辑模块的帮助说明:

[[email protected] python]# cat star.py
#-*- coding: utf8 -*-

‘‘‘演示程序                                       (加三引号 帮助说明)

这仅仅是一个包含变量和函数
‘‘‘

hi = ‘hello world‘


def pstar():
      ‘用于打印20个星号‘               (这是字符串,加引号)
      print ‘*‘ * 20

[[email protected] python]# python

>>> import star
>>> help(star)


Help on module star:

NAME
    star - 演示程序

FILE
    /root/python/star.py

DESCRIPTION
    这仅仅是一个包含变量和函数

FUNCTIONS
    pstar()
        用于打印20个星号

DATA
    hi = ‘hello world‘

——————————————————————————————————————————————————————




变量:第一个字符只能是大小写字母或下划线 后续字符只能是字母数字和下划线


>>> print 100
100
>>> 100 + 5
105
>>> 100 /5
20
>>> spedd = 100
>>> spedd+5
105
>>> a=10
>>> a=a+10
>>> a
20
>>> a +=1
>>> a
21
>>> -a
-21
>>> a
21
>>> 5/3
1
>>> 5.0/3
1.6666666666666667
>>> 5%3
2
>>> 5**2
25
>>> 5**3
125
>>> 5//3.0
1.0
>>> 5/3.0
1.6666666666666667
>>> 3==3
True
>>> 3=3
  File "<stdin>", line 1
SyntaxError: can‘t assign to literal
>>> 3>=3
True
>>> 3>3
False
>>> a=10
>>> if a=10 :
  File "<stdin>", line 1
    if a=10 :
        ^
SyntaxError: invalid syntax
>>> 3 !=4
True
>>> 10 <20 <30
True

>>> 10 <20 >15
True
>>> not 10*20 < 10+20 and 5 > 3
True
>>> not( 10*20 < 10+20) and 5 > 3
True                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
>>> not False and True
True










>>> True +1
2
>>> False +1
1
>>> 023
19
>>> 11
11
>>> 011
9
>>> 0x23
35
>>> 0b12
  File "<stdin>", line 1
    0b12
       ^
SyntaxError: invalid syntax
>>> 0b11
3
>>> 0b10
2

SyntaxError: EOL while scanning string literal
>>> os.chmod(‘day01.py‘,755)
>>> os.chmod(‘star.py‘,0755)

[[email protected] python]# ll
总用量 12
-rw-r--r--. 1 root root 133 3月  21 10:50 day01.py
-rw-r--r--. 1 root root 172 3月  21 11:29 star.py
-rw-r--r--. 1 root root 372 3月  21 11:29 star.pyc
[[email protected] python]# ll
总用量 12
--wxrw--wt. 1 root root 133 3月  21 10:50 day01.py
-rwxr-xr-x. 1 root root 172 3月  21 11:29 star.py
-rw-r--r--. 1 root root 372 3月  21 11:29 star.pyc





>>> name=‘tom‘
>>> name
‘tom‘
>>> print name
tom
>>> print ‘name‘
name
>>> print ‘hello %s‘ % name
hello tom
>>> print ‘%s is %s student‘ % (name,23)
tom is 23 student



>>> py_str=‘python‘
>>> py_str[1]
‘y‘
>>> py_str[0]
‘p‘
>>> len(py_str)
6
>>> py_str[5]
‘n‘

>>> py_str[-1]
‘n‘
>>> py_str[-6]
‘p‘

>>> py_str[2:4]  多取一个
‘th‘
>>> py_str[2:6]
‘thon‘
>>> py_str[2:10]
‘thon‘
>>> py_str[2:]
‘thon‘
>>> py_str[-4:]
‘thon‘
>>> py_str[-1:]
‘n‘
>>> py_str[-2:]
‘on‘
>>> py_str[-3:]
‘hon‘

>>> py_str[:]
‘python‘
>>> py_str[::2]
‘pto‘
>>> py_str[:2]
‘py‘

>>> py_str[1::2]
‘yhn‘
>>> py_str[::-1]
‘nohtyp‘
>>> py_str[:-1]
‘pytho‘


>>> py_str + ‘is good‘
‘pythonis good‘
>>> py_str *3
‘pythonpythonpython‘
>>> print ‘py_str *3‘
py_str *3
>>> print ‘%s *3‘% py_str
python *3

>>> ‘t‘ in py_str
True
>>> ‘th‘ in py_str
True
>>> ‘to‘ in py_str
False
>>> ‘to‘ not  in py_str
True

>>> ‘hello‘.upper()
‘HELLO‘
>>> py_str.upper()
‘PYTHON‘
>>> py_str.isalpha()
True
>>> py_str
‘python‘
>>> print ‘+%s+‘ % (‘-‘ * 48)
+------------------------------------------------+
>>> print "+%s+" % py_str.center(48)
+                     python                     +
>>> py_str.center(48)
‘                     python                     ‘
>>> py_str.center(48,"#")
‘#####################python#####################‘
>>> py_str.ljust(48,‘#‘)
‘python##########################################‘
>>> py_str.rjust(48,‘#‘)
‘##########################################python‘
>>> ‘         hello world!    ‘.strip()
‘hello world!‘
>>> ‘         hello world!    ‘
‘         hello world!    ‘
>>> ‘         hello world!    ‘.lstrip()
‘hello world!    ‘
>>> ‘         hello world!    ‘.rstrip()
‘         hello world!‘
>>> >>> ‘t‘ in py_str
  File "<stdin>", line 1
    >>> ‘t‘ in py_str
     ^
SyntaxError: invalid syntax
>>> True
True
>>> >>> ‘th‘ in py_str
  File "<stdin>", line 1
    >>> ‘th‘ in py_str
     ^
SyntaxError: invalid syntax
>>> True
True
>>> >>> ‘to‘ in py_str
  File "<stdin>", line 1
    >>> ‘to‘ in py_str
     ^
SyntaxError: invalid syntax
>>> False
False
>>> >>> ‘to‘ not  in py_str
  File "<stdin>", line 1
    >>> ‘to‘ not  in py_str
     ^
SyntaxError: invalid syntax
>>> True
True

>>> print "+%s+" %(‘-‘ * 48)
+------------------------------------------------+
>>> print "py_str%spy_str" %(‘-‘ * 48)
py_str------------------------------------------------py_str
>>> print "%s%s%s" %(py_str,"-" * 48,py_str)
python------------------------------------------------python









列表
>>> alist = [10,20,‘bob‘,[1,2]]
>>> len(alist)
4
>>> 10 in alist
True
>>> 1 in alist
False
>>> alist[2:4]
[‘bob‘, [1, 2]]
>>> alist[2:3]
[‘bob‘]
>>> alist[0:1]
[10]
>>> alist[0:2]
[10, 20]


>>> alist=[10,20,30,40,"bo","b"]

>>> alist[-1]=100  (该最后一个值)
>>> alist
[10, 20, 30, 40, ‘bo‘, 100]
>>> alist.append(200)   (追加一个值)
>>> alist
[10, 20, 30, 40, ‘bo‘, 100, 200]

>>> alist.insert(3,"tom")   (插入一个值)
>>> alist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100, 200]


>>> blist=[12,45,64,12,453]
>>> blist
[12, 45, 64, 12, 453]
>>> blist.sort()
>>> blist
[12, 12, 45, 64, 453]

>>> blist.pop()
453
>>> blist
[12, 12, 45, 64]


元组
>>> atuple=(10, 20, 30, ‘tom‘, 40, ‘bo‘, 100, 200)   (元组是用(),不可变)
>>> atuple
(10, 20, 30, ‘tom‘, 40, ‘bo‘, 100, 200)
>>> atuple[2:4]
(30, ‘tom‘)
>>> atuple[-1]=300
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ‘tuple‘ object does not support item assignment
>>> len(atuple)
8




字典
>>> adict={‘name‘: ‘bob‘,‘age‘:22}
>>> adict
{‘age‘: 22, ‘name‘: ‘bob‘}
>>> len(adict)
2
>>> 22 in adict
False
>>> ‘age‘ in adict
True
>>> adict[‘age‘]
22

    修改和增加
>>> adict[‘age‘]=25
>>> adict[‘age‘]
25
>>> adict[‘email‘]=‘[email protected]
>>> adict
{‘age‘: 25, ‘name‘: ‘bob‘, ‘email‘: ‘[email protected]‘}

>>> adict.get(‘phone‘)
>>> print adict.get(‘phone‘)
None
>>> adict.get(‘age‘)
25




>>> alist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100, 200]
>>> blist=alist
>>> blist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100, 200]
>>> blist.pop()
200
>>> blist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100]
>>> alist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100]
>>> clist=alist[::]
>>> clist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100]
>>> clist.pop()
100
>>> clist
[10, 20, 30, ‘tom‘, 40, ‘bo‘]
>>> alist
[10, 20, 30, ‘tom‘, 40, ‘bo‘, 100]





练习:

[[email protected] python]# vim if1.py
  1 a=10
  2
  3 if a>5:
  4  print ‘yes‘
  5 else:
  6   print ‘error‘

[[email protected] python]# python if1.py
yes



非0打印非空打印

>>> if -0.0:
...   print ‘yes‘
...   
...
>>> if 3:
...  print ‘yes‘
...
yes
>>> if ‘ ‘:
...  print ‘yes‘
...  
...
yes
>>> if ‘‘:
...  print ‘yes‘
...





(一个脚本如果输入的用户不是bob,密码不是123456,就报错,或就对的)
[[email protected] python]# vim if2.py
  1 username=raw_input(‘username:‘)
  2 password=raw_input(‘password:‘)
  3 if username=="bob" and password=="123456":
  4  print ‘login successful ‘
  5 else:  
  6  print ‘login incorrect‘
[[email protected] python]# python if2.py
username:bob
password:123456
login successful


[[email protected] python]# vim if3.py
  1 import getpass
   2
  3 username=raw_input(‘username:‘)
  4 password=getpass.getpass(‘password:‘)
   5
  6 if username=="bob" and password=="123456":
  7  print ‘login successful ‘
  8 else:
  9  print ‘login incorrect‘
[[email protected] python]# python if3.py
username:bob
password:
login successful

本文出自 “12336621” 博客,请务必保留此出处http://12346621.blog.51cto.com/12336621/1908971

以上是关于python 1的主要内容,如果未能解决你的问题,请参考以下文章

1Python环境安装部署

1Python简史

1python的安装

1python的基础

day-1Python基础

1python 的预备