python数据类型及转换

遍历字典python

dic1 = {'a':100, 'b':100, 'c':100, 'd':100}git

例子1函数

for k in dic1:ui

    print korm

例子2ip

for k in dic1:ci

    print k dic1[k]字符串

例子3input

for k in dic1:string

#,为去掉换行符

    print "%s-->%s" % (k, dic1[k]), 

例子4

#使用iteritems()和for循环遍历字典中的值

for k, v in dic1.iteritems():

    print k, v

例子5

#使用for循环写乘法口诀

for i in xrange(1,10):

    for k in xrange(1,i+1):

        print "%s X %s = %s" % (i, k, i*k),

    print

for循环的else退出,for循环结束后才会执行else内容

例子6

#for循环的几种语法用法

import sys

import time

for i in xrange(10):

    if i == 1:

        continue

    elif i == 3:

        pass

    elif i == 5:

       break

    elif i == 7:

        sys.exit()

else:

    print i

    time.sleep(1)

print "2"

例子7使用for循环遍历文件内容

#!/usr/bin/python

fd = open('/work/python/2.txt')

for line in fd:

    print line,

 

while循环使用在有条件的循环

例子1

#!/usr/bin/python

x = ''

while x != 'q':

    print 'hello'

    x = raw_input("please input :")

    if not x:

        break

    if x == 'quit'

        continue

    print 'continue'

else:

    print 'world'

使用while循环遍历文件

例子2

fd = open('/work/python/1.txt')

while True:

    line = fd.readline()

    if not line:

        break

    print line,

fd.close()

例子2使用with语法打开文件,能够不用close关闭文件

with open('/work/python/1.txt') as fd:

    while True:

        line = fd.readline()

        if not line:

            break

        print line,

整型(int型,只保存整数)

经常使用函数及方法

可以使用abs()函数取绝对值

abs(...)

    abs(number) -> number

    Return the absolute value of the argument.

例子:

a=-10

print (abs(a))

输出为10

可以使用dir()函数查看该整型有哪些方法可使用

dir(...)

    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.

    Else, return an alphabetized list of names comprising (some of) the attribut

es

    of the given object, and of attributes reachable from it.

    If the object supplies a method named __dir__, it will be used; otherwise

    the default dir() logic is used and returns:

      for a module object: the module's attributes.

      for a class object:  its attributes, and recursively the attributes

        of its bases.

      for any other object: its attributes, its class's attributes, and

        recursively the attributes of its class's base classes.

print (dir(a))

 

浮点型(float型,能够保存小数)

经常使用函数及方法

round函数

round(...)

    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).

    This always returns a floating point number.  Precision may be negative.

b=2.3333

print (round(b))

输出为2

 

布尔型

下面是python中布尔操做:
    x or y:if x is false,then y, else x
    x and y:if x is false, then x, else y
    not x:if x is false, then True, else False

 

python的字符串和经常使用方法

字符串是有下标的,经常使用方法

a='1234567'

print (a[0],a[5])

输出1 6

 

find()查找

a='1234567'

print (a.find('45'))

输出3 返回子字符串下标

 

join()插入

print ('99'.join('aaa'))

输出a99a99a

 

split()拆分

a='1234567'

print (a.split('45'))

输出['123', '67']列表

 

replace()替换

a='1234567'

print (a.replace('123','abc'))

输出abc4567

 

strip()去掉字符串先后空格

b='     a b c    '

print (b.strip())

输出 a   b   c

 

format()

print "{0} is {1} years old".format("FF", 99) 

print "{} is {} years old".format("FF", 99) 

print "Hi, {0}! {0} is {1} years old".format("FF", 99)

print "{name} is {age} years old".format(name = "FF", age = 99)

相关文章
相关标签/搜索