{"id":773,"date":"2020-10-26T03:33:30","date_gmt":"2020-10-26T03:33:30","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=773"},"modified":"2025-03-27T13:22:35","modified_gmt":"2025-03-27T13:22:35","slug":"dynamic-typing-in-python","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/dynamic-typing-in-python\/","title":{"rendered":"Dynamic Typing in Python"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about dynamic typing in Python and how it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-dynamic-typing-in-python'>Introduction to dynamic typing in Python <a href=\"#introduction-to-dynamic-typing-in-python\" class=\"anchor\" id=\"introduction-to-dynamic-typing-in-python\" title=\"Anchor for Introduction to dynamic typing in Python\">#<\/a><\/h2>\n\n\n\n<p>In some programming languages such as Java or C#, when declaring a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\">variable<\/a>, you need to specify a data type for it.<\/p>\n\n\n\n<p>For example, in Java, you can <a href=\"https:\/\/www.javazerotomastery.com\/java-tutorial\/java-variables\/\" target=\"_blank\" rel=\"noreferrer noopener\">declare a variable<\/a> with the type <code>String<\/code> and initializes its value as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">String<\/span> message = <span class=\"hljs-string\">\"Hello\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Behind the scenes, Java creates a new <code>String<\/code> object whose value is <code>\"Hello\"<\/code>. It also creates a variable called <code>message<\/code> with type <code>String<\/code> and references the <code>message<\/code> variable to the <code>String<\/code> object.<\/p>\n\n\n\n<p>In statically typed languages, the data types are associated with variables.<\/p>\n\n\n\n<p>Later, if you assign an integer to the <code>message<\/code> variable, it&#8217;s not going to work. The reason is that the <code>message<\/code> variable is already associated with the String type, not the integer type.<\/p>\n\n\n\n<p>Unlike statically typed languages, Python is a dynamically typed language. When declaring a variable in Python, you don&#8217;t specify a type for it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">message = <span class=\"hljs-string\">'Hello'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In Python, the <code>message<\/code> variable is just a reference to an object which is a string. There is no type associated with the <code>message<\/code> variable.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"418\" height=\"125\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Dynamic-Typing-in-Python-example.png\" alt=\"\" class=\"wp-image-776\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Dynamic-Typing-in-Python-example.png 418w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Dynamic-Typing-in-Python-example-300x90.png 300w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/figure>\n<\/div>\n\n\n<p>If you assign a number to the <code>message<\/code> variable, it&#8217;s perfectly fine:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">message = 100<\/code><\/span><\/pre>\n\n\n<p>In this case, Python creates a new integer object, and the <code>message<\/code> references to the new integer object:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"239\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Dynamic-Typing-in-Python-example-reassignment.png\" alt=\"\" class=\"wp-image-777\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Dynamic-Typing-in-Python-example-reassignment.png 404w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Dynamic-Typing-in-Python-example-reassignment-300x177.png 300w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/figure>\n<\/div>\n\n\n<p>To determine the type of object that a variable currently references, you use the <code>type()<\/code> function.<\/p>\n\n\n\n<p>The following example defines a variable named <code>message<\/code> and assigned it a string <code>'Hello'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">message = <span class=\"hljs-string\">'Hello'<\/span>\n<span class=\"hljs-keyword\">print<\/span>(type(message))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bWVzc2FnZSA9ICdIZWxsbycKcHJpbnQodHlwZShtZXNzYWdlKSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">str<\/span>'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you assign a number to the <code>message<\/code> variable, type of the object that the <code>message<\/code> variable references by also changes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">message = <span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-keyword\">print<\/span>(type(message))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bWVzc2FnZSA9IDEwMApwcmludCh0eXBlKG1lc3NhZ2UpKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">int<\/span>'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python is a dynamically typed language.<\/li>\n\n\n\n<li>In Python, variables don&#8217;t associate with any particular types.<\/li>\n\n\n\n<li>Use the <code>type()<\/code> function to get the type of the objects that variables reference.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"773\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/dynamic-typing-in-python\/\"\n\t\t\t\tdata-post-title=\"Dynamic Typing in Python\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"773\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/dynamic-typing-in-python\/\"\n\t\t\t\tdata-post-title=\"Dynamic Typing in Python\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the dynamic typing in Python and how it works.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-773","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/773\/revisions"}],"predecessor-version":[{"id":7103,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/773\/revisions\/7103"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}