Python笔记(一)开篇

前言: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!


一、简介

python的特性
- python语法简单 ,容易理解,容易学习
- 跨平台,可在多种系统上运行
- 可以做网站、爬虫、大数据处理、机器学习
- 拥有强大、丰富的第三方库:numpy、pandass

当计算器使用

import math

import math
print(math.pow(3,10));# 59049
print(3**10);# 59049
print(math.floor(2.2345));# 2
print(math.ceil(2.2345));# 3
print(math.radians(180));# 3.141592653589793 ---->度数转换
print(math.sin(math.pi/2));# 1.0
print(min(10,12,234,100,1));# 1
print(max(10,12,234,100,1));# 234
print(sum([10,12,234,100,1]));# 357
print(divmod(10,3))# (3, 1)  ----> 求商和余数 

增强格式化字符串函数format

a=5;
b=6;
c=7;
print("a:{};b:{};c:{}".format(a,b,c));# a:5;b:6;c:7

二、变量类型

标识符
- 第一个字符必须是字母表中字母或下划线 _
- 标识符的其他的部分由字母、数字和下划线组成
- 标识符对大小写敏感

命名规范
- 标识符的第一个字符必须是字母表中的字母(大写或者小写)或者下划线
- 标识符名称的其他部分可以由字母(大写或小写)下划线(_)或者数字(0-9)组成
- 标识符名称适度大小写敏感的

print(round(100/3));# 33
print(round(100/3),3);## 33.333

语法糖

#实现a和b交换
a=5;
b=12;
print(a,b);
a,b=b,a;
print(a,b);

代码规范建议
- 不要使用单个字符
- 变量名能清晰表达变量的意思
- 合理使用字母中间下划线

变量类型
- 字符串:str
- 数字:int 、floor、double …
- 列表:list
- 元祖:tuple
- 字典:dict

bool型,运算:与运算、或运算、非运算

print(True and False); #False
print(True and True);# True
print(True or False); #True
print(not False);# True

骚操作

print(True+10);# 11

字符串

#字符串为不可变类型,原先指向字符串的地址是不可改变的

line="he";
line_copy=line;
print(id(line));# 2607584542648
print(id(line_copy));# 2607584542648
line="she\'he";
print(id(line));# 2607584580696
line="it\'she\'he";
print(id(line));# 2607584558064
print(id(line_copy));# 2607584542648
print(line);# it'she'he
print(line);# it'she'he


line_1="hello ";
line_2="word";
line_3="!";
print(line_1+line_2+line_3);# hello word!
print(line_1*3);# hello hello hello
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值