<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Hàm max&#40;&#41; trong Python</title>
<meta name="description" content="Hàm max&#40;&#41; trong Python - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;ham-max-trong-python-10387.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Hàm max&#40;&#41; trong Python">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;ham-max-trong-python-10387.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-max-trong-python-10387.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-max-trong-python-10387.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Hàm max&#40;&#41; trong Python</h1>
		<ul class="list-inline">
			<li>Thứ sáu - 16/05/2025 00:24</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Hàm max&#40;&#41; trong Python" src="https://st.quantrimang.com/photos/image/2019/07/30/ham-tich-hop-san-python-640.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p><strong>Hàm Max trong Python&nbsp;</strong>được sử dụng như thế nào? Bài viết sẽ cho bạn biết mọi điều về<strong>&nbsp;hàm Max Python</strong>.</p>
<br />
<p><img alt="Hàm Max trong Python" data-i="0" data-src="https://st.quantrimang.com/photos/image/2019/07/30/ham-tich-hop-san-python-640.jpg" data-was-processed="true" height="335" src="https://st.quantrimang.com/photos/image/2019/07/30/ham-tich-hop-san-python-640.jpg" width="640" /></p>

<p>Khi làm việc với dữ liệu trong Python, bạn thường cần xác định giá trị lớn nhất trong một nhóm các mục. Đây là lúc hàm max() của Python phát huy tác dụng. Là một trong những hàm tích hợp của Python, max() xác định hiệu quả giá trị lớn nhất/phần tử lớn nhất từ một tập hợp đầu vào nhất định. Cho dù bạn đang so sánh số, chuỗi hay các đối tượng khác, max() là giải pháp phù hợp với bạn.</p>

<p>Ở bài viết này, chúng ta sẽ cùng nhau thảo luận về cách dùng hàm max trong Python, bao gồm hai dạng chính của nó—một để so sánh các đối tượng hoặc biến và một để làm việc với các đối số có thể lặp lại. Cuối cùng, bạn sẽ được trang bị hiểu biết vững chắc về cách khai thác hàm max() của Python cho nhiều trường hợp sử dụng thực tế khác nhau, từ các phép so sánh đơn giản đến thao tác dữ liệu nâng cao.</p>

<p>Giờ hãy cùng khám phá chi tiết cách dùng hàm Max trong Python nhé!</p>

<p><strong>Hàm max()</strong>&nbsp;được tích hợp sẵn trong Python trả về phần tử lớn nhất trong một iterable hoặc lớn nhất trong những tham số truyền vào.</p>
&nbsp;

<p>Nếu các giá trị là các chuỗi sẽ so sánh theo thứ tự chữ cái alphabet.</p>

<figure><img alt="Hàm max() trong Python" data-i="1" data-src="https://st.quantrimang.com/photos/image/2019/07/31/ham-max-python-640-1.jpg" data-was-processed="true" height="335" src="https://st.quantrimang.com/photos/image/2019/07/31/ham-max-python-640-1.jpg" width="640" /></figure>

<h3>Cú pháp hàm max() trong Python</h3>

<p>Hàm max() trong Python có 2 dạng:</p>

