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 ...
随机推荐
- CSSOM View Module
就在8月份,也就是上次gf大姨妈来的时候,W3C出炉了CSSOM视图模块(CSS Object Model View)草案.CSSOM视图模块(CSSOM View Module)定义了一些 API, ...
- asp.net DropDownList实现ToolTip功能
在绑定DropDownList控件时,可能出现绑定显示的文本过长以至于超过控件长度的内容看不到,这时候就需要使用ToolTip完成其功能,即鼠标放到相应选项后就可显示其完成内容. 首先,在页面引入jQ ...
- 读写分离提高 SQL Server 并发性
转自:http://www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=476 在一些大型的网站或者应用中,单台的SQL Serv ...
- CentOS配置ftp服务器
1.首先更新yum 源 yum update 2. 安装vsftpd yum install vsftpd -y // 安装 移除是 yum remove vsftpd 3. 配置Vsftp ...
- opacity在IE6~8下无效果,解决的办法
opacity在IE6~8下无效果,解决的办法 问题出现时rgba()在ie6下不出效果,最后查到是opacity的问题. opacity是css3时出现的,目前主流浏览器都支持.but老IE是个麻烦 ...
- English - therefore,so,hence,then,accordingly,thus用法解析
这几个词的区别大致可从以下几方面去看: 1.therefore adv.因此, 所以=for that reason=consequently常用于连接两个并列分句,其前加“and”或分号“:”.He ...
- leetcode 31. Next Permutation(字典序的下一个)
描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- Ztree异步树加载
JSP代码片段 <%@ page language="java" contentType="text/html; charset=utf-8" pageE ...
- APM代码学习笔记1
libraries目录 传感器 AP_InertialSensor 惯性导航传感器 就是陀螺仪加速计 AP_Baro 气压计 居然支持BMP085 在我印象中APM一直用高端的MS5611 AP_Co ...
- 【原创】Libjpeg 库使用心得(一) JPEG图像DCT系数的获取和访问
[原创]继续我的项目研究,现在采用Libjpeg库函数来进行处理,看了库函数之后发现C语言被这些人用的太牛了,五体投地啊...废话不多说,下面就进入正题. Libjpeg库在网上下载还是挺方便的,这里 ...