第一篇博客...暑假都快过了才开始学python。坚持每天记录一下,没准以后写报告还能用上。
莫烦python学习 https://morvanzhou.github.io/tutorials/python-basic/basic/02-1-print/
版本:python-3.7.0 windows
安装
官网下载
downloads-->windows-->python-3.7.0
配置环境变量
安装的时候勾一下 Configuring environment variables (忘截图了,记不太清楚)
编译器IDLE
按照这个教程找找就行 https://jingyan.baidu.com/article/148a1921ccc2f74d71c3b106.html
检验是否安装成功
打印数字1
>>> print 1
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(1)?
#python2.7及以下版本输出时不加(),我的是3.7需要加(),不加报错
>>> print(1)
1
#成功
print功能
输出字符串
>>> print('i have a pen')
#单引号‘string字符串’
i have a pen
>>> print("i hava an apple")
#双引号“string字符串”
i hava an apple
>>> print('apple'+'pen')
#合体
applepen
转义字符
>>> print('you're a pig')
# ’you‘ 你懂我的意思
SyntaxError: invalid syntax
>>> print('you\'re a pig')
you're a pig
类型转换
转成string有两种方法
>>> print(1+1)
#自动计算
2
>>> print("1"+"1")
#输出字符串
11
方法1:加引号
>>> print(4+'apple')
#4是int型,编译错误
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
print(4+'apple')
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> print('4'+'apple')
#4转成string型,正确编译
4apple
方法2:用str()
>>> print(str(4)+'apple')
#print前面多了个空格,报错
SyntaxError: unexpected indent
>>> print(str(4)+'apple')
#用内置函数str()
4apple
输出2.33+2
>>> print(float(2.33)+2)
#用内置函数float()
4.33