<pre id="pre0">
max(iterable, *iterables&#91;,key, default&#93;)
</pre>

<p>Hoặc:</p>

<pre id="pre1">
max(item1, item2, *item&#91;, key&#93;)
</pre>

<h3>Các tham số của hàm max()</h3>

<p><strong>Hàm max()</strong>&nbsp;làm việc với hai dạng tham số tương ứng với hai cú pháp đã nêu ở trên:</p>

<p>1.&nbsp;<code>max(iterable, *iterables&#91;, key, default&#93;)</code></p>

<ul>
	<li><code>iterable</code>: Bắt buộc. Các tuple, string, set, dictionary hoặc đối tượng iterator mà bạn cần tìm phần tử lớn nhất trong đó.</li>
	<li><code>*iterables</code>: Tùy chọn. Iterable nào lớn nhất sẽ được trả về.</li>
	<li><code>key</code>: Tùy chọn. Key function, nơi các iterable đi qua. Phép so sánh được thực hiện dựa trên kết quả trả về sau khi đi qua key function.</li>
	<li><code>default</code>: Tùy chọn. Giá trị mặc định khi iterable trống.</li>
</ul>

<p>2.&nbsp;<code>max(item1, item2, *item&#91;, key&#93;)</code></p>

<ul>
	<li><code>item1</code>,&nbsp;<code>item2</code>: Bắt buộc. Đối tượng để so sánh, có thể là number, string...</li>
	<li><code>*item</code>: Tùy chọn. Các đối tượng khác để so sánh.<br />
	→ Cần ít nhất hai đối tượng để thực hiện so sánh với hàm max().</li>
	<li><code>key</code>: Tùy chọn. Key function, nơi các item đi qua. Phép so sánh được thực hiện trên kết quả trả về sau khi đi qua key function.</li>
</ul>

<h3>Giá trị trả về từ max()</h3>

<p>Hàm max trả về kết quả khác nhau tương ứng với hai loại như trên:</p>

<p>1.&nbsp;<code>max(iterable, *iterables&#91;, key, default&#93;)</code></p>

<table border="1">
		<tr>
			<td><strong>Trường hợp</strong></td>
			<td><strong>Key</strong></td>
			<td><strong>Default</strong></td>
			<td><strong>Giá trị trả về</strong></td>
		</tr>
	<tbody>
		<tr>
			<td>Iterable trống</td>
			<td>Có hoặc Không</td>
			<td>Không có</td>
			<td>Sinh ra ngoại lệ&nbsp;<strong>ValueError</strong></td>
		</tr>
		<tr>
			<td>Iterable trống</td>
			<td>Có</td>
			<td>Có</td>
			<td>Trả về giá trị Default</td>
		</tr>
		<tr>
			<td>Một iterable (không trống)</td>
			<td>Không</td>
			<td>Có hoặc Không</td>
			<td>Trả về số lớn nhất trong iterable</td>
		</tr>
		<tr>
			<td>Một iterable (không trống)</td>
			<td>Có</td>
			<td>Có hoặc Không</td>
			<td>Truyền từng phần tử trong iterable cho hàm key, kết quả trả về là phần tử lớn nhất dựa trên giá trị trả về từ hàm key</td>
		</tr>
		<tr>
			<td>Nhiều iterable (không trống)</td>
			<td>Không</td>
			<td>Có hoặc Không</td>
			<td>Trả về iterable lớn nhất</td>
		</tr>
		<tr>
			<td>Nhiều iterable (không trống)</td>
			<td>Có</td>
			<td>Có hoặc Không</td>
			<td>Truyền từng iterable cho hàm key. Kết quả trả về là iterable lớn nhất dựa trên giá trị trả về từ hàm key</td>
		</tr>
	</tbody>
</table>
&nbsp;

<p>2.&nbsp;<code>max(item1, item2, *item&#91;, key&#93;)</code></p>

<table border="1">
		<tr>
			<td><strong>Trường hợp</strong></td>
			<td><strong>Key</strong></td>
			<td><strong>Giá trị trả về</strong></td>
		</tr>
	<tbody>
		<tr>
			<td>2 item</td>
			<td>Không</td>
			<td>Trả về tham số lớn hơn</td>
		</tr>
		<tr>
			<td>2 item</td>
			<td>Có</td>
			<td>Truyền từng tham số cho hàm key, kết quả trả về là phần tử lớn hơn dựa trên giá trị trả về từ hàm key</td>
		</tr>
		<tr>
			<td>Nhiều item</td>
			<td>Không</td>
			<td>Trả về tham số lớn nhất</td>
		</tr>
		<tr>
			<td>Nhiều item</td>
			<td>Có</td>
			<td>Truyền từng tham số cho hàm key, kết quả trả về là phần tử lớn nhất dựa trên giá trị trả về từ hàm key</td>
		</tr>
	</tbody>
</table>

<h3>Ví dụ 1: Tìm phần tử lớn nhất trong các số truyền vào</h3>

<pre id="pre2">
# su dung max(item1, item2, *item)
print(&#039;So lon nhat la:&#039;, max(1, 3, 2, 5, 4))

# su dung max(iterable)
num = &#91;1, 3, 2, 8, 5, 10, 6&#93;
print(&#039;So lon nhat la:&#039;, max(num))</pre>

<p>Chạy chương trình, kết quả trả về là:</p>

<pre id="pre3">
So lon nhat la: 5
So lon nhat la: 10</pre>

<h3>Ví dụ 2: Tìm số có tổng các chữ số lớn nhất bằng cách sử dụng key function</h3>

<pre id="pre4">
def sumDigit(num):
    sum = 0
    while(num):
        sum += num % 10
        num = int(num / 10)
    return sum

# su dung max(item1, item2, *item, key)
print(&#039;Ket qua lon nhat la:&#039;, max(100, 321, 267, 59, 40, key=sumDigit))

# su dung max(iterable, key)
num = &#91;15, 300, 2700, 821, 52, 10, 6&#93;
print(&#039;Ket qua lon nhat la:&#039;, max(num, key=sumDigit))</pre>

<p>Output trả về là:</p>

<pre id="pre5">
Ket qua lon nhat la: 267
Ket qua lon nhat la: 821</pre>

<p>Ở ví dụ này, các tham số hoặc từng phần tử trong tham số iterable được truyền lần lượt vào&nbsp;<code>sumDigit()</code>&nbsp;để lấy kết quả là số có tổng các chữ số lớn nhất.</p>

<h3>Ví dụ 3: Tìm list có độ dài lớn nhất bằng cách sử dụng key function</h3>

<pre id="pre6">
num = &#91;15, 300, 2700, 821&#93;
num1 = &#91;12, 2&#93;
num2 = &#91;34, 567, 78&#93;

# su dung max(iterable, *iterables, key)
print(&#039;List dai nhat la:&#039;, max(num, num1, num2, key=len))</pre>

<p>Kết quả trả về:</p>

<pre id="pre7">
List dai nhat la: &#91;15, 300, 2700, 821&#93;</pre>

<p>Trong chương trình trên, các iterable num, num1 và num2 được truyền vào key function là&nbsp;hàm len()&nbsp;tích hợp sẵn trong Python. Kết quả trả về là độ dài từng iterable và hàm max sẽ đưa ra iterable có độ dài lớn nhất.</p>

<h3>Ví dụ 4: Hàm max() với các đối số đối tượng</h3>

<p>Lập trình viên có thể dùng cú pháp sau để tìm đối tượng lớn nhất nằm giữa nhiều hơn 1 tham số.</p>

<p>Cú pháp:</p>

<pre id="pre8">
max(arg1, arg2, *args, key)</pre>

<p>Tham số:</p>

<ul>
	<li><strong>Arg1</strong>: Tham số này là một đối tượng. Ví dụ, số, chuỗi và nhiều hơn thế nữa.</li>
	<li><strong>Arg2</strong>: Tham số này cũng là một đối tượng. Ví dụ, số, chuỗi và nhiều hơn thế nữa.</li>
	<li><strong>*args&nbsp;</strong>(tùy chọn): Đây là tham số tùy chọn cho nhiều đối tượng hơn.</li>
	<li><strong>Key</strong>&nbsp;(tùy chọn): Tham số này cũng là tùy chọn. Nó hoạt động khi từng tham số được chuyển và so sánh được thực hiện trên những kiến thức cơ bản của giá trị trả về.</li>
</ul>

<p>Vì thế, hàm max() giúp chúng ta tìm thấy phần tử lớn nhất nằm giữa nhiều đối tượng. Giờ hãy cùng nhau xem xét một ví dụ để tìm số lớn nhất nằm giữa các số được cung cấp.</p>

<p>Ví dụ:</p>

<pre id="pre9">
# dùng hàm max() với các đối tượng số
large_num = max(10, -4, 5, -3, 13)

# in kết quả
print(&quot;The largest number:&quot;, large_num)</pre>

<p>Kết quả:</p>

<pre id="pre10">
The largest number: 13</pre>

<p><strong>Giải thích</strong></p>

<p>Ở đoạn code trên, bạn đã dùng hàm&nbsp;<strong>max()&nbsp;</strong>để tìm phần tử lớn nhất nằm trong số các đối tượng chỉ định làm tham số và đã in kết quả cho người dùng.</p>

<p><strong>Tóm lại:</strong></p>

<ul>
	<li>Hàm max() trong Python thường được dùng để trả về nhân tố lớn nhất hay cao nhất. Phụ thuộc vào tham số, nó có hai cú pháp, không iterable và có iterable.</li>
	<li>Trong trường hợp không có iterable, các phần tử trực tiếp được truyền đi trong tham số. Ngược lại, nếu có iterable, phần tử iterable đó có thể được chuyển trực tiếp vào tham số.</li>
	<li>Nếu iterabale trống và giá trị mặc định không được cung cấp, thì hàm max sẽ trả về ValueError.</li>
	<li>Nếu phần tử trong các kiểu dữ liệu khác nhau được chuyển tới hàm max, nó sẽ phát sinh TypeError.</li>
</ul>
&nbsp;
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-max-trong-python-10387.html" title="Hàm max&#40;&#41; trong Python">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-max-trong-python-10387.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>