python简明教程_01

python编程语言简明教程,翻译自以下仓库:
Python-Lectures
原仓库是一系列的jupyter notebook,可以直接在jupyter环境下运行程序示例。我添加了中文注释,并改为了兼容python3

The Zen Of Python

import this
# 一首小诗。
# 大概吧。

The Zen of Python, by Tim Peters

Beautiful is better than ugly. Explicit is better than implicit.
Simple is better than complex. Complex is better than complicated.
Flat is better than nested. Sparse is better than dense. Readability
counts. Special cases aren’t special enough to break the rules.
Although practicality beats purity. Errors should never pass silently.
Unless explicitly silenced. In the face of ambiguity, refuse the
temptation to guess. There should be one-- and preferably only one
–obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often
better than right now. If the implementation is hard to explain,
it’s a bad idea. If the implementation is easy to explain, it may be a
good idea. Namespaces are one honking great idea – let’s do more of
those!

Variables

变量

可以被赋与各种不同类型的值:

x = 2
y = 5
xy = 'Hey'
print(x+y,xy)

7 Hey

同个值可以被赋予多个变量:

x = y = 1
print(x,y)

1 1

Operators

算术运算符

SymbolTask Performed
+Addition
-Subtraction
/division
%mod
*multiplication
//floor division
**to the power of
1+2

3

2-1

1

1/2

0.5

15%10

5

Floor division
“地板除”这种运算符就是将除完的结果向下取整:

2.8//2.0

1.0

比较运算符

SymbolTask Performed
==True, if it is equal
!=True, if not equal to
<less than
>greater than
<=less than or equal to
>=greater than or equal to
z = 1 # 一个等号是赋值
z == 1 

True

z > 1

False

位运算符

SymbolTask Performed
&Logical And
lLogical OR
^XOR
~Negate
>>Right shift
<<Left shift
a = 2 #10
b = 3 #11
print(a & b) 
print(bin(a&b)) # bin() 返回一个整数 int 或者长整数 long int 的二进制表示。

2
0b10

5 >> 1

2

0000 0101 -> 5

Shifting the digits by 1 to the right and zero padding

0000 0010 -> 2

对右移操作的解释,详见网上其他资源

5 << 1

10

0000 0101 -> 5

Shifting the digits by 1 to the left and zero padding

0000 1010 -> 10

对左移操作的解释,详见网上其他资源

内置函数

不同计数转换

int( ) 函数可以将浮点数、字符串等转化为整数型:

print(int(7.7))
print(int('7'))

7
7

float( ) 是转化为浮点数. chr( ) ASCII码转为对应字母, ord( ) 从字母转为对应的ASCII码.

chr(98)

‘b’

ord('b')

98

简化算术运算函数

round( ) 取整,或者保留几位小数.

print(round(5.6231)) # 取整
print(round(4.55892, 2)) # 第二个参数2表示保留两位小数

6
4.56

complex( ) 用于定义复数 abs( ) 输出绝对值.

c =complex('5+2j') # 定义了一个复数c
print(abs(c)) # 求该复数c的绝对值

5.385164807134504

divmod(x,y) 除余;注意返回的是一个tuple格式结果

divmod(9,2) #即9除以2,结果等于4余1

(4, 1)

isinstance( ) 判断类型是否一致,如果一致返回true

print(isinstance(1, int))
print(isinstance(1.0,int))
print(isinstance(1.0,(int,float))) # 可以进行多个比较

True
False
True

pow(x,y,z) 计算乘方 x y x^y xy 用z参数对前两个的乘方求余 : ( x y x^y xy % z).

print(pow(3,3)) # 求3的3次方
print(pow(3,3,5)) # 求3的3次方,并用结果27除以5求余数

27
2

range( ) 返回一个一定范围的数字列表

print(range(3)) # 只输入一个参数,默认生成从0开始的自然数列
print(range(2,9)) # 输入两个参数,默认为生成以这两个数为头尾的数字列表,间隔为1,且包含第一个参数,不包含第二个参数
print(range(2,27,8)) # 输入三个参数,默认为生成以前两个数为头尾的,以第三个数为固定间隔的数字列表

range(0, 3)
range(2, 9)
range(2, 27, 8)

读取输入

input( ), 读取使用者的键盘输入并转化为字符串

abc1 =  input("what?  \t")
type(abc1)

str

type( ) 返回括号内对象的类型

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值