Giter Site home page Giter Site logo

python-tu's Introduction

My Python

PRs Welcome license

    ____        __  __                      __
   / __ \__  __/ /_/ /_  ____  ____        / /___  __
  / /_/ / / / / __/ __ \/ __ \/ __ \______/ __/ / / /
 / ____/ /_/ / /_/ / / / /_/ / / / /_____/ /_/ /_/ /
/_/    \__, /\__/_/ /_/\____/_/ /_/      \__/\__,_/
      /____/

Control Flow

  1. if Statements
    • Cấu trúc điều kiện if...else
    • Tùy chọn if...elif...elif... thay thế switch...case trong các ngôn ngữ khác
  2. for Statements
    • Vòng lặp for lặp qua cái item của sequence(list or string)
  3. The range() Function
    • Sử dụng range(n) vòng lặp chạy từ 0 đến n-1
  4. break and continue Statements, and else Clauses on Loops
    • break: Dừng chương trình
    • continue: Dùng để tiếp tục vòng lặp tiếp
  5. pass Statements
    • Sử dụng để yêu câu phải có cú pháp. Câu lệnh này không làm gì cả
  6. Defining Functions
    • Sử dụng def khai bao Function
    • Tạo 1 Function tính giai thừa
    def Factotial(n):
        while (n>0):
            if (n == 1):
                return 1
            else: return n*Factotial(n-1)

Data Structures

  1. More on Lists
    • list.append(x): Thêm phần tử vào cuối List tương đương a[len(a):] = [x]
    • list.extend(iterable): Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable
    • list.insert(i, x): Chèn item x vào vị trí i. a.insert(len(a), x) is equivalent to a.append(x)
    • list.remove(x): Remove item có giá trị bằng x. Trả về ValueError nếu không tìm thấy item đó
    • list.pop([i]): Remove item ở vị trí i và trả về giá trị của nó. a.pop() remove và trả về giá trị của item cuối cùng của list
    • list.clear(): Remove toàn bộ item trong list
    • list.index(x[, start[, end]]): Trả về vị trí của item trong lists. Trả về ValueError nếu không tìm thấy item đó.
    >>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
    >>> fruits.index('banana')
    4
    >>> fruits.index('banana', 4)
    6
    • list.count(x): Trả về số lần xuất hiện của item x trong list, nếu không có item x trong x trả về 0
    • list.sort(key=None, reverse=False): Sắp xếp các item trong list
    • list.reverse(): Đảo ngược lại list
    • list.copy(): Return a shallow copy of the list Equivalent to a[:]
  2. Using Lists as Stacks
    • LIFO
    >>> stack = [3, 4, 5]
    >>> stack.append(6)
    >>> stack
    [3, 4, 5, 6]
    >>> stack.pop()
    6
    >>> stack
    [3, 4, 5]
  3. Using Lists as Queues
    • FIFO. However, lists are not efficient for this purpose
    • Để thực hiện như 1 hàng đợi, sử dụng collections.deque
    >>> from collections import deque
    >>> queue = deque(["Eric", "John", "Michael"])
    >>> queue.append("Terry") 
    >>> queue
    deque(['Eric', 'John', 'Michael', 'Terry'])
    >>> queue.popleft()
    'Eric'
    >>> queue
    deque(['John', 'Michael', 'Terry'])
  4. List Comprehensions
    • List Comprehensions cung cấp các cách ngắn gọn để tạo ra List
    • Tạo 1 list các số chính phương
    >>> squares = []
    >>> for x in range(10):
    ...     squares.append(x**2)
    ...
    >>> squares
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    • Có thể tạo list theo cách sau
    squares = list(map(lambda x: x**2, range(10)))
    or
    squares = [x**2 for x in range(10)]
    
  5. Nested List Comprehensions
  6. The del statement
    • Remove item trong list, khác với pop() trả về item bị xóa.
    >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
    >>> del a[0]
    >>> a
    [1, 66.25, 333, 333, 1234.5]
    >>> del a[2:4]
    >>> a
    [1, 66.25, 1234.5]
    >>> del a[:]
    >>> a
    []
  7. Tuples and Sequences
    • Tuples are immutable, Lists are mutable
  8. Sets
    • Set là tập các element không trùng nhau, không có thứ tự
    >>> S = {'a', 'b', 'c', 'd', 'e', 'a'}
    >>> S
    {'a', 'b', 'c', 'd', 'e'}
    >>> set(S)
    {'e', 'a', 'b', 'd', 'c'}
  9. Dictionaries
    • Dictionaries đánh dấu bằng key, là cặp key: value
    >>> animals = {'cat': 'meow', 'chipu': 'ooo', 'mouse': 999}
    {'cat': 'meow', 'chipu': 'ooo', 'mouse': 999}
    >>> animals['bird'] = 0
    >>> animals
    {'cat': 'meow', 'chipu': 'ooo', 'mouse': 999, 'bird': 0}
    • dict() xây dựng dictionaries từ các cặp key-value
    >>> dict([('J', 11), ('Q', 12), ('K', 13), ('A', 1)])
    {'J': 11, 'Q': 12, 'K': 13, 'A': 1}

