复习:
条件判断 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. getHibernateTemplate().saveOrUpdate 不运行

    在ssh中使用hibernateTemplate来保存对象的时候.出现一个问题,就是saveOrUpdate既不报错.也不在控制台打印插入语句,也不想数据库插入数据. 问题解决: 这个是事务的原因.检 ...

  2. Android解析XML

    在Android平台上可以使用Simple API for XML(SAX) . Document Object Model(DOM)和Android附带的pull解析器解析XML文件. 下面是本例子 ...

  3. C#高效导出Excel(IList转DataTable,DataSet)

    微软的Excel操作类导出Excel会很慢,此方法简单的把表中内容以字符串的形式写入到Excel中,用到的一个技巧就是"\t". C#中的\t相当于Tab键,写入到Excel中时就 ...

  4. 红豆带你从零学C#系列之:使用集合组织相关数据

    ArrayList(数组列表) Why:如果一个公司有5名员工,一般我们会用长度为5的对象数组来存储信息,但要是有新员工来了,5个长度的数组就不够用了,因此我们需要一种能够根据需要自动分配容量的动态数 ...

  5. TCP/IP的网际层协议——ARP

    MAC地址对于每一台设备是全球唯一的,该地址被烧录在网卡的硬件电路上.MAC地址由12位十六进制数表示,其中前6位标识网卡的制造厂商,后6位是网卡的序列号.在以太网中,一个主机要和另一个主机进行通信, ...

  6. vim vimrc

    set nu set shiftwidth= set tabstop= set softtabstop= set autoindent set cindent set smartindent file ...

  7. SVN创建分支

    工具:TortoiseSVN 创建一个空白项目,例如OA 从客户端检出OA,在OA文件夹下新建三个子文件夹 trunk:存放开发的主线,团队成员在开发的时候一直要用这个库中的内容 branches:存 ...

  8. MYSQL 的数据读取方式

    例子: create table T(X bit(8)); insert into T (X) values(b'11111111'); select X from T; 这个时候会发现这个X 是乱码 ...

  9. underscore api 概览

    underscore 集合函数(数组或对象) _.each(list, iteratee, [context]); _.map(list, iteratee, [context]); _.reduce ...

  10. C++读写文件流的相关介绍

    掌握文本文件读写的方法了解二进制文件的读写方法 C++文件流:fstream // 文件流ifstream  // 输入文件流ofstream  // 输出文件流 //创建一个文本文件并写入信息//同 ...