Hello World
print('Hello,World') //单引号也可以换成双引号,注意print前面不要有空格啥的哦~
这么简单。是不是不能满足你Hello World的胃口。那么继续看下面吧~
输出print
格式:print('xxx'...)或者print("xxxx"...)
输出字符
print('A') //学过C或者java的朋友这里后面不用加;哦,下面也一样。
print('A'+'B')与print('A''B')一样
print('A','B') //AB间有一个空格
print('\tb')
print(r'\tb') //大家应该都知道转义字符,\t一般是制表符不会输出\t的,但是在前面加上r之后
//就会原样输出
Ps:转义字符表
输出数字及计算
print(5)
print(5*5)
print(5/2) //为什么输出2.5以后解释(对于学C以后不能理解的读者)
print(5-2)
print(5+5)
格式化输出
与C、java语言相同,用%运算符来格式化表示,数字的位数长度和C、JAVA一样。但是不同的是不是用%将前后的对应连接起来。
常见的占位符:
%d 整数
%f 浮点数
%s 字符串
%x 十六进制整数
要输出% 采用%%形式。
'Hi,%s,you have $%d in assets at first and you have spent $%.2f,then you only have $%s in total ' %('Lisa',1000,650.2,1000-650.2)
当不确定为什么类型的时候都可以用%s来代替,python会自动将其转换为字符串类型后输出。%g与C、Java中含义相同。由于浮点数的计算是不精确的所以会出现上面图片打印出和想象中不同的问题。
输入
格式:input()
//输入之后会等待你输入,默认输入都是字符串类型
不带提示的输入
input()
a=input() //用变量a来指向input输入的内容
带提示的输入
b=input("please input :") //注意字符串要么用单引号要么用双引号
格式:eval(input())
//使用eval(input())时就不是默认字符串了,输入什么就是什么
可能碰到问题
IndentationError:expected an indented block
解决:将print前面的空格删去即可。Python的缩进格式很严格。
SyntaxError:Missing parentheses in call to 'print'
解决:现在Python的版本不支持直接输出,需要加()才可。
SyntaxError:invalid syntax
解决:hello world未用引号引起来。