Describe the basics of the Python programming language

本文详细介绍了Python编程的基础知识,包括命名规则、关键字、函数使用、参数传递、循环结构以及简单的绘图操作,适合初学者入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值