{"id":77,"date":"2020-10-03T01:14:30","date_gmt":"2020-10-03T01:14:30","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=77"},"modified":"2025-03-26T03:42:18","modified_gmt":"2025-03-26T03:42:18","slug":"python-variables","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/","title":{"rendered":"Python Variables"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python variables and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-a-variable-in-python'>What is a variable in Python? <a href=\"#what-is-a-variable-in-python\" class=\"anchor\" id=\"what-is-a-variable-in-python\" title=\"Anchor for What is a variable in Python?\">#<\/a><\/h2>\n\n\n\n<p>When you develop a program, you need to manage many values. To store these values, you use variables.<\/p>\n\n\n\n<p>In Python, a variable is a label you can assign a value to. A variable is always associated with a value. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">message = <span class=\"hljs-string\">'Hello, World!'<\/span>\nprint(message)\n\nmessage = <span class=\"hljs-string\">'Good Bye!'<\/span>\nprint(message)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bWVzc2FnZSA9ICdIZWxsbywgV29ybGQhJwpwcmludChtZXNzYWdlKQoKbWVzc2FnZSA9ICdHb29kIEJ5ZSEnCnByaW50KG1lc3NhZ2Up\" 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-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Hello, World!\nGood Bye!<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, <code>message<\/code> is a variable. It holds the string <code>'Hello, World!'<\/code>. The <code>print()<\/code> function shows the message <code>Hello, World!<\/code> to the screen.<\/p>\n\n\n\n<p>The next line assigns the string <code>'Good Bye!'<\/code> to the <code>message<\/code> variable and print its value to the screen.<\/p>\n\n\n\n<p>The variable <code>message<\/code> can hold various values at different times. And its value can change throughout the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-variables'>Creating variables <a href=\"#creating-variables\" class=\"anchor\" id=\"creating-variables\" title=\"Anchor for Creating variables\">#<\/a><\/h2>\n\n\n\n<p>To define a variable, you use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">variable_name = value<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>=<\/code> is the assignment operator. In this syntax, you assign a value to the <code>variable_name<\/code>.<\/p>\n\n\n\n<p>The value can be anything like a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-numbers\/\">number<\/a>, a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">string<\/a>, etc., that you assign to the variable.<\/p>\n\n\n\n<p>The following defines a variable named <code>counter<\/code> and assigns the number 1 to it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">counter = <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='naming-variables'>Naming variables <a href=\"#naming-variables\" class=\"anchor\" id=\"naming-variables\" title=\"Anchor for Naming variables\">#<\/a><\/h2>\n\n\n\n<p>When you name a variable, you need to adhere to some rules. If you don&#8217;t, you&#8217;ll get an error.<\/p>\n\n\n\n<p>The following are the variable rules that you should keep in mind:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Variable names can contain only letters, numbers, and underscores (<code>_<\/code>). They can start with a letter or an underscore (<code>_<\/code>), not with a number.<\/li>\n\n\n\n<li>Variable names cannot contain spaces. To separate words in variables, you use underscores, for example <code>sorted_list<\/code>.<\/li>\n\n\n\n<li>Variable names cannot be the same as keywords, reserved words, and built-in functions in Python.<\/li>\n<\/ul>\n\n\n\n<p>The following guidelines help you define good variable names:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Variable names should be concise and descriptive. For example, the <code>active_user<\/code> variable is more descriptive than the <code>au<\/code>.<\/li>\n\n\n\n<li>Use underscores (_) to separate multiple words in the variable names.<\/li>\n\n\n\n<li>Avoid using the letter <code>l<\/code> and the uppercase letter <code>O<\/code> because they look like the number <code>1<\/code> and <code>0<\/code>.<\/li>\n<\/ul>\n\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>A variable is a label that you can assign a value to it. The value of a variable can change throughout the program.<\/li>\n\n\n\n<li>Use the <code>variable_name = value<\/code> to create a variable.<\/li>\n\n\n\n<li>The variable names should be as concise and descriptive as possible. Also, they should adhere to Python variable naming rules.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=variables\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\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=\"77\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\"\n\t\t\t\tdata-post-title=\"Python Variables\"\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=\"77\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\"\n\t\t\t\tdata-post-title=\"Python Variables\"\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>Summary: in this tutorial, you&#8217;ll learn about Python variables and how to use them effectively. What is a variable in Python? # When you develop a program, you need to manage many values. To store these values, you use variables. In Python, a variable is a label you can assign a value to. A variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-77","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/77","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=77"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/77\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}