{"id":13,"date":"2024-01-24T09:10:21","date_gmt":"2024-01-24T09:10:21","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=13"},"modified":"2024-01-24T09:10:22","modified_gmt":"2024-01-24T09:10:22","slug":"python-variables","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-variables\/","title":{"rendered":"Python Variables"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python variables and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a variable in Python<\/h2>\n\n\n\n<p>When you develop a program, you need to manage values, a lot of them. To store values, you use variables.<\/p>\n\n\n\n<p>In Python, a variable is a label that you can assign a value to it. And a variable is always associated with a value. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>message = 'Hello, World!' print(message) message = 'Good Bye!' print(message)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hello, World! Good Bye!<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example,&nbsp;<code>message<\/code>&nbsp;is a variable. It holds the string&nbsp;<code>'Hello, World!'<\/code>. The&nbsp;<code>print()<\/code>&nbsp;function shows the message&nbsp;<code>Hello, World!<\/code>&nbsp;to the screen.<\/p>\n\n\n\n<p>The next line assigns the string&nbsp;<code>'Good Bye!'<\/code>&nbsp;to the&nbsp;<code>message<\/code>&nbsp;variable and print its value to the screen.<\/p>\n\n\n\n<p>The variable&nbsp;<code>message<\/code>&nbsp;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\">Creating variables<\/h2>\n\n\n\n<p>To define a variable, you use the following syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>variable_name = value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>=<\/code>&nbsp;is the assignment operator. In this syntax, you assign a value to the&nbsp;<code>variable_name<\/code>.<\/p>\n\n\n\n<p>The value can be anything like a&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-numbers\/\">number<\/a>, a&nbsp;<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&nbsp;<code>counter<\/code>&nbsp;and assigns the number 1 to it:<code>counter = 1<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Naming variables<\/h2>\n\n\n\n<p>When you name a variable, you need to adhere to some rules. If you don\u2019t, you\u2019ll 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&nbsp;<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&nbsp;<code>active_user<\/code>&nbsp;variable is more descriptive than the&nbsp;<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&nbsp;<code>l<\/code>&nbsp;and the uppercase letter&nbsp;<code>O<\/code>&nbsp;because they look like the number&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>0<\/code>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll 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 values, a lot of them. To store values, you use variables. In Python, a variable is a label that you can assign a value to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-1-fundamentals"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/13\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}