Python基本语法_print与input
·
下学期马上就要开设python课程了,先超前学习一波。
晚上学习课程讲了python的input和print,在此作记录。
1.print()输出
格式如下:
print(’…’)
print()输出默认会换行
如果不需要换行,可以在括号内加上end = ‘’
如下:
①默认换行
print('hello python')
print('hello python')
# 输出结果为:hello python
# hello python
②取消换行
print('hello python',end='')
print('hello python',end='')
# 输出结果为:hello pythonhello python
2.input()输入
input代码功能需要用回车键来结尾,python的输入语句可以和定义变量语句一起实现,如下:
a = int(input('请输入正方形边长:'))
1.定义了变量a
2.实现了输入功能
举例:
求正方形的面积,先来看下面的代码:
a = input('请输入正方形边长:')
s = a * a
print('正方形的边长为:',s)
# TypeError: can't multiply sequence by non-int of type 'str'
此代码块会出现如注释所说的bug,原因是我们以为自己输入的值是整数,但实际上是字符串类型str,所以需要对a进行强制转型,如下:
a = int(input('请输入正方形边长:'))
s = a * a
print('正方形的边长为:',s)
# 输出结果:
# 请输入正方形边长:5
# 正方形的边长为: 25
ps:
变量占用内存空间
定义变量不能以数字开头
python的保留字即关键字
python用缩进来控制代码块,约定四个空格为一个缩进
python大小写敏感
(其余的笔记,不作分类啦)
更多推荐



所有评论(0)