2018年1月16日学习笔记
(autosaved)
Logout
Python 3
Trusted
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
Run
In [1]:
print("hello world!")
hello world!
In [2]:
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!
###最简单的开始
In [4]:
5+10
Out[4]:
15
In [5]:
10**(1/3)
Out[5]:
2.154434690031884
In [6]:
import math
In [7]:
math.ceil(9.235)
Out[7]:
10
引入变量
In [15]:
apple_price = 5
apple_weight = 2
apple_cost = apple_price * apple_weight
grape_price = 15
grape_weight = 1.5
grape_cost = grape_price*grape_weight
print("apple cost is %d"%apple_cost)
print("grape cost is %.2f"%grape_cost)
print("all cost is %.2f"%(apple_cost + grape_cost))
#另一种字符串格式化方法
print("apple cost is {},grape cost is {},total cost is {}".format(apple_cost,grape_cost,apple_cost+grape_cost))
#再来一种输出方式
print(f"aplle cost is {apple_cost},grape cost is {grape_cost},total cost is {apple_cost + grape_cost}")
apple cost is 10
grape cost is 22.50
all cost is 32.50
apple cost is 10,grape cost is 22.5,total cost is 32.5
aplle cost is 10,grape cost is 22.5,total cost is 32.5
语法糖
In [16]:
a = 37
b = 21
print(f"a = {a},b = {b}")
a,b = b,a
print(f"a = {a},b = {b}")
a = 37,b = 21
a = 21,b = 37
In [17]:
line = "hello"
id(line)#内存地址
Out[17]:
2105890899368
In [18]:
line = "world"
id(line)#内存地址
Out[18]:
2105890898528
序号
姓名
年龄
1
奔波霸
128
2
齐天大圣
500
In [23]:
list1 = [1,2,3]
id(list1)
Out[23]:
2105892079816
In [39]:
list1.append(0)#列表末尾追加
print(list1)
list1.clear()#清除列表
print(list1)
list1.extend((1,4,6,9))#追加序列
print(list1)
list2 = list1.copy()#copy一个列表,浅拷贝
print(list1)
print(list2)
a = list1.count(6)#列表中某个元素的个数
print(a)
b = list1.index(9)#元素的索引
print(b)
list1.insert(3,888)#在某个位置插入一个元素
print(list1)
c = list1.pop()#从最末尾开始往外抛
print (c)#返回抛出的那个元素
print(list1)
list1.pop(0)#可以指定索引抛那个
print(list1)
list1.reverse()#反转
print(list1)
list1.sort()#排序
print(list1)
list1.remove(6)#删除某元素
print(list1)
list1 = [66,99,66,66,99]
list1.remove(66)#只删除最靠前的那个
print(list1)
[9966, 66, 99, 0]
[]
[1, 4, 6, 9]
[1, 4, 6, 9]
[1, 4, 6, 9]
1
3
[1, 4, 6, 888, 9]
9
[1, 4, 6, 888]
[4, 6, 888]
[888, 6, 4]
[4, 6, 888]
[4, 888]
[99, 66, 66, 99]
In [ ]:
本文档为Python初学者提供了快速入门指导,包括基本语法介绍、变量使用、数据类型操作及常见函数应用等。通过实例演示了数学运算、字符串格式化、列表操作等关键技能。
7万+

被折叠的 条评论
为什么被折叠?



