#coding=utf-8
# 输入 等待用户输入内容
#将所有输入作为字符串看待,返回字符串类型
# content=raw_input("input any content\n");
# print content #结果是输入的内容
# print type(content) #查看变量类型 <type 'str'>
#输入
'''
input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )
input() 可接受合法的 python 表达式,举例:input( 1 + 3 ) 会返回 int 型的 4
input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数,所以,你甚至可以将表达式作为 input() 的参数,并且它会计算表达式的值并返回它。
除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。
'''
# content=input("可以输入合法的计算表达式\n")
# print content #输入1+2 结果为3
# #type() 内置函数 查看变量类型
# print type(content) #<type 'int'> or <type 'float'>
'''
缩进
是Python中函数和控制语句的必要格式
(对于我Java程序员来说,没有分号结束符和{}代码块,真的是痛苦)
'''
'''
流程控制
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
Python 编程中 if 语句用于控制程序的执行,基本形式为:
if 判断条件:
执行语句。。。
else:
执行语句。。。
'''
#if else
flag = False
name = 'zzq'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print 'welcome python' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
'''
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值时,可以使用以下形式:
if 判断条件1:
执行语句1
elif 判断条件2:
执行语句2
elif 判断条件3:
执行语句3
else:
执行语句4
'''
num = 5
if num == 3: # 判断num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零时输出
print 'error'
else:
print 'roadman' # 条件均不成立时输出
'''
由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
'''
num = 9
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print 'hello'
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine'
#格式化输出 占位符
#占位符 %d 整型 %f 浮点型 %s 字符串 %c char
a=97
print "a的值为%d" %a #123 int % 格式化输出变量值
print "a的值为%c" %a #a char
print "a的值为%f" %a #123.0 float
print "a的值为%s" %a #123 str
#连续占位符输出
name="强子"
age=22
height=180.123
#%.nf 保留n位小数点
print "姓名%s,年龄%d,身高%.2f"%(name, age, height)
#姓名强子,年龄22,身高180.12
#格式化输出
print "姓名{},年龄{},身高{}".format(name, age, height)
#姓名强子,年龄22,身高180.123
print "姓名{name:s},年龄{age:d},身高{height:.2f}".format(name=name, age=age, height=height)
#循环
'''
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for循环的语法格式如下:
for iterating_var in sequence:
statements(s)
'''
for letter in 'Python': # 第一个实例
print '当前字母 :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit
print "Good bye!"
'''
输出结果
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
'''
#通过序列索引迭代 range(start,end,step)
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '当前水果 :', fruits[index]
print "Good bye!"
'''
输出结果
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
'''
#range(start,end,step) 返回类型为 列表
#xrange(start,end,step) 执行效率高 作用同上
#但是返回值不是list 而是生成器 每次调用返回其中的一个值
#特点:包左不包右
#步长为2 输出1-100 奇数
# for x in range(1,101,2):
# print x,
#range(2)<==>range(0,2)<==>range(2,)
#break continue 同PHP Java C
#随机数函数 random
'''
使用其他类 需要先导入类 关键字 import
随机整数:
import random
random.randint(0,99)
21
随机选取0到100间的偶数:
import random
random.randrange(0, 101, 2)
42
随机浮点数:
import random
random.random()
0.85415370477785668
random.uniform(1, 10)
5.4221167969800881
随机字符:
import random
random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
import random
import string
string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
import random
random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
import random
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)
items
[3, 2, 5, 6, 4, 1]
'''
import random
#print help(random) 显示random的信息
print random.randint(1,20) #随机一个>=1 <=20间的整型数字
print random.random()#用于生成一个0到1的随机符点数: 0 <= n < 1.0
# 输入 等待用户输入内容
#将所有输入作为字符串看待,返回字符串类型
# content=raw_input("input any content\n");
# print content #结果是输入的内容
# print type(content) #查看变量类型 <type 'str'>
#输入
'''
input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )
input() 可接受合法的 python 表达式,举例:input( 1 + 3 ) 会返回 int 型的 4
input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数,所以,你甚至可以将表达式作为 input() 的参数,并且它会计算表达式的值并返回它。
除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。
'''
# content=input("可以输入合法的计算表达式\n")
# print content #输入1+2 结果为3
# #type() 内置函数 查看变量类型
# print type(content) #<type 'int'> or <type 'float'>
'''
缩进
是Python中函数和控制语句的必要格式
(对于我Java程序员来说,没有分号结束符和{}代码块,真的是痛苦)
'''
'''
流程控制
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
Python 编程中 if 语句用于控制程序的执行,基本形式为:
if 判断条件:
执行语句。。。
else:
执行语句。。。
'''
#if else
flag = False
name = 'zzq'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print 'welcome python' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
'''
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值时,可以使用以下形式:
if 判断条件1:
执行语句1
elif 判断条件2:
执行语句2
elif 判断条件3:
执行语句3
else:
执行语句4
'''
num = 5
if num == 3: # 判断num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零时输出
print 'error'
else:
print 'roadman' # 条件均不成立时输出
'''
由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
'''
num = 9
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print 'hello'
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine'
#格式化输出 占位符
#占位符 %d 整型 %f 浮点型 %s 字符串 %c char
a=97
print "a的值为%d" %a #123 int % 格式化输出变量值
print "a的值为%c" %a #a char
print "a的值为%f" %a #123.0 float
print "a的值为%s" %a #123 str
#连续占位符输出
name="强子"
age=22
height=180.123
#%.nf 保留n位小数点
print "姓名%s,年龄%d,身高%.2f"%(name, age, height)
#姓名强子,年龄22,身高180.12
#格式化输出
print "姓名{},年龄{},身高{}".format(name, age, height)
#姓名强子,年龄22,身高180.123
print "姓名{name:s},年龄{age:d},身高{height:.2f}".format(name=name, age=age, height=height)
#循环
'''
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for循环的语法格式如下:
for iterating_var in sequence:
statements(s)
'''
for letter in 'Python': # 第一个实例
print '当前字母 :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit
print "Good bye!"
'''
输出结果
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
'''
#通过序列索引迭代 range(start,end,step)
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '当前水果 :', fruits[index]
print "Good bye!"
'''
输出结果
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
'''
#range(start,end,step) 返回类型为 列表
#xrange(start,end,step) 执行效率高 作用同上
#但是返回值不是list 而是生成器 每次调用返回其中的一个值
#特点:包左不包右
#步长为2 输出1-100 奇数
# for x in range(1,101,2):
# print x,
#range(2)<==>range(0,2)<==>range(2,)
#break continue 同PHP Java C
#随机数函数 random
'''
使用其他类 需要先导入类 关键字 import
随机整数:
import random
random.randint(0,99)
21
随机选取0到100间的偶数:
import random
random.randrange(0, 101, 2)
42
随机浮点数:
import random
random.random()
0.85415370477785668
random.uniform(1, 10)
5.4221167969800881
随机字符:
import random
random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
import random
import string
string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
import random
random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
import random
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)
items
[3, 2, 5, 6, 4, 1]
'''
import random
#print help(random) 显示random的信息
print random.randint(1,20) #随机一个>=1 <=20间的整型数字
print random.random()#用于生成一个0到1的随机符点数: 0 <= n < 1.0