python基础整理一

这篇博客介绍了Python的基础特性,包括Python之禅、Hello World、用作计算器的功能。详细讲解了变量、代码规范、变量类型,特别是列表、元组和字典等数据结构。还涉及条件判断、循环结构、函数及装饰器的使用,以及类和继承的概念。
摘要由CSDN通过智能技术生成

python的特性

1.python语法简单,容易理解,容易学习
2.跨平台,可以在windows,linux,mac os
3.可以做网站、爬虫、大数据处理
4.拥有强大的第三方库numpy,panda...

python 之禅

输入import this
>>>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!
python之禅 翻译 备注
The Zen of Python, by Tim Peters 作者:Tim Peters
Beautiful is better than ugly. 优美胜于丑陋 python以编写优美的代码为目标
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. 不要包容所有的错误除非你确定要这么做 精确的捕获异常,不要编写except: pass风格的代码
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. 虽然这并不容易,因为你不是dutch dutch这里指的是python之父
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! 命名空间是一个很好的解决方案,我们需要多加利用

hello world

print('hello world')
hello world
print("hello world")
hello world
print("\"hello world\"")
"hello world"
print('\'hello world\'')
'hello world'
#合理的使用单双引号可以避免使用转义字符
print("'hello world'")
'hello world'
print('"hello world"')
"hello world"

python可以直接当作计算器来使用

2+3
5
2-1
1
2*3
6
2*3
6
5%2
1
#2的3次方
2**3
8
#8开3次方
8**(1/3)
2.0

import math
#math.pi是180°,math.pi/2是90°
math.sin(math.pi/2)
math.sin(math.pi/3)
#向下取整,输出9
math.floor(9.599)
#向上取整,输出10
math.ceil(9.599)
#非math,四舍五入
round(9.4)
9
round(9.5)
10

#format函数,增强的字符串格式化函数
a=5
b=6
"a的值为:{},b的值为:{}".format(a,b)
'a的值为:5,b的值为:6'

变量:代表某个值的名称

#语法糖
a=10
b=20
a,b=b,a
#变量的命名规范
1.标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线
2.标识符的其他部分可以由字母,下划线和数字组成
3.标识符对大小写敏感
总结:首字母不能是数字,标识符之间不能有空格,‘-

代码规范

1.不要使用单字符
2.变量名能清晰表达变量意思
3.合理使用字母中间下划线

变量类型

变量类型 标识符
字符串 str
数字 int, float,complex(复数)
列表 list
元组 tuple
字典 dict
布尔 boolean (True, False)
line="hello"
id(line)
1733427178992
line="hello world"
id(line)
1733990190576
#取前20个字符,每隔一个取一个,一共是10个字符
line[0:20:2]
#取后10个字符
line[-10:]
#反转字符
line[::-1]
String是不可变字符
line[0]='a'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值