复习:
条件判断 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\test.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\test.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\test.py
import sys
print sys.argv

执行结果:
C:\Users\***>python d:\python\test.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\test.py
def doublePrint(x):
    print "doublePrint(x) is", x*2
x=raw_input("Enter something x : ")
doublePrint(x)

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

C:\Users\***>python d:\python\test.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\test.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\test.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\test.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\test.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\test.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

0610 python 基础03的更多相关文章

  1. python基础03序列

    sequence 序列 sequence序列是一组有顺序的元素的集合 (严格的说,是对象的集合,但鉴于没有引入对象的概念,暂时说元素) 序列可以包含一个或多个元素,也可以没有任何元素 我们之前所说的基 ...

  2. Python基础03 序列

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! sequence 序列 sequence(序列)是一组有顺序的元素的集合 (严格的 ...

  3. Python基础03

    while循坏while属于条件判断 条件满足====>执行 条件不满足====>退出循环 whlie循环格式 while 条件 : 执行语句 while 1 == 1: print(&q ...

  4. Python 基础03 序列

    sequence 序列 sequence(序列) 是一组有顺序的元素的集合 (严格的说,是对象的集合,但鉴于我们还没有引入"对象" 概念,暂时说元素) 序列可以包含一个或多个元素, ...

  5. Python基础03 id

    id id(x)对应变量x所引用对象的内存地址.可以把id(x)看成变量x的身份标识. is 有时在编程中需要与变量的身份标识打交道,但不是通过 id 函数,而是 is 操作符. The operat ...

  6. python基础教程

    转自:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html Python快速教程 作者:Vamei 出处:http://www.cn ...

  7. python基础——错误处理

    python基础——错误处理 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数 ...

  8. python基础——获取对象信息

    python基础——获取对象信息 当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type( ...

  9. python基础——使用模块

    python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...

随机推荐

  1. 汇编程序hello world

    我们用C,C++,Java,C#等这样一些高级语言时一般会用到一个集成开发环境,啥编译链接之类的操作都集成到一起了,IDE给你自动完成了.随便点几下按钮就编译好运行起来了. 那假如是写了几行汇编代码该 ...

  2. PLSQL连接Oracle数据库,使用instantclient_10_2客户端

    使用instantclient_10_2作为PLSQL客户端配置步骤如下:1.将instantclient_10_2文件夹copy到PLSQL的安装目录下(如D:\Program Files\PLSQ ...

  3. MVC上传文件受限制

    mvc自带设置4M一下的字体可以上传,4M以上的字体需要对web.config进行设置 <system.web> <httpRuntime targetFramework=" ...

  4. 5.7.1.4 window对象

    ECMAScript虽然没有指出如何直接访问Global对象,但web浏览器都是将这个全局对象作为window对象的一部分加以实现的.因此,在全局作用域中声明的所有变量和函数,就都成为了window对 ...

  5. less 工具

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  6. php知识--递归

    <?php // /* * 遍历输出文件夹中的所有内容 * @param1 string $dir,要遍历的路径 * @param2 int $level = 0,当前的级别 */ functi ...

  7. unity3d 2d游戏制作的模式

      经过了4个月不懈的努力,我和图灵教育合作的这本3D游戏开发书预计下个月就要出版了.这里MOMO先打一下广告,图灵的出版社编辑成员都非常给力,尤其是编辑小花为这本书付出了很大的努力,还有杨海玲老师, ...

  8. nyist 500 一字棋

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=500 这太并不难,只要把情况分清楚就可以了,本人由于考虑不是很周全,WA了n次....悲 ...

  9. VS Code - Debugger for Chrome

    VS Code - Debugger for Chrome调试JavaScript的两种方式   VS Code - Debugger for Chrome调试JavaScript的两种方式 最近由于 ...

  10. 一起来看看css中的单位

    一起来看看css中的单位 由于一直以来对于css中的百分比单位究竟是相对于谁的比例,这个问题不是很了解,所以就专门找资料看了一下. <div class="container w500 ...