He Wang PRO
Knowledge increases by sharing but not by saving.
第 2 部分 基于 Python 的数据分析基础
主讲老师:王赫
2023/11/29
ICTP-AP, UCAS
数据科学语言 Python 入门到熟悉
# Response
# Python
数据分析
创建复杂的 Web 应用程序
网站开发
游戏开发
动画电影效果
...
# Python
and more complex, range, bytes, ... (内置类型)
整型
浮点型
字符串
布尔型
列表
元组
集合
字典
数字类型
# Python
and more complex, range, bytes, ... (内置类型)
整型
浮点型
字符串
布尔型
列表
元组
集合
字典
数字类型
文本
序列类型
# Python
and more complex, range, bytes, ... (内置类型)
整型
浮点型
字符串
布尔型
列表
元组
集合
字典
数字类型
序列类型
文本
序列类型
# Python
and more complex, range, bytes, ... (内置类型)
整型
浮点型
字符串
布尔型
列表
元组
集合
字典
数字类型
映射类型
序列类型
文本
序列类型
# Python
and more complex, range, bytes, ... (内置类型)
整型
浮点型
字符串
布尔型
列表
元组
集合
字典
小结:Python 中所有基本数据类型的特点总结与归纳、变量的内存机制、熟练表达式的用法提高编程效率
数字类型
映射类型
序列类型
文本
序列类型
# Python
# if if condition1: statement1 elif condition2: statement2 else: statment3
# for for i in range(5): my_func(i) # while i = 0 while i < 5: my_func(i) i += 1
# try ... except ... try: my_func() except Exception as e: print(e) raise
for n in range(2, 10): for x in range(2, n): if n % x == 0: print(n, 'equals', x, '*', n//x) break else: # loop fell through without finding a factor print(n, 'is a prime number') ''' 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 '''
break vs continue (pass)
for num in range(2, 10): if num % 2 == 0: print("Found an even number", num) continue print("Found a number", num) ''' Found an even number 2 Found a number 3 Found an even number 4 Found a number 5 Found an even number 6 Found a number 7 Found an even number 8 Found a number 9 '''
# Python
# if if condition1: statement1 elif condition2: statement2 else: statment3
# for for i in range(5): my_func(i) # while i = 0 while i < 5: my_func(i) i += 1
# try ... except ... try: my_func() except Exception as e: print(e) raise
break vs continue (pass)
for ... (else) ...
# Source: https://stackoverflow.com/a/9980752/1392860 for i in mylist: if i == target: break process(i) else: raise ValueError("List argument missing terminal flag.") # 所以无需专门建立一个临时标记变量来标记是否已经找到了 target
for i in range(3): print(i) if i % 2 == 0: break else: print("end") ''' 0 '''
for i in range(3): print(i) else: print("end") ''' 0 1 2 end '''
# Python
# if if condition1: statement1 elif condition2: statement2 else: statment3
# for for i in range(5): my_func(i) # while i = 0 while i < 5: my_func(i) i += 1
# try ... except ... try: my_func() except Exception as e: print(e) raise
break vs continue (pass)
for ... (else) ...
while/for ... try ... except ...
while True: try: x = int(input("Please enter a number: ")) break # Don't forget stop the Loop! except ValueError: print("Oops! That was no valid number. Try again...")
# Python
# if if condition1: statement1 elif condition2: statement2 else: statment3
# for for i in range(5): my_func(i) # while i = 0 while i < 5: my_func(i) i += 1
# try ... except ... try: my_func() except Exception as e: print(e) raise
break vs continue (pass)
for ... (else) ...
while/for ... try ... except ...
try ... except ... (else) ... (finally) ...
def divide(x, y): try: result = x / y except ZeroDivisionError: print("division by zero!") else: print("result is", result) finally: print("executing finally clause")
>>> divide(2, 1) ''' result is 2.0 executing finally clause ''' >>> divide(2, 0) ''' division by zero! executing finally clause ''' >>> divide("2", "1") ''' executing finally clause Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in divide TypeError: unsupported operand type(s) for /: 'str' and 'str' '''
# Python
# if if condition1: statement1 elif condition2: statement2 else: statment3
# for for i in range(5): my_func(i) # while i = 0 while i < 5: my_func(i) i += 1
# try ... except ... try: my_func() except Exception as e: print(e) raise
break vs continue (pass)
for ... (else) ...
while/for ... try ... except ...
try ... except ... (else) ... (finally) ...
from random import sample as love_from_boss while luck not in my_life: if holiday and love_from_boss([0]*364+[1], 1)[0]: print('Happiness!') My_life_my_choice(max_power=True, sleep_24h=True) wakeup() breakfasts() working() lunch() try: sleep(1) lifespan += 1 bonus -= 100 except: lifespan -= 1 working() try: bodyshaping() bodyweight -= 20 except: bodyweight += 20 dinner() while not luck: try: submit(working.output, stress=True) except Exception as e: print('Boss: 这是什么鬼?', e) print('Boss: 滚去加班!') overworking() else: sleeping(until = 7) break finally: lifespan -= 10 if lifespan <= 0: dead() print('Game over') break
# Python
std1 = { 'name': 'Michael', 'score': 98 } std2 = { 'name': 'Bob', 'score': 81 } # 处理学生信息可以通过函数实现,比如打印学生的成绩: def print_core(std): print('%s : %s'%(std['name'],std['score'])) print_core(std1) print_core(std2)
class Student(object): # 类 def __init__(self,name,score): self.name = name # 类的属性 self.score = score # 类的方法 def print_score(self): print('%s : %s'%(self.name,self.score)) amy = Student('amy',120) # 类对象的实例化 jack = Student('jack',108) amy.print_score() jack.print_score()
# Python
std1 = { 'name': 'Michael', 'score': 98 } std2 = { 'name': 'Bob', 'score': 81 } # 处理学生信息可以通过函数实现,比如打印学生的成绩: def print_core(std): print('%s : %s'%(std['name'],std['score'])) print_core(std1) print_core(std2)
class Student(object): # 类 def __init__(self,name,score): self.name = name # 类的属性 self.score = score # 类的方法 def print_score(self): print('%s : %s'%(self.name,self.score)) amy = Student('amy',120) # 类对象的实例化 jack = Student('jack',108) amy.print_score() jack.print_score()
import random as r class Fish(): # 父类 def __init__(self): self.x =r.randint(0,10) # 类属性 self.y =r.randint(0,10) def move(self): # 类方法 self.x -=1 # 一直向西移动 print("我的位置是:",self.x, self.y) # 利用继承演示鱼游动方向位置。 class Goldfish(Fish): # 子类 pass class Salmon(Fish): # 子类 pass class Shark(Fish): # 这里重写了__init__方法,就会覆盖掉父类的方法了, # 用到super函数后就可以继续使用父类的方法。 def __init__(self): # super函数不用给定任何基类的名字(如下),它会一层层找出代码所有父类里面对应的方法, # 要改变该类的继承关系时只需修改这个类的父类就行就是括号里面的Fish。 super().__init__() # super().重写的属性或方法 self.hungry = True def eat(self): if self.hungry: print("我要吃了。。。") self.hungry = False else: print('好饱了。。。')
# Python
os.getcwd()/os.listdir()/os.mkdir()/os.remore()/os.system()/...
os.sys.path/sys.path/sys.argv/sys.stdout/sys.stdin/sys.stderr/...
random.random()/random.sample()/random.seed()/...
from datetime import datetime, timedelta
now = datetime.today()
now.strftime("%m-%d-%y %H:%M:%S. %d %b %Y is a %A on the %d day of %B.")
# Python
# Python
My Simple Tips:
列表推导式
生成器
装饰器
递归函数
高阶函数库
编程思想
与
代码规范
Python_cheatsheet:
重构
2023 开发者生态系统现状https://www.jetbrains.com/lp/devecosystem-2023/
By He Wang
引力波数据探索:编程与分析实战训练营。课程网址:https://github.com/iphysresearch/GWData-Bootcamp