python编程基础
遇到问题上谷歌
StackOverlow
提出你想做什么,而不是你做了什么
指出错误的地方,简要描述
将错误和代码复制
表达式
1.表达式
表达式包含 值(2)和 操作符(+)
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 2=2
SyntaxError: cannot assign to literal
>>> 2+2
4
>>> 2
2
>>>
PS:常见python错误 http://nostar.com/automatestuff/
1.2数学操作符(优先级由高到低)
** 指数
% 取模/取余数 22%8=2…6 取6
// 整除/商数取整 22//8=2 取2
/ 除法 22/8=2.75
*乘法 - 减法 + 加法
>>> 2=2
SyntaxError: cannot assign to literal
>>> 2+2
4
>>> 2
2
>>> 2**3
8
>>> 28
28
>>> 22%8
6
>>> 22/8
2.75
>>> 22//8
2
>>> 22+8
30
>>> 22-8
14
>>>
常见数据类型:整形,浮点型,字符串
数据为整数类型的叫整形
数据带小数部分的叫浮点型
用’'包含的叫字符串
SyntaxError: invalid character in identifier
>>> 12+12
24
>>> 12.3+14.2
26.5
>>> 'a'+'b'
'ab'
>>>
PS:在中文输入法的模式下,无法正确输入(.)。
变量与复制语句
变量相当于一个黑盒:用来保存值
>>> s=10
>>> s
10
>>> a=2
>>> a
2
>>> s+a
12
>>> s=a+2
>>> s
4
>>> s=s+2
>>> s
6
>>>
PS:第一次存入值 相当于初始化,此后可在表达式中使用它 ,赋新值时会被覆盖。
变量名
1.3变量名的命名规则
1.只能是一个词
2.只能包含字母、数字和下划线
3.不能以数字开头
合法的变量名:
balance、curBalance、cur_dad、_dad、SPASDA、ac4
不合法的变量名:
ce-sad、asd asd、2sad、32、das$sad、‘dsa’
第一个程序
注意文件和交互式环境的区别
交互式环境窗口总是有>>>提示符
文本编辑器窗口没有>>>提示符
# This program says hello and asks for my name
print('hello world!')
print('What is you name?') # ask for their name
myName=input()
print('It is good to meet you,'+myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?') #ask for their age
myAge=input()
print('You will be'+str(int(myAge)+1)+'in a year.')
第一个错误
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mYkwAmIc-1595471890952)(file:///C:\Users\土豆666\Documents\Tencent Files\1036372306\Image\C2C@CU]%7OQ1XB%~DO1GAL(9L3.png)]
警告:unexpected indent
错误一unexpected indent 解决方法
https://blog.csdn.net/weixin_42717711/article/details/82800192
源文件中的
myAge=input()
没有首行缩进,多打了一个空格