1. Trang chủ
  2. » Mẫu Slide

Slide Python Functions và Packages

20 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Trong Python, một hàm là một nhóm các câu lệnh liên quan thực hiện một tác vụ cụ thể. Các hàm giúp chia nhỏ chương trình của chúng ta thành các phần nhỏ hơn và tổ chức lại theo từng môđun. Khi chương trình của chúng ta phát triển ngày càng lớn hơn, các hàm giúp cho nó dễ tổ chức và dễ quản lý hơn. Hơn nữa, nó tránh lặp lại các đoạn code và làm cho code có thể được sử dụng lại.

Scientific Programming Language NGƠN NGỮ LẬP TRÌNH LẬP TRÌNH P TRÌNH KHOA HỌCC Nguyễn Mạnh Cường Scientific Programming Language BÀI FUNCTIONS & PACKAGES Scientific Programming Language BÀI 2: FUNCTIONS & PACKAGES • Python functions (hàm Python) • Arguments (đối số) • Global variables (biến tồn cục) • Python modules (module Python) Scientific Programming Language BÀI Python functions • Python functions – Hàm Python • Định nghĩa hàm o Ví dụ 2.1: Viết hàm tính n! Sử dụng hàm để tính: P = (n+1) ! + (2n) ! Arguments Global variables Python modules Python package def fuctorial(n): g = for i in range(1, n + 1): g = g * i return g n = int(input("n = ")) P = fuctorial(n+1) + fuctorial(2*n) print("Result: P= ", P) Bài 2: Functions & Modules Scientific Programming Language BÀI Python functions Arguments • Python functions – Hàm Python • Định nghĩa hàm def tên_hàm ( [đối_số]đối_số] ): lệnh 1 o tên_hàm : Đặt theo quy tắc lệnh 2 giá o [đối_số]đối_số] : Thông qua ta truyền Global variables Python modules Python package trị vào hàm o lệnh 1, lệnh 2, : Các câu lệnh thân hàm o return giá_trị_1, Lệnh return sửgiá_trị_2, dụng để trả giá trị Bài 2: Functions & Modules Scientific Programming Language BÀI Python functions • Python functions – Hàm Python def • Định nghĩa hàm o Ví dụ 2.2: Viết hàm tìm số lớn Arguments ba số nguyên a, b, c Sử dụng hàm để Global variables Python modules Python package tìm số lớn số x, y, z, t, q max(a, b, c): m = a if b > m: m = b if c > m: m = c return m x = int(input("x = ")) y = int(input("y = ")) z = int(input("z = ")) t = int(input("t = ")) q = int(input("q = ")) F = max(x, y, z) S = max(F, t, q) print("Max of numbers: ", S) Bài 2: Functions & Modules Scientific Programming Language BÀI Python functions • Python functions – Hàm Python  BÀI TẬP 2.1 o Viết hàm tính khoảng cách Euclidean hai điểm A(x1, y1) B(x2, y2) Arguments Global variables Python modules Python package o Viết hàm kiểm tra xem hai điểm A, B, điểm gần tâm O o Chương trình chính: Nhập vào tọa độ hai điểm A, B Sử dụng hàm để tính in ra: - Khoảng cách A, B - Chu vi tam giác OAB - Cho biết A hay B gần tâm O Bài 2: Functions & Modules Scientific Programming Language BÀI Python functions • Arguments – Tham số • Truyền tham số cho hàm def greet(name, msg): print("Hello", name + ', ' + msg) Arguments Global variables greet("Monica", "Good morning!") o Nếu hàm có đối số, gọi hàm, ta cần truyền tham số cho hàm Python modules Python package o Tham số truyền vào cần: - Đủ số lượng - Đúng thứ tự Bài 2: Functions & Modules Scientific Programming Language BÀI Python functions Arguments Global variables Python modules Python package • Arguments – Tham số • Tham số có giá trị mặc định – Default def greet(name, msg = “How are arguments you ?”): print("Hello", name + ', ' + msg) greet("Monica") greet("Monica“, “My name is Ricky”) o Nếu đối số có giá trị mặc định, gọi hàm ta có hai lựa chọn: - Khơng truyền tham số cho đối số - Truyền tham số bình thường o Khơng định nghĩa đối số “không mặc định” theo sau đối số “mặc định” Bài 2: Functions & Modules Scientific Programming Language BÀI Python functions • Arguments – Tham số • Truyền số theo từ khóa def tham greet(name, msg): print("Hello", name + ', ' + msg) greet(name = "Monica“, msg = “How are Arguments you?”) greet(msg = “How are you?”, name = Global variables Python modules "Monica“) o Sử dụng từ khóa “tên đối” để truyền thamare số: you?”) greet("Monica“, msg = “How Tên_đối_số = Giá_trị Python package o Khơng truyền tham số theo từ khóa, theo sau tham số khơng theo từ khóa Bài 2: Functions & Modules 10 Scientific Programming Language BÀI • Arguments – Tham số • Đối số tập hợp (các - tuple) Python functions def greet(*names): print("Hello", name) Arguments greet("Monica", "Luke", "Steve", "John") Global variables Python modules Python package o Sử dụng đối số tuple: ‒ ‒ *tên_đối Số lượng tham số truyền vào linh động Bài 2: Functions & Modules 11 Scientific Programming Language BÀI Python functions Arguments Global variables Python modules Python package • Global Variables – Biến tồn cục • Sử dụng biến tồn cục k=0 o Biến khai báo bên ngồi hàm o Có thể truy cập bên bên hàm o Trong hàm, muốn sử dụng biến toàn cục, cần khai báo: # Biến k biến toàn cục def Func1(): k=3 # biến k cục print("K inside Func1= ", k) def Func2(): global k # Chỉ định k biến toàn cục k=5 print("K inside Func2= ", k) global tên_biến_toàn_cục Func1() print("K outside: ", k) Func2() Bài 2: Functions & Modules 12 Scientific Programming Language BÀI Python modules • Lập trình theo modules Python functions o Module: tệp chứa câu lệnh định nghĩa Python Mỗi file py coi module, tên module tên file Arguments o Một chương trình tạo nên từ nhiều module Một module sử dụng nhiều chương trình Global variables Python modules Python package o Để sử dụng module: import toàn module import tên_module [as as bí_danh]  Trong trường hợp khơng tìm module, chương trình báo lỗi (Đặt đường dẫn tới thư mục chứa module: xem trực tiếp Pycharm.) Bài 2: Functions & Modules 13 Scientific Programming Language BÀI Python modules • import mymodule.py Python functions Arguments PI = 3.14 def add(a, b): return a + b Global variables Python modules Python package  Để xem module có chứa gì, ta dùng dir: import mymodule as m print(dir(m)) myprogram.py import mymodule a=3 b=5 print("Tong = “, mymodule.add(a, b)) print("PI = ", mymodule.PI) myprogram.py import mymodule as m a=3 b=5 print("Tong = “, m.add(a, b)) print("PI = ", m.PI) Bài 2: Functions & Modules 14 Scientific Programming Language BÀI Python functions Python modules • from/ import mymodule.py myprogram.py PI = 3.14 from mymodule import add from mymodule import PI as P a=3 b=5 print("Tong = “, add(a, b)) print("PI = ", P) Arguments Global variables def add(a, b): return a + b Python modules o Chỉ import vài thứ module: Python package from tên_module import tên [as as bí_danh] Bài 2: Functions & Modules 15 Scientific Programming Language BÀI Python functions Python modules • import * from mymodule.py PI = 3.14 Arguments myprogram.py from mymodule import * a=3 b=5 print("Tong = “, add(a, b)) print("PI = ", PI) Global variables def add(a, b): return a + b Python modules o import tất tên module: from tên_module import * Python package Bài 2: Functions & Modules 16 Scientific Programming Language BÀI Python functions Arguments Global variables Python modules Python package Python modules  BÀI TẬP 2.2 o Tổ chức chương trình thành module: Module 1: - Định nghĩa tỷ giá: USD = 23000, EUR = 26000, RUB = 170 Module 2: - Các hàm quy đổi n USD/ EUR/ RUB VND (ba hàm) - Định nghĩa hàm cộng ba số thực, cộng hai số thực Chương trình chính: - Sử dụng hàm hai module để: Nhập vào số tiền USD, EUR, RUB có; In tổng số tiền VND sau quy đổi Bài 2: Functions & Modules 17 Scientific Programming Language BÀI Python functions Python package • Package o Package: gói chứa gói (sub-package) module o Trong package/ sub-package có chứa: Arguments Global variables - File dấu: - Các file module - Subpackages init .py (bắt buộc) Python modules Python package Bài 2: Functions & Modules 18 Scientific Programming Language BÀI Python package • Package o Cấu trúc package (ví dụ) Python functions Arguments Global variables Python modules Python package Bài 2: Functions & Modules 19 Scientific Programming Language BÀI Python package  BÀI TẬP 2.3 o Tạo package theo cấu trúc sau Python functions Arguments MYPACK SUBPACK1 SUBPACK2 Global variables module1.py module2.py Python modules Với module1.py module2.py hai module tạo BÀI TẬP 2.2 Python package o Viết chương trình BÀI TẬP 2.2 để sử dụng package Bài 2: Functions & Modules 20

Ngày đăng: 27/07/2023, 23:46

Xem thêm: