0610 python 基础03

复习:
条件判断 if..else
>>> age=28
>>> if age<18:
...   print "你还没有成年吧"
... else:
...   print "你已经是成人了"
...
你已经是成人了

while 死循环,当输入OK时跳出循环
>>> while True:
...   str=raw_input("请输入你要的内容:")
...   if str=="ok":
...     break
...   print str
...
请输入你要的内容:hello
hello
请输入你要的内容:haha
haha
请输入你要的内容:ok

python 定义变量无需声明数据类型
>>> str1="hello"
>>> print str1*2
hellohello
>>> type(str1)
<type 'str'>
>>> num=9
>>> print num*10
90
>>> type(num)
<type 'int'>

输入点什么东西,判断并打印其数据类型
>>> import types
>>> if type("hello")== types.StringType:
...   print "ok"
...
ok

# -*- coding: utf-8 -*-
# D:python est.py
import types
str1 = 55
if type(str1) is types.IntType:
    print "This is IntType"
elif type(str1) is types.StringType:
    print "This is StringType"
else:
    print "Sorry, I don't know."

C:Users***>python d:python est.py
This is IntType

引入包
>>> import sys
>>> print sys.path
['', 'C:\Windows\system32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27
lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27
', 'C:\Python27\lib\site-packages']

python path指什么?
python程序,python中引用其他程序,安装目录下的一些东西

>>> import sys
>>> dir(sys) # 使用内建的dir函数来列出模块定义的标识符(函数、类和变量)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__s
tderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_g
etframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorde
r', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_
write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix
', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckint
erval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursi
onlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversi
on', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'pa
th', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'p
y3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace',
'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptio
ns', 'winver']
>>> print sys.version
2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)]

>>> help(sys.path) # 查看某个东西的作用

# -*- coding: utf-8 -*-
# D:python est.py
import sys
print sys.argv

执行结果:
C:Users***>python d:python est.py 111 222
['d:\python\test.py', '111', '222']

定义一个函数
>>> x=50
>>> def printSth(x):
...   print x
...
>>> printSth()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: printSth() takes exactly 1 argument (0 given)
>>> printSth(x)
50
>>> printSth(100)
100
>>> printSth("hello")
hello
>>> printSth("2.00")
2.00

# -*- coding: utf-8 -*-
# D:python est.py
def doublePrint(x):
    print "doublePrint(x) is", x*2
x=raw_input("Enter something x : ")
doublePrint(x)

C:Users***>python d:python est.py
Enter something x : hello
doublePrint(x) is hellohello

C:Users***>python d:python est.py
Enter something x : 42
doublePrint(x) is 4242

return 语句
>>> def sum(a,b):
...   return a+b
...
>>> print sum(10,35)
45

加减乘除:add , subtract , multiply , divide
addition , subtraction , multiplication , division

编写两个数乘法和除法
>>> a=100
>>> b=4
>>> def multiply(a,b):
...   return a*b
...
>>> print "The multiplication a and b is", multiply(a,b)
The multiplication a and b is 400
>>> def devide(a,b):
...   return a/b
...
>>> print "The devision a and b is",devide(a,b)
The devision a and b is 25
>>> print devide(2,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in devide
ZeroDivisionError: integer division or modulo by zero

# -*- coding: utf-8 -*-
# D:python est.py
def multiply(a,b):
    return a*b

def devide(a,b):
    if b==0:
        # break
        print u"sorry, b 不能为 0"
    else:
        return a/b
a=int(raw_input("Enter a int number a: "))
b=int(raw_input("Enter a int number b: "))
print "The a*b is", multiply(a,b)
print "The a/b is", devide(a,b)

运行结果:
C:Users***>python d:python est.py
Enter a int number a: 10
Enter a int number b: 4
The a*b is 40
The a/b is 2

C:Users***>python d:python est.py
Enter a int number a: 2
Enter a int number b: 0
The a*b is 0
The a/b is sorry, b 不能为 0
None

输入两个、三个数比较大小,输出较大
# -*- coding: utf-8 -*-
# D:python est.py
a=int(raw_input("Enter a: "))
b=int(raw_input("Enter b: "))
c=int(raw_input("Enter c: "))

def sortTwo(a,b):
    if a>b:
        return a
    else:
        return b
print "The bigger in a and b is", sortTwo(a,b)

def sortThree(a,b,c):
    if sortTwo(a,b)>c:
        return sortTwo(a,b)
    else:
        return c
print "The biggest in a,b and c is", sortThree(a,b,c)

运行结果:
C:Users***>python d:python est.py
Enter a: 39
Enter b: 24
Enter c: 88
The bigger in a and b is 39
The biggest in a,b and c is 88

原文地址:https://www.cnblogs.com/blueskylcc/p/5545673.html