Python第一课 用IDE实现第一个简单的程序

来源:互联网 发布:什么软件聊天 编辑:程序博客网 时间:2024/06/05 07:21

特别说明:本文章参考《跟老齐学Python》齐伟 著


Python中“万物皆对象”

对象:身份,类型,值


1.数字(严格的讲是对象)*********************


>>> 3
3
>>> id(3)#求对象的内存地址(身份)
27320616
>>> 3
3
>>> id(3)
27320616


>>> type(3)#求对象的类型
<type 'int'>
>>> type(3.0)#求对象的类型
<type 'float'>
#对象的值的话,因为这里的对象是数字,对象的值就是对象本身




大整数可以直接输
>>> 666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666
666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666L


2.变量(严格的讲是对象)*********************************************************



对象有类型,变量无类型
>>> x=3
>>> x
3
>>> x=3.4
>>> x
3.4
>>> x="hello, Python"
>>> x
'hello, Python'




3.四则运算**********************************************************************

整数之间的运算结果还是整数,除法结果是商


>>> 2+3
5
>>> 2-3
-1
>>> 2*3
6
>>> 3/2
1






>>> 2+3
5
>>> 2+3.0
5.0
>>> 2.0+3
5.0
>>> 2.0+3.0
5.0


4.大整数运算(Python不用考虑大整数运算的溢出问题)*********************************************************

>>> 666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666*999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666665999999333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333334L
>>> 2**10000 #2的10000次方
19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376L


计算机只能进行二进制的运算,我们输入的十进制的数,计算机先把十进制转换成二进制(这个过程理论上就无法精确表示浮点数了),然后再计算
>>> 0.1+0.1+0.1-0.3
5.551115123125783e-17
>>> 10.0/3
3.3333333333333335
要求精度比较高的话,可以使用decimal模块、fractions模块(基于有理数,1/3可以精确表示)、SciPy提供的Numerical Python包和其他用于数学和统计学的包




5.模块初识***************************************************************************



引用模块方法:
import module-name
from module1 import module11 #module11是module1模块中的子模块


引入模块os然后清屏


zsq@ubuntu:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("clear")


#显示结果(返回值是0)
0
>>>




#引用__future__中的division
>>> 3/2
1
>>> from __future__ import division
>>> 3/2
1.5


6.余数(整数和小数都适用)***************************************************************************



>>> 5%2
1
>>> 5.0%2
1.0
>>> divmod(5,2)
(2, 1)  #返回商和余数
>>> divmod(5.0,2)
(2.0, 1.0)


 

7.四舍五入********************************************************************************************



>>> round(1.235,2)#第二个数表示保留小数点后多少位
1.24
#下面这个结果不是Python的bug!(官方文档解释:This is not a bug:it's a result of the fact that most decimal fractions can't be represented exactly as a float.)
>>> round(1.2345,3)
1.234
>>> round(1.2345)#默认应该保留一位小数
1.0
>>> round(1.9)
2.0




8.再识模块之math模块***********************************************************************************



>>> import math
>>> math.pi #得到pi的值
3.141592653589793
>>> dir(math) #得到math模块中包含的函数,这个函数很有用,可以查看每个模块包含了什么功能的函数
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']




>>> help(math.pow) #查看math模块下的pow函数的功能,这个函数很有用,可以查看每个函数的功能
#返回结果
Help on built-in function pow in module math:


pow(...)
    pow(x, y)
    
    Return x**y (x to the power of y).




#对比下面的不同
>>> pow(3,2)
9
>>> math.pow(3,2)
9.0




>>> math.sqrt(9)
3.0
>>> math.floor(3.14)
3.0
>>> math.floor(3.92)
3.0
>>> math.fabs(-2)
2.0
>>> abs(-2)
2
>>> 5%3
2
>>> math.fmod(5,3)
2.0
>>> 


#求绝对值的函数
>>> abs(-2.3)
2.3
>>> abs(-2)
2
>>> abs(2)
2




9.Python中的运算符的优先级***************************************************************************

lambda lambda表达式
or 布尔“或”
and 布尔“与”
not x 布尔“非”
in, not in 成员测试
is, is not 同一性测试
<,<=,>,>=,!=,== 比较
| 按位或
^ 按位异或
& 按位与
<<, >> 移位
+, -
*, /, %
+x, -x 正负号
~x 按位翻转
** 指数幂
括号的优先级最高!!!!!!!!!!!!!!!

10.第一个Python程序



不经编译就可运行的程序,通常称为脚本程序。Python编写的程序就是脚本程序。
进入第一个最简单的Python程序了!!!!!!!!!!!!!!!!!!!!!
这里使用最low的编译器!!!!!!!


如果Python没有安装编译器,可以安装idle编译器:apt-get install idle
直接在命令行窗口输入idle即可进入idle编译器,然后新建一个文件,在文件中写代码:
print "Hello, World"

然后点击run运行,就可以在编译器中看到结果了


在命令行中运行这个Python程序的方法:
在Python文件所在的文件夹,运行命令行窗口,然后按照下面操作,来运行程序!


zsq@ubuntu:~/20171215$ ls
1.py
zsq@ubuntu:~/20171215$ python 1.py
Hello, World
This is my first Python program.


11.第二个Python程序


第二个Python程序:


#!/usr/bin/env python
#coding: utf-8
"""
这是我的一个python程序,想用中文注释的话,必须要有#coding: utf-8这一句

"""
a=19+2*4-8/2
print a



上面的程序中,
#!/usr/bin/env python这一行是必须写的,它能引导程序找到Python的解释器。