Python学习笔记

面向过程:根据业务逻辑从上到下写垒代码
函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
参考:一起来学习 Python 函数式编程
面向对象:对函数进行分类和封装,让开发“更快更好更强…”
参考:全面深入理解Python面向对象编程
脚本(scripting)语言,因为它们可以用于编写简短而粗糙的小程序(也就是脚本)。
isinstance(a, int):可用于检查一个对象是否是某个特定类型的实例

库Library/包Package/模块Module

模块Module

用来从逻辑上组织Python代码(变量、函数、类、逻辑:实现一个功能),本质就是.py结尾的Python文件(文件名:test.py,对应的模块名:test
导入模块的本质就是把Python文件解释一遍
(import test test=‘all code of test.py’)
(from test import name name=‘code’)
导入整个模块到命名空间:(When we import a module this way, we add all of the objects and functions within that module to the global namespace.)

import math:调用时使用math.function1、math.function2、、、

import math as m:调用时使用m.function1、m.function2、、、

仅导入模块中所需要的类/函数/变量:(If a module contains hundreds of functions and we only need a few, specific functions, we can import just the ones we need.)

from math import function1 ,function2:调用时直接使用function1、function2、、、
from math import * :can import all of the objects (this includes both objects and functions) from a module into the global namespace using the * symbol

包Package

用来从逻辑上组织模块的,本质就是一个目录(必须带有一个__init__.py文件)
导入包的本质就是执行该包下的__init__.py文件

##把CSV文件转换成list of lists对象(三种方法)
#方法一:
fo = open(fname)
ls = []
for line in fo:
    line = line.replace("\n","")
    ls.append(line.split(","))
fo.close()
#方法二:
f = open("movie_metadata.csv", "r")    #CSV:"Comma-Separated Values",逗号分隔值:行内以","分隔,行间以"\n"分隔
data = f.read()                        #data是字符串对象,包含分隔符\n,形如:Albuquerque,749\nAnaheim,371\nAnchorage,828\n...
rows = data.split("\n")
movie_data = []
for row in rows:
    movie_data.append(row.split(","))  #movie_data is a list of lists
#方法三:
import csv
f = open("movie_metadata.csv", 'r')#f TextIOWrapper (<class '_io.TextIOWrapper'>) <_io.TextIOWrapper name='nfl.csv' mode='r' encoding='UTF-8'>
csvreader = csv.reader(f)      #csvreader reader (<class '_csv.reader'>) <_csv.reader at 0x7f259de10208>
movie_data = list(csvreader)   #movie_data is a list of lists

函数Function(Method):

f = open("movie_metadata.csv", "r")    
data = f.read()                        
rows = data.split("\n")
movie_data = []
for row in rows:
    movie_data.append(row.split(","))

open: The open() function returns a File object. This object stores the information we passed in, and allows us to call methods specific to the Fileclass. We can assign the File object to a variable so we can refer to it later:

f = open("movie_metadata.csv", "r"). Note that the File object, f, a wrapper for the file, won’t contain the actual contents of the file. It’s instead an object that acts as an interface to the file and contains methods for reading in and modifying the file’s contents. TextIOWrapper is a built-in Python object that represents the File handler.
data = f.read():File objects have a read() method that returns a string representation of the text in a file.
data is string object, like this: Albuquerque,749\nAnaheim,371\nAnchorage,828\n

rows = data.split('\n') :rows is list object, like this: ['Albuquerque,749', 'Anaheim,371', 'Anchorage,828']
enumerate函数:Python内建了一个enumerate函数,可以返回(i, value)元组序列:

for i, value in enumerate(collection):
	# do something with value

类Classes:

object-oriented programming language: integers, floats, strings, and anything else you can imagine is an object which is created from a class.
同样的类共享相似的函数
这里写图片描述
这里写图片描述

语句Statement:

n=(n+1)<<1:前提n得有一个初始值,比如n=1,不然会报错.那么n=n+1的意思就1+1再赋值给变量n,那么n就等于2。然后<<符号是按位左移的意思,就是把对象转为2进制,有效数字往左移动。<<1就是按位左移1个单位。

变量Variable:

parameter:形参(formal parameter),函数定义中的参数,是一个变量(variable)
argument:实参(actual parameter),函数调用时的实际参数,即inputs参数,是一个值(value)
attribute:属性、特征、价值
method:方法、函数,即function
这里写图片描述
变量作用域:全菊变量和菊部变量

关于全局变量、局部变量、形参、实参、变量作用域

函数参数传递:关于函数参数传递,80%人都错了
可变对象与不可变对象:可变对象与不可变对象

可变对象包括 list、dict、set、自定义类型等;
不可变对象包括 int、float、bool、str、tuple 等。
不可变对象可以作为字典 dict 的键 key

杂Others:

在windows cmd中运行Python脚本:

C:\Users\Famir>d:
D:\>cd /Study/MOOC/Python Programming/examples
D:\Study\MOOC\Python Programming\examples>python example04.py

Python has the in statement:

animals = ["cat", "dog", "rabbit"]  
if "cat" in animals:  
    print("Cat found")  
animals = ["cat", "dog", "rabbit"]  
cat_found = "cat" in animals 

将字符串型列表转化为整数型列表:int_list = [int(i) for i in string_list]
To convert a list to a string call str(my_list).
python enumerate用法总结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值