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\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的更多相关文章
- python基础03序列
sequence 序列 sequence序列是一组有顺序的元素的集合 (严格的说,是对象的集合,但鉴于没有引入对象的概念,暂时说元素) 序列可以包含一个或多个元素,也可以没有任何元素 我们之前所说的基 ...
- Python基础03 序列
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! sequence 序列 sequence(序列)是一组有顺序的元素的集合 (严格的 ...
- Python基础03
while循坏while属于条件判断 条件满足====>执行 条件不满足====>退出循环 whlie循环格式 while 条件 : 执行语句 while 1 == 1: print(&q ...
- Python 基础03 序列
sequence 序列 sequence(序列) 是一组有顺序的元素的集合 (严格的说,是对象的集合,但鉴于我们还没有引入"对象" 概念,暂时说元素) 序列可以包含一个或多个元素, ...
- Python基础03 id
id id(x)对应变量x所引用对象的内存地址.可以把id(x)看成变量x的身份标识. is 有时在编程中需要与变量的身份标识打交道,但不是通过 id 函数,而是 is 操作符. The operat ...
- python基础教程
转自:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html Python快速教程 作者:Vamei 出处:http://www.cn ...
- python基础——错误处理
python基础——错误处理 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数 ...
- python基础——获取对象信息
python基础——获取对象信息 当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type( ...
- python基础——使用模块
python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...
随机推荐
- iOS获取手机当前的网络状态
获取iOS网络状态,目前有两个办法. 1.通过监听手机状态栏的信息. 2.通过使用官方提供的类Reachability. 一.通过手机监听手机状态栏的信息 好处: 1.可以通过苹果的审核上架AppSt ...
- uva 639 Don't Get Rooked 变形N皇后问题 暴力回溯
题目:跟N皇后问题一样,不考虑对角冲突,但考虑墙的存在,只要中间有墙就不会冲突. N皇后一行只能放一个,而这题不行,所以用全图暴力放棋,回溯dfs即可,题目最多就到4*4,范围很小. 刚开始考虑放一个 ...
- .Net 利用消息在进程间通讯实现进程互操作
有时候我们会遇到需要在两个进程间通过某种方式实现互操作,方法有很多,例如你可以尝试让两个进程持续监视一个外部文件,由此文件记录各自进程的数据:还有可以使用网络端口实现进程间通讯.共享一片内存区域记录及 ...
- 基于Jquery的Ajax分页,只有上一页和下一页
最近项目中用到ajax分页 在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接 http://www.zhangxinxu.com/wordpress/2010/ ...
- 配置tomcat的https通信(单向认证)
1.首先用jdk带的工具生成证书库 打开cmd命令行窗口,cd 到tomcat安装目录的bin下面执行 keytool -v -genkey -alias tomcat -keyalg RSA -ke ...
- matlab最小二乘法数据拟合函数详解
定义: 最小二乘法(又称最小平方法)是一种数学优化技术.它通过最小化误差的平方和寻找数据的最佳函数匹配.利用最小二乘法可 以简便地求得未知的数据,并使得这些求得的数据与实际数据之间误差的平方和为最小. ...
- BZOJ 1021: [SHOI2008]Debt 循环的债务( dp )
dp(i, j, k)表示考虑了前i种钱币(从小到大), Alice的钱数为j, Bob的钱数为k, 最小次数. 脑补一下可以发现, 只有A->B.C, B->A.C, C->A.B ...
- 初来乍到--------smarty
Smarty Day01-----smarty的使用 作用:把php+html分离,程序和美工 使用: 1.下载源码包 2.目录结构 libs 源码文件 Smarty.class.php templa ...
- Codeforces 700B Connecting Universities(树形DP)
[题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...
- swith
“开关”(Switch)有时也被划分为一种“选择语句”.根据一个整数表达式的值,switch语句可从一系列代码选出一段执行.它的格式如下: switch(整数选择因子) { case 整数值1 : 语 ...