python开发技术详解(二)

文章目录:

  1. 数据类型
  2. 控制语句

数据类型:

 先看两个例子:

1>

a=1
b=1
print id(a)
print id(b)

Out[1]: 163578032

Out[2]: 163578032

2>

a=1

print id(a)
a=2

print id(a)

Out[1]: 150876336

Out[2]: 150876324

 第一个表明python 一切皆对象,其中 id(1),id(a),id(b)都是同一个对象的拷贝。

第二个表明python 一切皆对象,重新复制的过程不仅仅是变量引用地址的改变,而是整个对象的重新赋值的过程。

python的基本类型有四种: int,float,bool,long(长整型) 

复合类型:字符串类型string,复数类型complex(7+8j)

数据运算:

1,求 1/2结果的浮点值:

from __future__ import division
1/2

2,2的三次方:

  2**3


 控制语句:

  这个环节不会过多的描述.

  if elif else

  for

  while;

  break,continue;

  

  看了这么多,那么问题来了?

  1,input,raw_input 都是获取用户输入,他们有什么区别?

  2,通过字段实现 c语言中的 switch 功能?

   

'''
实现C语言中的switch功能
'''
class switch(object):
    def __init__(self,value):
        self.value=value
        self.fall=False
        
    def __iter__(self):
        yield self.match
        raise StopIteration
        
    def match(self,*args):
        if self.fall or not args:
            return True
        elif self.value in args:
            self.fall=True
            return True
        else:
            return False
x,y=4,5    
for case in switch('-'):
    if case('-'):
        print x-y
        break
    if case('+'):
        print x+y
        break
        

 3,冒泡排序:

a=[1,6,5,6,7,8,9,3,2,4,6,8]

#第一次排序,最大的石头沉下去了
for i in xrange(len(a)-1):
    if a[i]>a[i+1]:
        tmp=a[i]
        a[i]=a[i+1]
        a[i+1]=tmp
#第二次排序
for i in xrange(len(a)-2):
    if a[i]>a[i+1]:
        tmp=a[i]
        a[i]=a[i+1]
        a[i+1]=tmp
               
for i in a:
    print i
a=[1,6,5,6,7,8,9,3,2,4,6,8]

#冒泡排序:
for i in xrange(len(a)-1):
    goOn=True
    for j in xrange(len(a)-i-1):
        if a[j]>a[j+1]:
            tmp=a[j]
            a[j]=a[j+1]
            a[j+1]=tmp
            goOn=False
    if goOn:
        break
    print a
    print '------------'

  

原文地址:https://www.cnblogs.com/canbefree/p/4015146.html