{"id":6932,"date":"2024-04-11T11:59:01","date_gmt":"2024-04-11T11:59:01","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6932"},"modified":"2024-04-11T11:59:01","modified_gmt":"2024-04-11T11:59:01","slug":"python-variables","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-variables\/","title":{"rendered":"Python Variables"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#example-of-variable-in-python\">Example Of Variable In Python<\/a><\/li><li ><a href=\"#what-are-the-rules-for-python-variables\">What are the Rules For Python Variables?<\/a><\/li><li ><a href=\"#what-is-the-variable-assignment-in-python\">What is the Variable Assignment In Python?<\/a><\/li><li ><a href=\"#what-is-the-declaration-and-initialization-of-variable\">What is the Declaration and Initialization Of Variable?<\/a><\/li><li ><a href=\"#what-is-redeclaring-variables-in-python\">What is Redeclaring Variables In Python?<\/a><\/li><li ><a href=\"#python-assigns-values-to-multiple-variables\">Python Assigns Values to Multiple Variables\u00a0<\/a><\/li><li ><a href=\"#assigning-different-values-to-multiple-variables\">Assigning different values to multiple variables<\/a><\/li><li ><a href=\"#using-the-same-name-for-different-types\">Using the Same Name For Different Types<\/a><\/li><li ><a href=\"#how-does-operator-work-with-variables\">How does +operator work with variables?<\/a><\/li><li ><a href=\"#using-operator-for-different-datatypes\">Using + Operator for Different Datatypes<\/a><\/li><li ><a href=\"#output\">Output<\/a><\/li><li ><a href=\"#what-is-global-and-local-python-variables\">What is Global and Local Python Variables?<\/a><\/li><li ><a href=\"#what-is-global-keyword-in-python\">What is Global Keyword in Python?<\/a><\/li><li ><a href=\"#what-is-variable-type-in-python\">What is Variable Type in Python?<\/a><\/li><li ><a href=\"#creating-objects\">Creating Objects<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-variables-fa-qs\">Python Variables -FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Python variable is a container that will store value. Python is not referred to as statically typed. There is no need to declare the variables before using them or declare their type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, a variable is formed after we provide a value to it. A Python variable is a name that has been given to a memory location. It is considered the fundamental unit of storage in the program. Let us check out this article to define a variable in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-variable-in-python\">Example Of Variable In Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After an object is given a <a href=\"https:\/\/www.skillvertex.com\/blog\/static-variables-in-c\/\" data-type=\"post\" data-id=\"3029\">variable<\/a>. It will known by that name. According to Layman&#8217;s terms, variables in Python have store values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example given below, Skill Vertex is stored as the variable <strong>var<\/strong>. Thus, the stored information will be printed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Var = \"Skillvertex\"\nprint(Var)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skillvertex<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep in mind, that the stored value can be altered during the program execution.  The Variables in Python are just a name that is assigned to a memory location. Hence, every operation that is being done on a variable will affect its<a href=\"https:\/\/www.skillvertex.com\/blog\/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc\/\" data-type=\"post\" data-id=\"3096\"> memory <\/a>location.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-rules-for-python-variables\">What are the Rules For Python Variables?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Python Variables name will start with a letter or an underscore character.<\/li>\n\n\n\n<li>A Python variable cannot begin with a number.<\/li>\n\n\n\n<li>It has alpha-numeric characters and underscores such as (A-z, 0-9, and _ ).<\/li>\n\n\n\n<li>It&#8217;s case-sensitive  (name, Name, and Name are the three different variables.<\/li>\n\n\n\n<li>The reserved words in Python cannot be included as the variable name in Python.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># valid variable name\nskill = 1\nSkill= 2\nSk_k_il = 5\n_skill = 6\nskill_ = 7\n_SKILL_ = 8\n \nprint(skill, skill, Sk_k_il )\nprint(_skill, skill_, _SKILL_)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 5\n6 7 8<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-variable-assignment-in-python\">What is the Variable Assignment In Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Defining variables in Python by providing a number, floating-point number, and a <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-reverse-a-string-in-java\/\" data-type=\"post\" data-id=\"728\">string<\/a> to the variable like age, salary, and a name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># An integer assignment\nage = 45\n \n# A floating point\nsalary = 1456.8\n \n# A string\nname = \"John\"\n \nprint(age)\nprint(salary)\nprint(name)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>45\n1456.8\nJohn<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-declaration-and-initialization-of-variable\">What is the Declaration and Initialization Of Variable?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Refer to the example below to declare the variable, define the variable, and print the variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># declaring the var\nNumber = 100\n \n# display\nprint( Number)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>100<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-redeclaring-variables-in-python\">What is Redeclaring Variables In Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"> Re-declare the variable even after declaring the variable and then define it in Python already.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># declaring the var\nNumber = 100\n \n# display\nprint(\"Before declare: \", Number)\n \n# re-declare the var\nNumber = 120.3\n   \nprint(\"After re-declare:\", Number)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before declare:  100\nAfter re-declare: 120.3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-assigns-values-to-multiple-variables\"><strong>Python Assigns Values to Multiple Variables&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python will provide a value to several variables simultaneously with the  &nbsp;\u201c=\u201d <a href=\"https:\/\/www.skillvertex.com\/blog\/and-operators-in-c\/\" data-type=\"post\" data-id=\"3294\">operators<\/a>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = b = c = 10\n \nprint(a)\nprint(b)\nprint(c)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n10\n10<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"assigning-different-values-to-multiple-variables\">Assigning different values to multiple variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python will add different values in a single line with the help of the \u201c,\u201d operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b, c = 1, 20.2, \"SkillVertex\"\n \nprint(a)\nprint(b)\nprint(c)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1,\n 20.2\n\"Skillvertex\"\n \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-the-same-name-for-different-types\">Using the Same Name For Different Types<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python program will allow to use of the same name and can refer to the new value and type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10\na = \"Skillvertex\"\n \nprint(a)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-does-operator-work-with-variables\">How does +operator work with variables?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python Plus will give a way to add a value if it&#8217;s a number and concatenate if it&#8217;s a <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-find-length-of-string-in-java-python-cpp-javascript-sql-shell-script-mysql-oracle-and-perl\/\" data-type=\"post\" data-id=\"452\">string<\/a>. If the variable is already formed and has a new value in the same variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10\nb = 20\nprint(a+b)\n \na = \"Skill\"\nb = \"Vertex\"\nprint(a+b)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>30\nSkillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-operator-for-different-datatypes\">Using + Operator for Different Datatypes<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10\nb = \"Geeks\"\nprint(a+b)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"output\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>TypeError: unsupported operand type(s) for +: 'int' and 'str'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-global-and-local-python-variables\">What is Global and Local Python Variables?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Local Variables in Python can be defined and declared inside the function. This variable cannot be defined outside the<a href=\"https:\/\/www.skillvertex.com\/blog\/variadic-functions-in-c\/\" data-type=\"post\" data-id=\"2561\"> function<\/a>.&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This function uses global variable s\ndef f():\n    s = \"Welcome skillvertex\"\n    print(s)\n \n \nf()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome skillvertex<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Global variables <\/strong>in Python are the ones that will defined and declared outside the function and will be used inside the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This function has a variable with\n# name same as s\ndef f():\n    print(s)\n \n# Global scope\ns = \"I love Skillvertex\"\nf()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>I love Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-global-keyword-in-python\">What is Global Keyword in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python global is a keyword that will allow the user to change the variable outside of the scope. Hence, it is used to make a global variable from the non-global scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the global variable will be used inside the function only when it is required to do an assignment or to change the variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Rules of global keyword<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Local and Global Variables:<\/strong>\n<ul class=\"wp-block-list\">\n<li>When you create a variable inside a function, it&#8217;s like a secret code that only that function understands. We call this a &#8220;local&#8221; variable.<\/li>\n\n\n\n<li>If you create a variable outside of any function, it&#8217;s like a universal code that everyone can understand. We call this a &#8220;global&#8221; variable.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Implicitly Global Variables:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If you only talk about (reference) a global variable inside a function, Python understands that you&#8217;re talking about the big universal code. You don&#8217;t need to declare it as global unless you want to change it.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Using <code>global<\/code> Keyword:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If you want to change the big universal code (global variable) inside a function, you need to tell Python explicitly by using the word &#8220;global.&#8221;<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 15\n \ndef change():\n \n    # using a global keyword\n    global x\n \n    # increment value of a by 5\n    x = x + 5\n    print(\"Value of x inside a function :\", x)\n \n \nchange()\nprint(\"Value of x outside a function :\", x)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Value of x inside a function : 20\nValue of x outside a function : 20<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-variable-type-in-python\">What is Variable Type in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Data types have classification or categorization of data items. Data types will show the value that tells what operations can be done on a particular data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Since everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Build-in Python Data types:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a.Numeric<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b.Text Type<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c. Sequence Type<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">d. Boolean<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">d. Set <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">e. Dictionary<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># numberic\nvar = 123\nprint(\"Numeric data : \", var)\n \n# Sequence Type\nString1 = 'Welcome to the Skillvertex'\nprint(\"String with the use of Single Quotes: \")\nprint(String1)\n \n# Boolean\nprint(type(True))\nprint(type(False))\n \n# Creating a Set with\n# the use of a String\nset1 = set(\"Skillvertex\")\nprint(\"\\nSet with the use of String: \")\nprint(set1)\n \n# Creating a Dictionary\n# with Integer Keys\nDict = {1: 'Skill', 2: 'For', 3: 'Vertex'}\nprint(\"\\nDictionary with the use of Integer Keys: \")\nprint(Dict)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Numeric data :  123\nString with the use of Single Quotes: \nWelcome to the Skill vertex\n&lt;class 'bool'&gt;\n&lt;class 'bool'&gt;\nSet with the use of String: \n{'v', 'S', 'K', 'I', 'E', 'L', 'R'}\nDictionary with the use of Integer Keys: \n{1: skill', 2: 'For', 3: 'skill'}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-objects\">Creating Objects<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class CSStudent:\n    # Class Variable\n    stream = 'cse'\n    # The init method or constructor\n    def __init__(self, roll):\n        # Instance Variable\n        self.roll = roll\n \n# Objects of CSStudent class\na = CSStudent(101)\nb = CSStudent(102)\n \nprint(a.stream)  # prints \"cse\"\nprint(b.stream)  # prints \"cse\"\nprint(a.roll)    # prints 101\n \n# Class variables can be accessed using class\n# name also\nprint(CSStudent.stream)  # prints \"cse\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cse\ncse\n101\ncse<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To conclude, this article is about Python variables, assigning values to multiple and different variables. A global and local variable in Python is also added to this. Students can understand Python variables easily through this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-variables-fa-qs\">Python Variables -FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1706877454732\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are variables in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A Python variable is\u00a0a reserved memory location to store values.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1706877462939\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What are the 4 variables in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Integer, float, String, and Boolean<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1706877470554\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are called variables?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A variable can be either a characteristic, number, or quantity that can be measured or counted.\u00a0<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python variable is a container that will store value. Python is not referred to as statically typed. There is no need to declare the variables before using them or declare their type. Therefore, a variable is formed after we provide a value to it. A Python variable is a name that has been given to &#8230; <a title=\"Python Variables\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" aria-label=\"More on Python Variables\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6934,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[72,57,881],"class_list":["post-6932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-programming","tag-python","tag-python-variables","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6932"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6932\/revisions"}],"predecessor-version":[{"id":8895,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6932\/revisions\/8895"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6934"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}