python笔记(一)

一.
(1).hello World!
1.打开编辑器,输入以下代码:
print 'hello World!'
2.另存为 hellowolrd.py
3.存到指定目录,打开终端,输入:python hellloworld.py
(2)程序注释
#
二.
(1)数
在Python中有4种类型的数——整数、长整数、浮点数和复数。
2是一个整数的例子。
长整数不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子。
(2)字符串
●使用单引号(')
你可以用单引号指示字符串,就如同'Quote me on this'这样。所有的空白,即空格和制
表符都照原样保留。
●使用双引号(")
在双引号中的字符串与单引号中的字符串的使用完全相同,例如"What's your name?"。
●使用三引号('''或""")
利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。例如:
'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''
(3)变量
既可以存储信息,有可以进行操作。
变量是标识符的例子。用来识别某样东西的名字。
标识符的命名规则:
1.标识符的地一个字母是大小写字母或者下划线(_)
2.标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘ _’)。
3.标识符名称的其他部分可以由字母(大写或小写)、下划线(‘ _ ’)或数字(0-9)组成。
4.标识符名称是对大小写敏感的。例如,myname和myName不是一个标识符。注意前者中的小写n和后者中的大写N。
有效 标识符名称的例子有i、__my_name、name_23和a1b2_c3。
无效 标识符名称的例子有2things、this is spaced out和my-name。
(4)数据类型
python 中有两种数据类型:数和字符串。
(5)对象
Python把在程序中用到的任何东西都称为对象。python是完全面向对象的。
如何编写Python程序,标准流程。
. 打开你最喜欢的编辑器。
. 输入例子中的程序代码。
. 用注释中给出的文件名把它保存为一个文件。我按照惯例把所有的Python程序都以扩展名.py保存。
. 运行解释器命令python program.py或者使用IDLE运行程序
(6)逻辑行和物理行
逻辑行就是python所认定的行,物理行是编写的程序的人认定的行。

 

#Filename:var.py
i=5
print i
i=i+1
print i

s='''This is a multi-line string. 
      This is the second line.'''
print s

s1='la'*3
print s1

length=5
width=10
area=length*width
print '***************************'
print 'Area is ',area
print 'Perimeter is',2*(length+width)

number=23
guess=int(raw_input("Enter an integer:"))
if guess==number:
    print 'Congratulations!You guessed it!'
    print 'But you do not win any prizes '
elif guess < number:
    print 'No ,it is a little lower than that'
else:
   print 'No,it is a little higher than that'    
print 'DONE'



number=30
running=True
while running:
   guess=int(raw_input("Enter an integer:"))
   if guess==number:
       print"Congratulations,you guessed it"
       running=False
   elif guess<number:
       print'No,it is a little higher than that '
   elif guess>number:
       print 'No ,it is a little lower than that ' 
       
   else:
        print 'The while loop is  over'
        
print 'Done'

for i in range(0,100,2):  
   print i
else:
    print 'Done'
     
while True:
    s=raw_input("Enter something:")
    if s=="quit":
       break
    print 'Lenght of the string is ',len(s)
print 'Done!'

while True:
    s=raw_input('Enter somthing:')
    if s=='quit':
         break;
    if len(s)<3:
      continue
print 'Input is of sufficient length'
 
def sayhello():
    print 'hello!'
    
sayhello()

def printMax(a,b):
    if a>b:
        print a,'is maximum'
    else:
        print b, 'is maximum'
printMax(12,89)

x=5
y=7
printMax(x,y)

def func(x):
     print 'x is ',x
     x=2
     print 'Changed local x to',x
     
     
x=50

func(x)
print 'x is still',x

def func1():
     global x
     print 'x is ',x
     x=2
     print 'Changed local x to',x
     
     
x=50

func1()
print 'x is still',x

def say(mes,times=1):
    print mes*times
 
say('hello')
say('World ',5)

def func2(a,b=5,c=10):
    print 'a is',a,'b is ',b,'c is' ,c
func2(3,7)
func2(25,c=30)
func2(c=50,a=100)

def maxmin(x,y):
     if x>y:
          return x
     else:
          return y
print maxmin(30,20)

def printMax2(x,y):
     '''
        Prints the maximum of two numbers
        The two values must be integers
     '''
     x=int(x)
     y=int(y)
     
     if x>y:
         print x,'is maximum'
     else:
         print y,'is maximum'
printMax2(3,5)
print printMax2.__doc__

 

 

 


 

posted @ 2013-12-15 11:00  CSER_think more  阅读(276)  评论(0编辑  收藏  举报