额外注意:block在python这里很严格重要
不规范缩进,block会报错!
一 print函数的版本差异
(1)py2里可以不带括号,如 print "hello world"
PY2里写print ("hello" ,"world") 会输出 ("hello" ,"world") #被认识是元组,但1个字符串不会
(2)py3里必须是函数形式,
PY3里写必须写成print ("hello" ,"world") 会输出 "hello" ,"world"
print"let me have test on you,ok or not?"
user_option=raw_input()
if user_option=="ok":
print"enter your name"
user_name=raw_input()
user_password=raw_input("enter your password:\n")
user_age = raw_input("enter your age\n")
user_confirm=raw_input("are you ok?enter ok or not:\n")
if user_confirm == "ok":
print ("hello",user_name,"your password is:",user_password)
print ("you were born in ",2018-int(user_age))
else:
print ("quit")
else:
print "compare some cals"
print "hello" "world"
print ("hello" "world")
print "hello" ,"world"
print ("hello" ,"world")
print "7/4 is",7/4
print "7.0/4.0 is",7.0/4.0
二 print 多个参数
print 可以支持打印多个字符串等,中间以,分隔
print在一行结尾的,不是分隔符,而是表示,这个print执行完后,不处理\n,也就是不进行分行
三 print 与格式控制符号
有中文记得开头输入这个,否则报错 # -*- coding:utf-8 -*-
print("your name and age is %s %s") %(your_name,your_age),
注意有多个格式化符号%时,和后面不加符号分隔,只空格,然后,%要括号,并且注意括号前后的空格!!
# -*- coding:utf-8 -*-
# "你好"
# 你好
# nihao
print ("hello","rhino1")
my_name="rhino2"
print ("hello",my_name)
my_name="rhino3"
my_type="big"
num=1
print ("hello %s") %my_name
print ("there is 1 %s") %my_name
print ("there is %d rhino3") %num
print ("there is 1 %s %s") %(my_type,my_name)
print ("there is %d %s") %(1,my_name)
print ("there is %d %s") %(1,"rhino3")
print ("there is %d %s") %(num,my_name)
print "there is %d %s" % (num,my_name)
#"reference应用在不区分是decimal还是string的地方" %r
#your_name=raw_input("请输入你的名字\n") #加了UTF8之后不会报错了,但是仍然显示不正常?
#your_age=raw_input("请输入你的年纪\n") #加了UTF8之后不会报错了,但是仍然显示不正常?
your_name=raw_input("enter your name\n")
your_age=raw_input("enter your age\n")
print("your name and age is ",your_name,your_age)
#print("your name and age is %s %d") %(your_name,your_age) #注意,raw_input()导入的都是字符串类型的,要转
#print("your name and age is %d %d") %(your_name,int(your_age)) #数据类型不用对会报错
print("your name and age is %s %d") %(your_name,int(your_age)) #用户年龄输入数字不会出错,但乱填就会还是报错。
#可以要求输入的必须是某格式,或者强转的,我记得
print("your name and age is %s %s") %(your_name,your_age)
print("your name and age is %r %r") %(your_name,your_age) # 这个可以保证用户不按照实际输入也不报错
print("ok")
# 我很容易写错的,%d %s %r 而不是 d% s%等等 ,要记住%像\一样,都是要写在前面!!!
四 其他print,如pprint
注意,需要使用前,先import pprint
message='It was a bright cold day in April,and the clocks were striking thirtenn.'
count={}
for character in message:
count.setdefault(character,0)
count[character]=count[character]+1
print(count)
import pprint
message='It was a bright cold day in April,and the clocks were striking thirtenn.'
count={}
for character in message:
count.setdefault(character,0)
count[character]=count[character]+1
print(count)
pprint.pprint(count)
message='It was a bright cold day in April,and the clocks were striking thirtenn.'
count={}
for character in message:
count.setdefault(character,0)
count[character]=count[character]+1
print(count)
pprint.pformat(count)
输出结果是如下