码迷,mamicode.com
首页 > 编程语言 > 详细

python基本语法

时间:2014-11-06 17:41:11      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:python   脚本   

语法特点:
-------------------------------------------------------------
严格的缩进
定义变量直接写变量名
没有++ -- &&  || 代替 and or not
判断条件后需要加:
函数内使用函数外的全局变量需要加global

4种数据类型——整数、长整数、浮点数和复数:
---------------------------------------------------------------------
2是一个整数的例子。
长整数不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10 -4 。
(-5+4j)和(2.3-4.6j)是复数的例子。

字符串:单引号‘‘  双引号""  三引号‘‘‘ ‘‘‘
-----------------------------------------------------------------------------
单引号与双引号一样,三引号可以在多行写
字符串与字符串相连:两字符串相邻会自动相加
字符串与变量相连,通过,会自动在两者之间加空格

转义符:\ 同时也是换行符   r或R 忽略转义
------------------------------------------------------------------------------

控制语句:
------------------------------------------
for:
----------------------------------------------------------------
number = 23
guess = int(raw_input(‘Enter an integer : ‘))
if guess == number:
     print ‘Congratulations, you guessed it.‘ # New block starts here
     print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
     print ‘No, it is a little higher than that‘ # Another block
else:
     print ‘No, it is a little lower than that‘
print ‘Done‘  

while:
----------------------------------------------------------------------------------------------
number = 23
running = True
while running:
     guess = int(raw_input(‘Enter an integer : ‘))
     if guess == number:
         print ‘Congratulations, you guessed it.‘
         running = False # this causes the while loop to stop
     elif guess < number:
         print ‘No, it is a little higher than that‘
     else:
         print ‘No, it is a little lower than that‘
else:
     print ‘The while loop is over.‘

for:
----------------------------------------------------------------------------------------------
for i in range(1, 5):
     print i
else:
     print ‘The for loop is over‘

break:
-----------------------------------
while True:
     s = raw_input(‘Enter something : ‘)
     if s == ‘quit‘:
          break
     print ‘Length of the string is‘, len(s)
print ‘Done‘

continue:
-------------------------------------------
while True:
     s = raw_input(‘Enter something : ‘)
     if s == ‘quit‘:
          break
     if len(s) < 3:
         continue
print ‘Input is of sufficient length‘

函数:
--------------------
函数内使用外部变量加global
默认值直接在参数后用=相连
默认返回None
-----------------------------------
def say(message="hello world"):
     print ‘say‘,message
sayHello("hello")
--------------------------
def func(a, b=5, c=10):
     print ‘a is‘, a, ‘and b is‘, b, ‘and c is‘, c
func(3, 7)
func(25, c=24)
func(c=50, a=100)
------------------------------
def maximum(x, y):
     if x > y:
         return x
     else:
         return y
print maximum(2, 3)

模块:
----------------
每个python脚本都算是一个模块,我们可以在自己的脚本里引入其它模块,这样我们就可以直接使用引入模块的功能了。
与java相比,类似于每个python脚本是一个java类,我们在自己的类里面导入别的类,自己定义模块就相当于自己写个类然后在
另外一个类文件引入
------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python
#coding=utf-8   
import sys        #使用模块 sys模块包含了与Python解释器和它的环境有关的函数
print sys.argv
-----------------------------------
dir()函数:
—————-----
列出模块内所有的属性和函数



              

python基本语法

标签:python   脚本   

原文地址:http://blog.csdn.net/wangjuntytl/article/details/40861757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!