Vu Python - 备考宝典

此篇博客注重概念的总结,以及它们的英文表述。

来吧,对着PPT抠定义,走起!

Topic 2 – Operators and Variables

1.What is an expression?
An expression is made of operands (values) and operators.
表达式由操作数(值)和运算符组成。
1
2.Order of Precedence(优先级)

3.What is a variable?
A variable is a memory location in computer memory. It has a type and a name. This memory location can store different values of the same type.(变量是计算机内存中的一个内存位置。它有一个类型和一个名称。此内存位置可以存储同一类型的不同值。)

习题讲解:
Variables例题精讲

Selection and Repetition Structures

好像没啥概念,刷下例题吧。
Selection and Repetition Structures例题精讲

Strings and Text files

别急,概念集中在class类那个章节里。先看看例题吧。
Strings and Text files例题精讲

Lists and Dictionaries

1.The values in lists are called elements or sometimes items.

2.一个列表也可以是列表中的元素
如:[‘John’, 2.0, 5, [10, 20]] is also a list

3.The index starts at 0.
索引从0开始

4.Tuples(与 list 的区别是元组不可修改)

  • Unlike lists, tuples are immutable.
  • Structure of a tuple can not be changed
  • Tuples are used when there is a need of an unchanging list.
  • 定义元组时如果只有一个元素则需要加英文逗号。这样是为了避免和增加优先级的那个小括号混交。例:

错误写法:

list = (1)
print(list)

运行结果:

1

正确写法:

list = (1,)
print(list)

运行结果:

(1,)
  • A list can be converted to a tuple
list = [7,"whh"]
print(tuple(list))

运行结果:

(7, 'whh')

5.Dictionaries
格式: {key : value , key : value ……}
eg:

list = {7:"whh","whh":7 }
print(list)

运行结果:

{7: 'whh', 'whh': 7}

注意:

  • In dictionaries, keys can be of any data type.
  • Keys are unique in each dictionary
  • Keys must be of an immutable data type such as strings, numbers, or tuples.
list = {7:"whh","whh":7 }
print(list)
print(list.keys())
print(list.values())
list["whh"] = "whh" # 修改字典中的值
print(list)

运行结果:

{7: 'whh', 'whh': 7}
dict_keys([7, 'whh'])
dict_values(['whh', 7])
{7: 'whh', 'whh': 'whh'}

具体题目解析请点击下方链接

Lists and Dictionaries例题精讲

Design with Functions

1.Why use functions?

  • Functions avoid repetitive code
  • Function modular code
  • Helps division of labor

2.Top down design
Top down design is breaking a large problem into several small problems.
And then solving each small problem in order to solve the initial large problem.(说白了就是自定义函数将问题拆分)

3.Recursive Functions
A recursive function is a function that calls itself.

4.variable

  • Local variable(局部变量)
    A local variable can only be used inside the function it is declared.
  • Global variable(全局变量)
    A global variable is declared in the body of the program and can be used by any function in the program.
  • Instance variable(实例变量)
    Instances variables must begin with self.

Turtle Graphics

海龟画图这节没啥定义,看看实例吧。
海龟画图实例

Graphical User Interface

1.widgets
GUI programs use objects called widgets.
Examples of widgets are: Label,Entry Field,Button,Radio button, etc.

2.Benefits of using GUI

  • The user is not constrained to enter inputs in a particular order.
  • Running different data sets does not require re-entering all of the data.
  • The user cannot enter an unrecognized command.

3.What is Tk?
Tk is a free and open-source, cross-platform widget toolkit.
It provides a library of basic elements of GUI widgets

4.What is Tkinter?
Tkinter is a set of wrappers that implement the Tk widgets as Python classes.

5.mainloop()
mainloop() function start an infinite event-loop that processes user events by calling an endless loop of the window.

Graphical User Interface例题精讲

Design with Classes

1.Class
A Class is a user defined datatype which contains properties and methods in it.

2.object
Objects are the real ‘things’ that is generated using the class definition.

A class is a blueprint of an object. An object is an instance belonging to a class.

3.def __ init __

  • def __ init __ is the constructor of the class.
  • init must begin and end with two underscores.
  • The purpose of the constructor is to initialize an individual object’s attributes.

4.__ str __
__ str __ method builds and return a string representation of an object’s state.

5.Accessor Method(“get” Method)
An accessor method is used to return the value of an instance variable. These methods are also called ‘get’ methods. They simply allow the user to observe the state of an object but not to change.

6.Mutator Method(“set” Method)
A mutator method is used to set a value of an instance variable. These methods are also called ‘set’ methods. They allow the user to modify the state of an object.

Design with Classes例题精讲

Object Oriented Programming Concepts(OOP)

  • Encapsulation(封装)
  • Abstraction(抽象)
  • Inheritance(继承)
  • Polymorphism(多态)

1.Encapsulation
This means that the internal representation of an object can not be seen from outside of the objects definition.
To read values of the created object ‘get’ and __ str __ methods were used.

2.Abstraction
Abstraction and Encapsulation are closely related.
Abstraction is achieved by encapsulation.
简单说就是因为封装了,所以看不见,所以抽象。

3.Inheritance
eg:
在test1.py文件中写父类

class Employee(object):
    def __init__(self,empname,empnum,empposition):
        self._empname = empname
        self._empnum = empnum
        self._empposition = empposition

    def getname(self):
        return self._empname

    def getnum(self):
        return self._empnum

    def getpostion(self):
        return self._empposition

换个py文件写子类:

from test1 import Employee

class permEmployee(Employee):
    def __init__(self,empname,empnum,empposition,salary):
        # 通过继承,将父类的变量传给子类
        Employee.__init__(self,empname,empnum,empposition)
        self._salary = salary

    def getsalary(self):
        return self._salary

    def calcBonus(self):
        bonus = (self._salary * 0.1) + 500
        return bonus

    def __str__(self):
        # 子类继承父类中的方法,注意self不可省略
        return Employee.getname(self) + "Bonus : $ " + str(self.calcBonus())


pe = permEmployee("whh","777","Analyst",85000)
print(pe)

运行结果:

whhBonus : $ 9000.0

Benefits of using inheritance?

  • it is possible to re-use code and reduce duplicate code.
  • Saves time in programming due to reusability of code.
  • Subclasses becomes more manageable which helps extending individual subclasses

4.Polymorphism

Different subclass objects call the same parent method to produce different execution results.

Benefits of using Polymorphism?
Polymorphism can increase the flexibility of code

eg:
父类class类:

class Animal(object):
    def __init__(self,name,mood):
        self._name = name
        self._mood = mood

    def getname(self):
        return self._name

    def getmood(self):
        return  self._mood

    def speak(self):
        print("Heard a " + self._mood + " " + self._name)

子类:

from test1 import Animal

class Goat(Animal):
    def __init__(self,name,mood):
        Animal.__init__(self,name,mood)

    def speak(self):
        Animal.speak(self) # 调用父类speak方法


class Monkey(Animal):
    def __init__(self, name, mood):
        Animal.__init__(self, name, mood)

    def speak(self):
        Animal.speak(self) # 调用父类speak方法

ans1 = Goat("Goat","happy")
ans2 = Monkey("Monkey","unhappy")
ans1.speak()
print()
ans2.speak()

运行结果:

Heard a happy Goat

Heard a unhappy Monkey

解析:
可见,同样是调用父类的speak方法,但是他们的输出却不一样,这便是多态。

发布了54 篇原创文章 · 获赞 27 · 访问量 2681

猜你喜欢

转载自blog.csdn.net/Deam_swan_goose/article/details/103588146
今日推荐