Python简明入门教程简明入门教程
主要介绍了Python简明入门教程,较为详细的分析了Python的基本概念及语法基础,有助于Python初学者更好的掌
握Python的基本语法与使用技巧,需要的朋友可以参考下
本文实例讲述了Python简明入门教程。分享给大家供大家参考。具体如下:
一、基本概念一、基本概念
1、数
在Python中有4种类型的数——整数、长整数、浮点数和复数。
(1)2是一个整数的例子。
(2)长整数不过是大一些的整数。
(2)3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(4)(-5+4j)和(2.3-4.6j)是复数的例子。
2、字符串
(1)使用单引号(')
(2)使用双引号(")
(3)使用三引号('''或""")
利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。例如:
'''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."
'''
(4)转义符
(5)自然字符串
自然字符串通过给字符串加上前缀r或R来指定。例如r"Newlines are indicated by "。
3、逻辑行与物理行
一个物理行中使用多于一个逻辑行,需要使用分号(;)来特别地标明这种用法。一个物理行只有一个逻辑行可不用分号
二、控制流二、控制流
1、if
块中不用大括号,条件后用分号,对应elif和else
if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
elif guess < number:
print 'No, it is a little higher than that' # Another block
else:
print 'No, it is a little lower than that'
2、while
用分号,可搭配else
while running:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that'
else:
print 'No, it is a little lower than that'
else:
print 'The while loop is over.'
# Do anything else you want to do here
3、for
用分号,搭配else
for i in range(1, 5):
print i
else:
print 'The for loop is over'