一、组织代码:
python通过缩进来组织代码,缩进时,几个空格都是允许的,但是数目必须统一,我们通常用四个空格表示一个缩进。tab一次是四个空格。
使用行\,加这个符号后,可以换行再敲代码,不会直接运行。
二、对象 包含id \type\value
>>> a = 3
>>> id(a)
1935636523376
>>> id(3)
1935636523376
>>> type(3)
<class 'int'>
>>> b = "woaini"
>>> print(b)
woaini
>>> id(b)
1935676502768
>>> type(b)
<class 'str'>
>>
#对象就是一个内存块,一个个车位一样,有编号称为id(计算机内存地址),有类型,有可打印值
变量就是对象的引用,因为变量存储的就是对象的地址,变量通过地址引用了对象。变量位于栈zhan内存;对象位于堆内存。
python是动态类型的语言,变量不需要显示声明类型,根据变量引用的对象,python讲究语法的简洁,会自动解释数据类型。
但是每个对象都有数据类型,只支持该类型支持的操作。如果不支持的类型,就会报错。
比如:
>>> a = 3 + 10
>>> print(a)
13
>>> a = 3 + m
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
a = 3 + m
NameError: name 'm' is not defined
>>
三、标识符:给变量(后面还会学到函数、类、模块等)起名字的一些规则
1、区分大小写,大小写不同含义是不同的 ;
2、第一个字符必须是字母或者是下划线,后面的字符必须是字母、下划线、数字。其他的不可以。
3、不能使用关键词命名,比如 if or 等。
4、以双下划线开头和结尾的名称通常有特殊含义,避免这种写法。比如_init_是类的构造函数
5、关键词有哪些
False def if raise None del import return True elif in try and else is while as except lambda with......
>>> sat = 123
>>> if = 333
SyntaxError: invalid syntax
四、小tips:使用help 查询关键词
或者在idle页面按F1也可以查询一些信息,比如turtle的使用规则。
>>> help()
Welcome to Python 3.9's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.9/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help> keywords
Here is a list of the Python keywords. Enter any keyword to get more help.
False break for not
None class from or
True continue global pass
__peg_parser__ def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
help> quit
记得要退出之后才会又回到交互模式哦。
五、今日疑问
对象之后空格然后再输入,可以把空格省略掉吗?
a = 3
a=3是否可行呢?