Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基础 #1

Open
MatNoble opened this issue Jan 6, 2021 · 6 comments
Open

基础 #1

MatNoble opened this issue Jan 6, 2021 · 6 comments
Labels
documentation Improvements or additions to documentation

Comments

@MatNoble
Copy link
Owner

MatNoble commented Jan 6, 2021

程序 = 数据结构 + 算法

name = input("I'm Ross, What's your name?")
print(f"Hi, {name}. Let's start coding!")

@MatNoble MatNoble changed the title 基础 - 栈 基础 - 栈 Stack Jan 6, 2021
@MatNoble MatNoble changed the title 基础 - 栈 Stack 基础 Jan 6, 2021
@MatNoble
Copy link
Owner Author

MatNoble commented Jan 6, 2021

数据类型

变量 Variable

  • 不可变:Number, String, Tuple
  • 可变:Bool, List, Set, Dictionary

布尔 Bool

True or False

and True False
True True False
False False False
or True False
True True True
False True False
not True False
False True

数字 Number

int, long, float, complex

  • // 向下取整:返回商的整数部分
365 // 10 # 36
5 // 3 # 1
-5 // 3 # -2
  • % 取模 - 返回除法的余数
365 % 10 # 5
5 % 3 # 2
# 奇数
num % 2 != 0
# 偶数
num % 2 == 0

字符串 String

  • f -> format
first_name = "Ross"
last_name = "MatNoble"
full_name = f"{first_name} {last_name}" # Ross MatNoble
  • 特殊符号
操作 说明
\ 转义符
\n 换行符
\t 制表符

r 避免转义
print(r"\\\t\\") # \\\t\\

操作 说明
.title() 首字母大写
.upper() 大写
.lower() 小写
.strip() 消除空格

列表 List[]

操作 说明
.insert(index, value)
.append(value)
.remove(value)
del list[index]
.pop()

直接赋值 & 浅拷贝(copy) & 深拷贝(deepcopy)

  • 直接赋值:其实就是对象的引用(别名)。
  • 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象。
  • 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象。

import copy
a = [1, 2, 3, 4, ['a', 'b']] #原始对象
 
b = a                #赋值,传对象的引用
c = copy.copy(a)        #对象拷贝,浅拷贝
d = copy.deepcopy(a)    #对象拷贝,深拷贝
 
a.append(5)               #修改对象a
a[4].append('c')          #修改对象a中的['a', 'b']数组对象
 
print( 'a = ', a )
print( 'b = ', b )
print( 'c = ', c )
print( 'd = ', d )

>>> ('a = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5])
>>> ('b = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5])
>>> ('c = ', [1, 2, 3, 4, ['a', 'b', 'c']])
>>> ('d = ', [1, 2, 3, 4, ['a', 'b']])

其他函数

操作 说明
.sort() 排序
.reverse() 逆序

元组 Tuple()

不可修改

字典 Dictionary{:}

dict = {key1:value1, key2:value2, key3:value3}


  • dict['key'] = value

  • del dict['key']

Open In Colab

集合 Set{}

唯一性

操作 说明
.add(value)
.update(value) 增, 列表、元组、字典
.remove(value)
.discard(value) 删,不报错

@MatNoble MatNoble added the documentation Improvements or additions to documentation label Jan 6, 2021
@MatNoble
Copy link
Owner Author

MatNoble commented Jan 7, 2021

if-elif-else 条件判断

if condition1:
    statement_block1
elif condition2:
    statement_block2
else:
    statement_block3

while 循环

while condition:
    expressions
a = range(10)
while a:
    print(a[a[-1]])
    a = a[:len(a) - 1] # 使用切片操作去掉最后一个元素

Open In Colab

for 循环

for item in sequence:
    expressions

range

# range(start, stop)
for i in range(2, 7):
    print(i)
print('##################')
# range(stop) # form 0
for i in range(5):
    print(i)
print('##################')
# range(start, stop, step)
for i in range (1, 10, 2):
    print(i)

Open In Colab

@MatNoble
Copy link
Owner Author

MatNoble commented Jan 7, 2021

函数 function

def function_name(parament):
    expression
def mergeSum(L1, L2):
    Sum = []
    while True:
        sum = 0
        if L1:
            sum += L1[0]
            del L1[0]
        if L2:
            sum += L2[0]
            del L2[0]
        Sum.append(sum)
        if not (L1 or L2):
            break
    return Sum

Open In Colab

@MatNoble
Copy link
Owner Author

MatNoble commented Jan 7, 2021

高级特性

廖雪峰

Open In Colab

Repository owner locked as off-topic and limited conversation to collaborators Jan 7, 2021
@MatNoble MatNoble pinned this issue Jan 7, 2021
@MatNoble
Copy link
Owner Author

MatNoble commented Jan 8, 2021

类 class

面对对象编程(Object-Oriented Programming):

  1. 封装(Encapsulation):对外部世界隐藏对象的工作细节。
  2. 继承(Inheritance):继承使子类具有父类的各种属性和方法,而不需要编写相同的代码。
  3. 多态(Polymorphism):为不同的数据类型的实体提供统一的接口。

class:

  • 属性 attribute
  • 方法 method

图灵星球

创建一个Car类,其中包含brand,model,year属性。这个类初始化的时候必须要传入品牌名字(比如Subaru),初始化的时候车的型号和生产年会被默认设定为’xxx’和0,Car类还有内置方法可以修改车型号和生产年,还有可以打印出完整车信息的方法。
请根据要求定义出这个完整的类,并在另一个文件调用此类,创建一个实例,然后使用内置方法修改车的型号和生产年,最后使用类的方法打印出车的完整信息。

# car.py
class Car:
    # brand, model, year

    def __init__(self, brand): 
        self.brand = brand
        self.model = 'xxx'
        self.year = 0
   
    def set_model(self, model):
        self.model = model

    def set_year(self, year): 
        self.year = year

    def get_info(self): 
        print(f"Brand: {self.brand}, Model: {self.model}, Year: {self.year}")
# main.py
from car import Car
new_car = Car("Subaru")
new_car.set_model("WRX")
new_car.set_year(2014)
new_car.get_info()

@MatNoble
Copy link
Owner Author

MatNoble commented Jan 8, 2021

代码规范

  • 类的名字最好使用骆驼命名法 CamelCase,也就是让每个单词的第一个字母大写,不使用下划线分割单词。
  • 实例和模块的名字最好都使用 Snake case,也就是所有字母都小写,然后使用下划线分割。

  • 在一个中,您可以在方法之间使用一个空行
  • 在一个模块中,您可以使用两个空行来分隔类

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant