2019独角兽企业重金招聘Python工程师标准>>>

遍历字典

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

例子1

for k in dic1:

print k

例子2

for k in dic1:

print k dic1[k]

例子3

for k in dic1:

#,为去掉换行符

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)

转载于:https://my.oschina.net/u/3713142/blog/1583183

python数据类型及转换相关推荐

  1. python数据类型的转换

    python数据类型的转换 在python编程过程中,我们会经常用到对不同数据类型的转换,本章节将会跟大家一起学习和分享,python 内置函数的类型转换,接下来就开始一起学习吧,朋友. int(x[ ...

  2. python如何强制转换数据类型,python数据类型强制转换的方法

    python数据类型强制转换的方法 发布时间:2020-06-22 16:43:25 来源:亿速云 阅读:101 作者:清晨 这篇文章将为大家详细讲解有关python数据类型强制转换案例,小编觉得挺实 ...

  3. python数据类型的转换_python 数据类型间转换

    自动类型转换: 数字类型精度从低到高(默认从高进度到低精度): bool 例 True+1 => 2 False + 1 => 1 3+3.14 =>6.14 3+(3+4j) =& ...

  4. matlab中float类型的_【Python基础学习】2. 变量、基本数据类型及其转换

    知行校园汇 记录学习.分享干货.吐槽人生! 关注 相关说明 文章内容:变量.基本数据类型及其转换 作者博客:csdn.cxhit.com www.cxhit.com 1. 变量 1.1 变量规则 在P ...

  5. python 为什么要用astype()函数对numpy数据类型进行转换,而不直接指定其dtype?float(64) float(32) int(64) int(32)(转换为整型int)

    numpy中的数据类型转换,不能直接改原数据的dtype! 只能用函数astype().否则你的元素个数可能会倍增或倍减,数值也会对应不上! 第一种情况: import numpy as npa = ...

  6. python数据类型转换原因_浅谈Python数据类型之间的转换

    Python数据类型之间的转换 函数 描述 int(x [,base]) 将x转换为一个整数 long(x [,base] ) 将x转换为一个长整数 float(x) 将x转换到一个浮点数 compl ...

  7. python数据类型之间的转换

    对python内置的数据类型进行转换时,可以使用内置函数,常用的类型转换函数如下 python常用类型转换函数 函数格式 使用示例 描述 int(x [,base]) int("8" ...

  8. python学习Day7 数据类型的转换,字符编码演变历程

    一.数据类型的转换 1.1.1.字符转列表:lst1 = str.split(默认空格,也可依据指定字符分界),若无分界字符,就没法拆分,这时可以直接放进list转成列表 ----> s1 = ...

  9. python基础-第1关数据类型与转换

    数据类型与转换(沟通语言) 数据类型 (1)字符串string 只要是被[单/双/三引号]这层皮括起来的内容,不论那个内容是中文.英文.数字甚至火星文.只要是被括起来的,就表示是字符串类型. ※字符串 ...

最新文章

  1. 基于视频理解TSM和数据集Kinetics-400的视频行为识别
  2. 献给新手的深度学习综述
  3. Deepin安装Curl的方法
  4. Oracle 实验7 存储过程
  5. 《You can do it!》的chapter 2笔记
  6. python创建txt文件_Mac怎么创建txt文件?教你设置新建txt的快捷键
  7. 如何打开*.caa文件?
  8. 分享几个vue后台模板
  9. RobotFramework与Eclipse集成
  10. 通赢A5管理系统服务器连不进,赢通软件A5A6系列管理系统参数设置说明
  11. Data Structures and Algorithm Analysis in C, Second Edition(《数据结构与算法分析》C语言版 第二版)——Mark Allen Weiss
  12. 基于单片机的水壶自动加热系统_基于单片机的智能热水壶设计 -
  13. php的ct表现,巨大垂体腺瘤(Pituitary adenoma)CT病例图片影像诊断分析
  14. GIS影像数据格式说明
  15. 【离散数学】最小生成树失败普罗米
  16. 苹果iPad Pro为什么选择激光雷达?
  17. 携手红帽拥抱开源,微软助力企业客
  18. 在linux下解压rar文件
  19. 一维消消乐c语言数据结构,Python数据结构:一维开心消消乐
  20. 通过外扫二维码判断手机是否有某App

热门文章

  1. 汇编 eax寄存器和AX,AH,AL之间的关系
  2. 带滤波器的PID控制仿真-3(Simulink仿真)
  3. STM32L431 立即睡眠模式(代码+讲解)
  4. 已解决SyntaxError: invalid syntax
  5. 三步完成两张网卡共享上网,区别于移动热点共享上网
  6. 设为首页,收藏本站写法
  7. Mac有滚动截图工具吗?----解救 MAC 使用者们的高效截图工具- Xnip
  8. 教你一个无本万利的赚钱方法_一个在股市中稳健赚钱的方法
  9. 大数据实训室课程体系设计案例分享
  10. 5-2 学习打卡(11.7)