Python代码整理

python

python语言基础

2.1标识符与关键字

2.1.标识符

2.1.1关键字

表2-1

2.2变量

2.2.1对象和类型

>>> type(2)#整形
<class 'int'>#clss表示类别
>>> type(43.56)#浮点型
<class 'float'>
>>> type({1,2,3})#集合
<class 'set'>
>>> type('2')#字符串
<class 'str'>
>>> 

2.2.2变量的创建

>>> a=2#整型赋值
>>> type(a)
<class 'int'>
>>> b='python'#字符串赋值
>>> c=a*3#整型乘法赋值
>>> a="hello"+b#字符串连接赋值
>>> type(a)
<class 'str'>
>>> a=100
>>> b=100
>>> id(a)#内置函数id()可以获取变量的内存地址
2071916592
>>> id(b)
2071916592
>>> a=300
>>> b=300
>>> id(a)
60749600
>>> id(b)
77440032
>>> a=-6
>>> id(a)
77440096
>>> b=-6
>>> id(b)
77440352
>>> a=b=-6
>>> id(a)==id(b)
True

上课补充

>>> a=b=-6
>>> a is b #判断是否在同一内存上
True

例2.1

姓名='李国'
年龄=16
身高=174.5
是否党员=True
个人介绍信息="我叫李国,今年16岁,身高175.5"
print(姓名)
print(年龄)
print(身高)/
print(个人介绍信息)
李国
16
174.5
我叫李国,今年16岁,身高175.5

2.2.3变量的删除

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

2.3数据类型

2.4运算符

2.4.1算数运算符

算数运算符示例

>>> 3/5
0.6
>>> 3//5
0
>>> 4//3
1
>>> 9/3
3.0
>>> 5//3
1
>>> 3.1%2
1.1
>>> 3*2
6
>>> 5%2
1
>>> 3**2
9
>>> (3+4j)*2
(6+8j)
>>> a=3
>>> type(a)
<class 'int'>
>>> a=a+2.4
>>> type(a)
<class 'float'>
>>> a=a+(10+2j)
>>> type(a)
<class 'complex'>
>>> b=True
>>> c=int(b)
>>> type(c)
<class 'int'>
>>> d=10+20j
>>> int(d)
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
TypeError: can't convert complex to int
>>> c=2.3
>>> int(c)
2
>>> x=1+int('2')
>>> x
3
>>> x=1+ord('a')
>>> x
98
>>> float('98.6')
98.6

例2-2

代码如下

李国出生年份 = 2002
当前年份 = 2020
李国年龄 = 当前年份-李国出生年份
print('李国现在的岁数是:',李国年龄)

运行结果

李国现在的岁数是: 18

2.4.2逻辑运算符

短路规则

>>> 0 and 'a'
0
>>> 5 and 'a'
'a'
>>> 'a' and 5 
5
>>> 'a' and ''
''
>>> '' or 10
10
>>> 10 or '' 
10
>>> 5 or 10
5
>>> 0 or 10
10
>>> not 5
False
>>> not 0
True
>>> not 1
False

例2-3

代码如下

x=7*3
print(x > 10 and x < 20)

运行结果如下

False

2.4.3关系运算

>>> 1<=2<=3 #等价于 1<=2 and 2<=3
True
>>> 2>=1>10 #等价于 2>1 and 1>10
False
>>> 4<5>3 #补充例子
True

例2-4 判断字母’h’是否在字符串’Hellow Python’中

代码如下:

print('h' in 'Hellow Python')

运行结果如下:

True

例2-5将a、b两个变量分别赋值为15,使用身份运算符比较他们。

代码如下:

a=15
b=15
print(a is b)
print(b is a)
print(id(a))
print(id(b))

运行结果如下:

True
True
1687677664
1687677664

2.4.4 位运算符

位运算符只能用于整型数据,不能用于浮点型数据

位运算符示例如下:

>>> 2&10
2
>>> 2|10
10
>>> 2^2
0
>>> ~2
-3
>>> 8>>3
1
>>> 8<<3
64
>>> {'a','b','c'} & {'c','d','e'} 
{'c'}
>>> {'a','b','c'} | {'c','d','e'} 
{'a', 'b', 'c', 'd', 'e'}
>>> {'a','b','c'} - {'c','d','e'} 
{'a', 'b'}
>>> {'a','b','c'} ^ {'c','d','e'} 
{'b', 'e', 'a', 'd'}

矩阵相乘运算符@用于矩阵的乘法运算,示例如下:

>>> import numpy as np #导入numpy库
>>> x=np.array([1,2,3]) #创建数组
>>> y=np.array([[4,5,6],[7,8,9],[10,11,12]])
>>> z=y@x #矩阵相乘
>>> z
array([32, 50, 68])

2.4.5 赋值运算

>>> x=10
>>> x>>=2
>>> print(x)
2
>>> x=y=z=10 多重赋值
>>> print(x,y,z)
10 10 10
>>> y=20
>>> x,y,z=x+y,x+z,y+z #多元复制
>>> print(x,y,z)
30 20 30

Python程序基本结构

1.物理行和逻辑行

>>> s='This is a string.\
This continues the string.'
>>> print(s)
This is a string.This continues the string.

2.语句分割

>>> a=10;s='python'

3.缩进

if (a>80) :
    if (a<=100) :
        print('恭喜你!')
        print('你非常优秀!')
    else :
        print('你还需要努力')
a=1   
    b=2
IndentationError: unexpected indent

4.注释

>>> '''本程序从一个三位数中提取百位、十位和个位上的数字,使用内置函数divmod()函数来返回商和余数'''
>>> x=153
>>> a,b = divmod(x,100)
>>> b,c = divmod(b,10)
>>> s='''This is a statements,but it is not comment'''

2.8 基本输入输出

2.8.1 input()函数

  • 返回结果都是字符串

    >>> x = input('Please input:')
    Please input:3
    >>> print(type(x))
    <class 'str'>
    >>> x=input('Please input:')
    Please input:1
    >>> y=int(x)*10
    >>> y
    10
    >>> x=eval(input('Please input:'))
    Please input:123+10
    >>> x
    133
    >>> x,y,z=eval(input('请输入三个数字:'))
    请输入三个数字:45,56,67
    >>> print('%6d%6d%6d'%(x,y,z))
        45    56    67
    >>> x=input('请输入三个数字:')
    请输入三个数字:123 456 789
    >>> a,b,c=map(int,x.split())
    >>> print('%6d%6d%6d'%(a,b,c))
       123   456   789
    

    2.8.2 print()函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值