Python 数据类型和控制结构
Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候。不得不说的是Python的官方文档相当齐全,如果你是在Windows上学习Python,安装包自带的“Python Manuals”就是一份很好的学习资料(基本上不用去找其他资料了);尤其是其中的Tutorial,非常适合初学者。本文一方面总结了python语言的核心——数据类型和控制结构;另一方面,通过与其他语言的对比表达了我对Python的一些拙见。
数据类型
int, long, float, str, complex
>>> type(123)
<type 'int'>
>>> type(-234)
<type 'int'>
>>> type(123456123456)
<type 'long'>
>>> type(-123456123456)
<type 'long'>
>>> type(123.456)
<type 'float'>
>>> type('abc')
<type 'str'>
>>> type("hello, world")
<type 'str'>
>>> type(123456)
<type 'int'>
>>> type(123456789)
<type 'int'>
>>> type(1234567890)
<type 'int'>
>>> type(12345678901)
<type 'long'>
可以看到1234567890还是int,12345678901就是long了,说明int是有范围的。记得C/C++的int长度(4B)的同学都知道,C/C++里int的取值范围是:[-2^31, 2^31-1]也就是[-2147483648, 2147483647]。据此,我们可以看看Python的int范围:
>>> type(2147483647)
<type 'int'>
>>> type(2147483648)
<type 'long'>
>>> type(-2147483648)
<type 'int'>
>>> type(-2147483649)
<type 'long'>
这次试验说明,Python的int范围和C/C++一样。(事实上和long一样,这里只是因为运行的是32位的python解释器,如果是64位python解释器,int是8字节)
>>> type(1L)
<type 'long'>
>>> type(2l)
<type 'long'>
>>> type(123.456)
<type 'float'>
>>> type(123456123456.123456123456123456123456)
<type 'float'>
complex(复数)
>>> type(3+4j)
<type 'complex'>
>>> type(3+4J)
<type 'complex'>
>>> type(4j)
<type 'complex'>
>>> type(j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
>>> type(1j)
<type 'complex'>
但是1j不允许直接写成j,j会被当做name查找,如果没找到就会报错。
tuple, list, set, dict
>>> type([1, 2, 3])
<type 'list'>
>>> type({2, 3, 4})
<type 'set'>
>>> type((3, 4, 5))
<type 'tuple'>
>>> type({'key1': 'value1', 'key2': 'value2'})
<type 'dict'>
可以看到(), [], {}和它括起来的一系列元素,分别是表示:元组、列表、集合。而dict则是{key1: value1, [key2: value2, ...]}的形式。
>>> (1, 'two', 3.0)
(1, 'two', 3.0)
>>> [(1, 'two', 3.0), '4', 5]
[(1, 'two', 3.0), '4', 5]
>>> {1, 2L, 3.0, 4j}
set([1, 2L, 3.0, 4j])
>>> {1: 'one', 'one': 1}
{1: 'one', 'one': 1}
控制结构
顺序结构
语句
>>> print "hello, world"
hello, world
并且Python程序没有所谓的“入口”,这和多数脚本语言类似。
弱类型
>>> a = 123
>>> b = "asdf"
>>> c = [3, 4, 5]
>>> a
123
>>> b
'asdf'
>>> c
[3, 4, 5]
>>> a = b
>>> b
'asdf'
- 使用变量前不用向提前声明变量的类型
- 一个变量初始化为一个类型后还能给他赋其他类型的值
函数
def sayHello(name):
print 'Hello, ' + name + '!' sayHello('Jack')
这段代码的运行结果为:Hello, Jack!
类
class Man:
def __init__(self, name):
self.name = name def hello(self):
print 'Hello, ' + self.name + '!' m = Man('Jack')
m.hello()
这段代码也会输出:Hello, Jack!
类的更多特性和OOP有关,以后有时间再单独发一篇博文展示。
>>> type(sayHello)
<type 'function'>
>>> type(Man)
<type 'classobj'>
>>> type(m)
<type 'instance'>
>>> type(m.hello)
<type 'instancemethod'>
>>> type(Man.hello)
<type 'instancemethod'>
可以想象,Python世界里的东西都是”灰色“的,解释器对它们”一视同仁“,从来不以貌取人,只看他们现在身上的标签是什么~
选择结构
Python的选择结构以if开始。
bool
>>> type(1==1)
<type 'bool'>
>>> type(True)
<type 'bool'>
>>> type(False)
<type 'bool'>
对于Number(int, long, float, complex),0在if条件上也是False:
>>> if 1:
... print "true"
...
true
>>> if 0:
... print "true"
... else:
... print "false"
...
false
>>> if 0.0:
... print "0.0 is true"
...
>>> if 0j:
... print "0j is true"
...
提示:Python是以代码缩进区分代码块的
除此之外,空的string和空的集合(tuple, list, set)也是False:
>>> if '':
... print 'null string is true'
...
>>> if ():
... print 'null tuple is true'
...
>>> if []:
... print 'null list is true'
...
>>> if {}:
... print 'null set is true'
...
if, if-else & if-elif-else
>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
More
循环结构
for
>>> a = [1, 'two', 3.0]
>>> for i in a:
... print i
...
1
two
3.0
这种for迭代集合很方便。
>>> for i in range(1, 6):
... print i
...
1
2
3
4
5
>>> for i in range(10, 65, 10):
... print i
...
10
20
30
40
50
60
这里展示了range的两种调用形式,一种是range(a, b),它将返回一个从a(包含a)到b(不包含)的整数列表(list),另一种range(a, b, s),将返回一个a~b,以s为步长的list:
>>> range(1, 6)
[1, 2, 3, 4, 5]
>>> range(10, 65, 10)
[10, 20, 30, 40, 50, 60]
while
>>> i = 1
>>>
>>> while i < 5:
... i = i+1
... print i
...
2
3
4
5
顺便一提,Python里 i=i+1 不能写成i++,Python不支持这种语法;但可以写成 i += 1:
>>> i
5
>>> i += 1
>>> i
6
>>> i++
File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax
>>> ++i
6
>>> i
6
各位可能会疑惑,为什么++i可以?因为pyhon支持前置的+(正负号)运算,++被当做两次正运算了;同理,+++i,++++i都是一样的;我们可以顺便测一下负号运算:
>>> i
6
>>> +++i
6
>>> ++++i
6
>>> -i
-6
>>> --i
6
>>> ---i
-6
和想象的结果一致,Great!
输入输出(IO)
>>> varA = raw_input('please input:')
please input:Life is too short, you need Python!
>>> varA
'Life is too short, you need Python!'
>>> type(raw_input('input something:'))
input something:asdf
<type 'str'>
>>> type(raw_input('input something:'))
input something:123
<type 'str'>
A:你看到了,raw_input不论你输入什么都会返回str类型,这也是为什么叫做raw_input的原因。
>>> type(input('input sth:'))
input sth:123
<type 'int'>
>>> type(input('input sth:'))
input sth:asdf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'asdf' is not defined
>>> type(input('input sth:'))
input sth:varA
<type 'str'>
>>> input('sth:')
sth:varA
'Life is too short, you need Python!'
>>> input('try some input like your code:')
try some input like your code:[1, 'two', 3.0]
[1, 'two', 3.0]
>>> input('try again:')
try again:'Oh!!!'
'Oh!!!'
Python 数据类型和控制结构的更多相关文章
- Python 30分钟入门——数据类型 and 控制结构
Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候.不得不说的是Python的官方文档相当齐全,如果你是在Windows上学习Pytho ...
- Python 30分钟入门——数据类型 & 控制结构
Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候.不得不说的是Python的官方文档相当齐全,假设你是在Windows上学习Pytho ...
- Python学习笔记(四)Python程序的控制结构
在学习了 Python 的基本数据类型后,我们就要开始接触Python程序的控制结构,了解 Python 是如何使用控制结构来更改程序的执行顺序以满足多样的功能需求.如果有的小伙伴在之前学过C语言,j ...
- python 数据类型---布尔型& 字符串
python数据类型-----布尔型 真或假=>1或0 >>> 1==True True >>> 0==False True python 数据类型----- ...
- Python 数据类型及其用法
本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点型以及布尔类型.这些基本数据类型组 ...
- day01-day04总结- Python 数据类型及其用法
Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...
- Python数据类型及其方法详解
Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...
- Python学习笔记(五)--Python数据类型-数字及字符串
Python数据类型:123和'123'一样吗?>>> 123=='123'False>>> type(123)<type 'int'>>> ...
- python数据类型之元组、字典、集合
python数据类型元组.字典.集合 元组 python的元组与列表类似,不同的是元组是不可变的数据类型.元组使用小括号,列表使用方括号.当元组里只有一个元素是必须要加逗号: >>> ...
随机推荐
- 极客时间-左耳听风-程序员攻略-UI/UX设计
程序员练级攻略:UI/UX设计 学习设计新手, 7 steps to become a UI/UX designer 学习设计的一些原则和套路,如配色.平衡.排版.一致性等. 用户体验的 4D 步骤- ...
- 【ARTS】01_30_左耳听风-201900603~201900609
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- nginx.conf文件配置明细详解
#etnx运行的用户和用户组 user nginx nginx; #工作进程数,建议设置为CPU的总核数 worker_processes ; #全局错误日志定义类型,日志等级从低到高依次为: #de ...
- Nuget下载 MySql.Data 连接MySql数据库
打开VS项目,右键项目选择 右上角搜索框中搜索 MySql.Data 然后可以选择安装,但是可能会出现这个提示 这是因为版本的问题,所以,自己去官网重新下载,我这里选择的是这个版本, 官网地址: ht ...
- 使用jquery操作iframe中的元素
使用jquery操作iframe中的元素<iframe src="/test/demo.htm" width="99%" height="300 ...
- python中文编码 - python基础入门(5)
python到目前为止,一共有两个版本,分别是2.x和3.x版本,根据官方正式通知2020年停止对python更新和维护,距离今天还有110天左右,所以正在学习python的小伙伴应该暗中庆幸一波. ...
- Java Foreach用法
java中的while.for.if.switch等用法和c语言差不多,所以我们关注下foreach就行了. 一.创建ForeachTest.java public class ForeachTest ...
- 微软的一道网红Java面试题
题目: 给定一个int类型数组:int[] array = new int[]{12, 2, 3, 3, 34, 56, 77, 432}, 让该数组的每个位置上的值去除以首位置的元素,得到的结果,作 ...
- 【转】spring基础:@ResponseBody,PrintWriter用法
理解:很多情况我们需要在controller接收请求然后返回一些message. 1.在springmvc中当返回值是String时,如果不加@ResponseBody的话,返回的字符串就会找这个St ...
- (一)HTTP协议的一些知识点(来自那些年的笔记)
目录 http协议1.0.1.1两个版本的区别 访问几次服务器? Http请求行和请求方式详解 可以在超链接上传一些数据 HTTP请求头各个头字段的详解 HTTP响应和响应行状态详解 断点下载 HTT ...