1、python 命名规则
a、不能以数字开头
b、不能包含非法字符。例如:@
c、不能是关键字
2、python 关键字
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
3、functions
functions type 能够自动识别类型,lg:
>>> type(42)
<class 'int'>
>>> type('你好,中国!')
<class 'str'>
>>> print(print_test)
<function print_test at 0x00000000021D0158>
>>> type(print_test)
<class 'function'>
int function,lg:
>>> int('32')
32
>>> int('Hello')
ValueError: invalid literal for int(): Hello
>>> int(3.99999)
3 >>> int(-2.3)
-2
float function,lg:
>>> float(32)
32.0
>>> float('3.14159')
3.14159
str function,lg:
>>> str(32)
'32'
>>> str(3.14159)
'3.14159'
create new function,lg:
在打印字符串时,单引号和双引号效果一样(Single quotes and double quotes do the same thing;)
>>> def print_test():
... print('aaa')
... print('bbb')
...
>>>
函数的结尾,需要多打一行空格。(To end the function, you have to enter an empty line.)
4、参数(Parameters and Arguments)
>>> def print_twice(test):
... print(test)
... print(test)
...
>>> print_twice('hello world!')
hello world!
hello world!
>>> print_twice(42)
42
42
由此看出,这里参数可以不用区分类型,既可传入字符串,也可传整型数
>>> print_twice('Spam '*4)
Spam Spam Spam Spam
Spam Spam Spam Spam
>>> print_twice(math.cos(math.pi))
-1.0
-1.0
参数会优先函数调用函数之前计算(The argument is evaluated before the function is called, so in the examples the expressions
'Spam '*4 and math.cos(math.pi) are only evaluated once.)
>>> math.sqrt(5)
2.2360679774997898
>>> result = print_twice('Bing')
Bing
Bing
>>> print(result)
None
上面是有返回值的函数和无返回值的函数,当无返回值输出None时,这里None不是字符串,而是一种类型(The value None is not the same as the string 'None'. It is a special value that has its own type)
>>> print(type(None))
<class 'NoneType'>
5、循环
>>> for i in range(4):
... print('Hello!')
...
Hello!
Hello!
Hello!
Hello!
>>>
6、简单画图(The turtle Module)
导入相关工具包,并打开画图框
>>> import turtle
>>> bob = turtle.Turtle()
箭头往前画100(Once you create a Turtle, you can call a method to move it around the window. A method is similar to a function, but it uses slightly different syntax. For example, to move the turtle forward:)
bob.fd(100)