Modules and Packages

  1. Modules
    • Module: Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter.
    • Các definitions từ 1 module có thể được import vào module khác hoặc module chính.
    • Tạo file fibo.py
    def fib(n):    # write Fibonacci series up to n
        a, b = 0, 1
        while a < n:
            print(a, end=' ')
            a, b = b, a+b
        print()
    
    def fib2(n):   # return Fibonacci series up to n
        result = []
        a, b = 0, 1
        while a < n:
            result.append(a)
            a, b = b, a+b
        return result
    • import fibo trong interpreter
    >>> import fibo
    >>> fibo.fib(1000)
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
    >>> fibo.fib2(100)
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    >>> fibo.__name__
    'fibo'
    • Trong trường hợp nếu không muốn sử dụng toàn bộ module mà chỉ sử dụng 1 phần, sử dụng from...import
    >>> from fibo import fib
  2. Packages
    • Package trong Python là một thư mục chứa một hoặc nhiều modules hay các package khác nhau, nó được tạo ra nhằm mục đích phân bố các modules có cùng chức năng hay một cái gì đó, để dễ quản lý source code
    • Tạo 1 package chỉ cần tạo 1 thư mục, tên thư mục là tên package và phải có file __init__.py, file này sẽ được gọi ra đầu tiên khi import package

Errors and Exceptions

  1. Errors
  2. Exceptions
    • Xử lý lỗi ngoại lệ, là Error được phát hiện khi thực hiện chương trình.
    • Tạo chương trình tính thương 2 số
    def test(a, b):
        return a / b
    print(test(6, 0))
    Kết quả
    Traceback (most recent call last):
        File "test.py", line 4, in <module>
            print(test(6, 0))
        File "test.py", line 2, in test
            return a / b
    ZeroDivisionError: division by zero
  3. Handling Exceptions
    • Sử dụng try...except
    • Nếu khối lệnh trong try có 1 lỗi gì đó xảy ra thì chương trình sẽ tìm các except bên dưới, nếu có except thỏa mãn nó sẽ thực thi code trong khối except đó
    def test(a, b):
        return a / b
    
    try :
        print(test(6, 0))
    except ZeroDivisionError:
        print('Bug Bug !!!')
    Kết quả
    Bug Bug !!!
  4. Raising Exceptions
    • Cho phép chỉ định exception khi xảy ra lỗi
    >>> raise ValueError('Hello World !!!')
    Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
    ValueError: Hello World !!!

Classes

  • Khai báo class
class Person:
    pass:
  • Khai báo các thuộc tính (name, age, sex) và các phương thức (getName(), setAge(), getAge(), setMale(), getMAle()) trong class
class Person:
    # thuộc tính
    name = "DUCTN";
    age = 22;
    male = True
    # phương thức
    def setName(self, name):
        self.name = name
    
    def getName(self):
        return self.name
    
    def setAge(self, age):
        self.age = age
    
    def getAge(self):
        return self.age
    
    def setMale(self, male):
        self.male = male
    
    def getMale(self):
        return self.male
  • Khởi tạo class sau khi khái báo
person = Person()

Virtual Environments

  1. Intro
    • Khi thực hiện 1 project, việc tạo ra 1 môi trường ảo giúp project thực hiện được độc lập với các project khác, phù hợp theo yêu cầu, đúng phiên bản với các modules hay packages sử dụng mà không nằm trong thư viện chuẩn của python.
    • Tránh bị conflic giữa các app khi đang code.
    • Các app khác nhau có thể sử dụng trong các môi trường khác nhau
  2. Creating Virtual Environments
    • Tạo môi trường ảo tên ductn
    $ python3 -m venv ductn
    • Kích hoạt môi trường
    $ source ductn/bin/activate
    • Cài đặt các gói package Tải ver mới nhất
    (ductn) $ pip install requests
    Tải ver cụ thể
    (ductn) $ pip install requests==2.6
    Update ver
    (ductn) $ pip install --upgrade requests
    • Khi có thông tin về tên và ver của các packages trong requirements.txt, chỉ cần thực thi lệnh sau để cài đặt.
    $ pip install -r requirements.txt
    • Đưa lists các packages và 1 file, sử dụng
    $ pip freeze > requirements.txt
    
  3. ML Basic
  4. Algorithm
  5. Syntax basic
  6. Syntax basic_2
  7. Network automation

Show your support

Give a ⭐ if you like this repo ❤️

Contribution

All contributions are welcomed in this project.

License

The MIT License (MIT). Please see LICENSE for more information.

python-tu's People

Contributors

ductnn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.