python知识体系第二节——python编程基础规则

1 程序的构成

(1)python程序由多个模块构成,每一个模块都是一个.py文件,每个模块都会实现各自的功能(由各语句组成),
多个模块即构成了一个复杂的python程序。
(2)python语句的执行顺序为依次执行
(3)语句为python程序的构造单元,用于创建对象、变量赋值、调用函数、控制语句。

2 对象的基本组成和内存示意图

python中所有的东西都可以称为对象,对象包含type(类型)、标识(identity)、value(值)三个要素。
对象的本质是一个内存块,拥有特定的值,支持特定类型的相关操作。

>>> a
3
>>> print(a)
3
>>> id(a)
1658103760
>>> type(a)
<class 'int'>
>>> 

实质上所谓赋值,例如a=3,就是将3的属性传递给a,
通常所理解的a等于3仅仅只包含的大小的传递,而计算机系统中包含3个信息,所以
a=3不能仅仅理解为“大小等于”,而是属性的传递。
正是由于上述原因,使得python不需要声明变量类型,即python为动态类型语言,并且python
对数据类型很敏感,每个数据类型只支持其对应的数据类型的操作。

2 标识符、帮助系统的使用、命名规则、删除

2.1 标识符

用于标识变量、函数、类、模块,命名规则为
(1)区分大小写;
(2)名字由字母,数字,下划线构成,并且开头不能为数字
(3)关键字不能用作标识符,例如打出if后,系统会自动将其识别为关键字,当后面接等号赋值时系统会报错
(4)尽量避免使用下划线开头与结尾,例如_int_,其代表的是构造函数。
例如:
首字母为数字报错

>>> 3a=3
SyntaxError: invalid syntax

使用关键词报错

>>> if =3
SyntaxError: invalid syntax

2.2 查关键词方法

>>> help()

Welcome to Python 3.8'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.8/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               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not                 

help> quit

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.

也可以直接打开编译器按F1

2.3 删除变量可以直接使用del

例如:

>>> a=3
>>> a
3
>>> del a
>>> a
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    a
NameError: name 'a' is not defined

3 链式赋值、系列解包赋值、常量

3.1 链式赋值

链式赋值用于将同一个对象赋值给多个变量(有时候会变量的值相同可以直接赋值)
例如:

>>> a=3
>>> b=3
>>> a
3
>>> b
3
>>> a=b=3
>>> a
3
>>> b
3

3.2 系列解包赋值

系列数据赋值给对应相同个数的变量(个数必须保持一致)
例如:a,b,c=4,5,6 相当于: a=4,b=5,c=6

>>> a,b,c=4,5,6
>>> a
4
>>> b
5
>>> c
6

并且可以运用系列解包赋值实现变量的互换,例如:

>>> a,b=1,2
>>> a,b=b,a
>>> a
2
>>> b
1

3.3 常量

python不支持常量,没有语法规则限制说不能够更改,我们智能逻辑上约定,常量尽量都大写,
SPEED,WIDTH。

4 最基本内置数据类型

4.1 python中最基本的数据类型

包括整形(例如123,3)、浮点型(例如3.14,3.14**2)、布尔型(例如False、True)、字符串型(例如"星期一",“abc”)

4.2 运算符

a+b#a与b相加
13
>>> a-b#a与b相减
7
>>> a*b#a与b相乘
30
>>> a/b#a与b相除
3.3333333333333335
>>> a//b#a与b相除取整
3
>>> a**2#a的平方
100
>>> a%b#a除以b取模(余)
1
>>> divmod(a,b)#同时取商和余数
(3, 1)

5 整数、不同进制、其他类型转换

5.1 整数

python中,除10进制以外,还有
0B或0b二进制:0,1
0O或0o八进制:0,1,2,3,4,5,6,7
0X或0x十六进制:0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f

>>> 0xf11
3857
>>> 15*16**2+1*16+1
3857

5.2 使用int()实现类型的转化

(1)转化浮点数:直接舍去小数部分

>>> int(4.12)
4

(2)转化字符串:只能转化整数类型

>>> int("3.14")
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    int("3.14")
ValueError: invalid literal for int() with base 10: '3.14'
>>> int("3")
3
>>> int("a")
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    int("a")
ValueError: invalid literal for int() with base 10: 'a'

(3)转化布尔型:true变为1,false变为0

>>> int(True)
1
>>> int(False)
0

5.3 整数可以有多大

python3中int可以存储无限大小的数,因此不会造成"整数溢出",所以python非常适合科学计算。

6 浮点数,自动转换,强制转换

6.1 浮点数

浮点数(float),可以使用科学计数法表示,例如(3.14,314e-2)

>>> 314e-2
3.14
>>> 314E-2
3.14

6.2 类型转换和四舍五入

类似于int(),float()也可以将其他数据类型转换成浮点型
整数和浮点数相加得到浮点数:

>>> 2+1.1
3.1

round(value)可以返回四舍五入的值

>>> round(3.14)
3
>>> round(4.59)
5

6.3 增强型赋值运算符

a+=1,即为a=a+1;
a-=1,即为a=a-1;
。。。。。
a**=2, a=a**2;

7 时间表示,unix时间点,豪秒

计算机中的时间是以1970年7月1日00:00:00为时间开始点,成为unix时间点
时间函数输出的都是从1970年至今过了多少秒,例如

>>> import time
>>> time.time()
1595224706.542923

也就是从1970年7月1日00:00:00到此刻一共过了1595224706.542923秒,本质上时间就是数字。

8 绘制多点坐标

绘制多点坐标,绘出折线,并计算起始点和终点距离

#引入库函数
import math
import turtle

#定义初始变量
x1,y1=100,100
x2,y2=-100,100
x3,y3=-100,-100
x4,y4=100,-100

#绘制折线
turtle.penup()
turtle.goto(x1,y1)
turtle.pendown()
turtle.goto(x2,y2)
turtle.goto(x3,y3)
turtle.goto(x4,y4)

#计算起点与终点的距离
distance=math.sqrt((x4-x1)**2+(y4-y1)**2)
turtle.write(distance)
turtle.done()

代码绘制出的图形

9 布尔值,比较运算,逻辑运算,逻辑判断

布尔值类型:True,False
比较运算、逻辑运算、逻辑判断:

>>> a,b=1,2
>>> a==b
False
>>> a!=b
True
>>> a>b
False
>>> a<b
True
>>> print(a<b)
True
>>> a,b=True,2
>>> a
True
>>> b
2
>>> a,b=False,True
>>> a and b
False
>>> a or b
True
>>> not a
True

“==”用于比较两个对象的数值,"is"用来比较两个对象所有属性
is ,is not用来比较两个对象属性

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值