{"id":6953,"date":"2024-04-11T11:59:59","date_gmt":"2024-04-11T11:59:59","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6953"},"modified":"2024-04-11T11:59:59","modified_gmt":"2024-04-11T11:59:59","slug":"python-literals","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-literals\/","title":{"rendered":"Python Literals"},"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=\"#types-of-literals-in-python\">Types of Literals in Python<\/a><\/li><li ><a href=\"#python-string-literal\">Python String literal<\/a><\/li><li ><a href=\"#python-character-literal\">Python Character Literal<\/a><\/li><li ><a href=\"#python-numeric-literal\">Python Numeric literal <\/a><\/li><li ><a href=\"#integer\">Integer<\/a><\/li><li ><a href=\"#float\">Float<\/a><\/li><li ><a href=\"#complex\">Complex<\/a><\/li><li ><a href=\"#python-boolean-literal\">Python Boolean literal<\/a><\/li><li ><a href=\"#python-literal-collection\">Python literal collection<\/a><\/li><li ><a href=\"#list-literal\">List Literal<\/a><\/li><li ><a href=\"#tuple-literal\">Tuple Literal<\/a><\/li><li ><a href=\"#dictionary-literal\">Dictionary Literal<\/a><\/li><li ><a href=\"#set-literal\">Set Literal<\/a><\/li><li ><a href=\"#python-special-l-iteral\">Python Special LIteral<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-literals-fa-qs\">Python Literals -FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>A Python Literal is a syntax that will denote a value of a particular data type. Literals are constants that will be self-explanatory and don&#8217;t require to be evaluated or computed. Further, they will give values or directly use them in the expressions.<\/p>\n\n\n\n<p>However, literals are a system of symbols that have a fixed value in the<a href=\"https:\/\/www.skillvertex.com\/blog\/data-science-projects-with-source-code\/\" data-type=\"post\" data-id=\"5609\"> source code<\/a>. It is also known as the raw values or the data given in variables or constants. Read this article to learn about the different types of literals in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-literals-in-python\">Types of Literals in Python<\/h2>\n\n\n\n<p>Python has several types of <a href=\"https:\/\/www.skillvertex.com\/blog\/literals-in-c-c-with-examples\/\" data-type=\"post\" data-id=\"1972\">literals <\/a>like numeric literals, string literals, and boolean literals. The different types of literals in<a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\"> Python <\/a>with examples are provided below:<\/p>\n\n\n\n<p>a. <a href=\"https:\/\/www.skillvertex.com\/blog\/what-does-0-mean-in-this-python-string\/\" data-type=\"post\" data-id=\"3237\">String<\/a> literals<\/p>\n\n\n\n<p>b. <a href=\"https:\/\/www.skillvertex.com\/blog\/character-arithmetic-in-c-and-c\/\" data-type=\"post\" data-id=\"1876\">Character <\/a>literals<\/p>\n\n\n\n<p>c. Numeric Literals&#8217;<\/p>\n\n\n\n<p>d. Boolean literals<\/p>\n\n\n\n<p>e. <a href=\"https:\/\/www.skillvertex.com\/blog\/literals-in-c-c-with-examples\/\" data-type=\"post\" data-id=\"1972\">literal <\/a>collection<\/p>\n\n\n\n<p>f. Special literal<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-string-literal\">Python String literal<\/h2>\n\n\n\n<p>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> is considered a literal and will be made by writing a text that is surrounded by single (&#8220;), double (&#8220;), and triple quotes. Multi-line strings will be displayed with the triple quotes. The example given below illustrates the Python String literal<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># in single quote\ns = 'skillvertex'\n \n# in double quotes\nt = \"skillvertex\"\n \n# multi-line String\nm = '''skill\n           vertex\n               blog'''\n \nprint(s)\nprint(t)\nprint(m)\n\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>skillvertex\nskillvertex\nskill       \n                 vertex\n                             blog<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-character-literal\">Python Character Literal<\/h2>\n\n\n\n<p>Python character literal is referred to as a type of string literal which has a single <a href=\"https:\/\/www.skillvertex.com\/blog\/character-arithmetic-in-c-and-c\/\" data-type=\"post\" data-id=\"1876\">character<\/a> and is surrounded by single or double quotes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># character literal in single quote\nv = 'n'\n \n# character literal in double quotes\nw = \"a\"\n \nprint(v)\nprint(w)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n\na<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-numeric-literal\">Python Numeric literal <\/h2>\n\n\n\n<p>The three types of Python numeric literal are provided below<\/p>\n\n\n\n<p>a. Integer<\/p>\n\n\n\n<p>b. Float<\/p>\n\n\n\n<p>c. complex<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"integer\">Integer<\/h2>\n\n\n\n<p>Integers are those numbers that consist of both positive and negative numbers which include 0. In the example below, we have provided integer literals (0b10100, 50, 0o320, 0x12b)into different variables.<\/p>\n\n\n\n<p>&#8216;a&#8217; is considered as the binary literal &#8216;b&#8217; is the decimal literal, &#8216;c&#8217;  is referred to as the octal literal, and &#8216;d&#8217; is the hexadecimal literal. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># integer literal\n \n# Binary Literals\na = 0b10100\n \n# Decimal Literal\nb = 50\n \n# Octal Literal\nc = 0o320\n \n# Hexadecimal Literal\nd = 0x12b\n \nprint(a, b, c, d)<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/input-output-system-calls-in-c-create-open-close-read-write\/\" data-type=\"post\" data-id=\"3685\">Output<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>20 50 208 299\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"float\">Float<\/h2>\n\n\n\n<p>Float are those real numbers that have both integer and fractional parts. For example, 24.8 and 45.0 are the floating-point literals as 24.8 and 45.0 are the floating-point numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Float Literal\ne = 24.8\nf = 45.0\n \nprint(e, f)<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/what-will-be-the-output-of-the-following-program\/\" data-type=\"post\" data-id=\"3081\">Output<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>24.8 45.0\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"complex\">Complex<\/h2>\n\n\n\n<p>These numerals are in the form of a+bj. Whereas, a is considered as the real part and b is the complex part.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>z = 7 + 5j\n \n# real part is 0 here.\nk = 7j\n \nprint(z, k)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(7+5j) 7j\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-boolean-literal\">Python Boolean literal<\/h2>\n\n\n\n<p>There are two boolean literals in Python. Those are true and false. True will denote the value as 1 and false will give the value as 0. In the example provided below, a is true and b is false as 1 is equal to true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = (1 == True)\nb = (1 == False)\nc = True + 3\nd = False + 7\n \nprint(\"a is\", a)\nprint(\"b is\", b)\nprint(\"c:\", c)\nprint(\"d:\", d)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a is True\nb is False\nc: 4\nd: 7<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-literal-collection\">Python literal collection<\/h2>\n\n\n\n<p>Python has four different types of literal collection<\/p>\n\n\n\n<p>a, list literal<\/p>\n\n\n\n<p>b. tuple literal<\/p>\n\n\n\n<p>c.Dict Literal<\/p>\n\n\n\n<p>d. Set Literal<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"list-literal\">List Literal<\/h2>\n\n\n\n<p>List literal has items of different<a href=\"https:\/\/www.skillvertex.com\/blog\/what-are-the-data-types-for-which-it-is-not-possible-to-create-an-array\/\" data-type=\"post\" data-id=\"2632\"> data types<\/a>. The values that are stored in the lists will be separated by a comma and will be enclosed within the square brackets. Different types of data can be stored in the list. These lists are mutable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number = &#91;1, 2, 3, 4, 5]\nname = &#91;'Amit', 'kabir', 'bhaskar', 2]\nprint(number)\nprint(name)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 2, 3, 4, 5]\n&#91;'Amit', 'kabir', 'bhaskar', 2]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tuple-literal\">Tuple Literal<\/h2>\n\n\n\n<p>Tuple is referred to as the collection of different data types. It will be enclosed by the parentheses  &#8220;()&#8221; and every element will be separated with a comma and is immutable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>even_number = (2, 4, 6, 8)\nodd_number = (1, 3, 5, 7)\n \nprint(even_number)\nprint(odd_number)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(2, 4, 6, 8)\n(1, 3, 5, 7)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dictionary-literal\">Dictionary Literal<\/h2>\n\n\n\n<p>This dictionary will store the data in the key-value pair. It will be enclosed by the curly braces &#8216;{}&#8217;  and every pair will be separated by the comma. Different types of data can be stored in the <a href=\"https:\/\/www.skillvertex.com\/blog\/dictionary-is-mutable-or-immutable\/\" data-type=\"post\" data-id=\"1615\">dictionary.<\/a> These dictionaries are mutable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alphabets = {'a': 'apple', 'b': 'ball', 'c': 'cat'}\ninformation = {'name': 'amit', 'age': 20, 'ID': 20}\n \nprint(alphabets)\nprint(information)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'a': 'apple', 'b': 'ball', 'c': 'cat'}\n{'name': 'amit', 'age': 20, 'ID': 20}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"set-literal\">Set Literal<\/h2>\n\n\n\n<p>Set literal is referred to as the collection of the unordered data set. It will be enclosed by the {} and every element will be separated by the comma.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vowels = {'a', 'e', 'i', 'o', 'u'}\nfruits = {\"apple\", \"banana\", \"cherry\"}\n \nprint(vowels)\nprint(fruits)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'o', 'e', 'a', 'u', 'i'}\n{'apple', 'banana', 'cherry'}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-special-l-iteral\">Python Special LIteral<\/h2>\n\n\n\n<p>Python has one special literal which is None. <strong>None<\/strong> will define a null variable<strong>. <\/strong>However, if the<strong> None <\/strong>will be compared with anything else other than <strong>None<\/strong>. There is a possibility to get the result as false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>water_remain = None\nprint(water_remain)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>None<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, beginners can learn about Python string literals, Python character literal, and Python numeric literal. The three types of numeric literal are also included in this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-literals-fa-qs\">Python Literals -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-1707300515181\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How many literals are there in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. There are five types<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707300528381\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the difference between a variable and a literal in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Literals are the raw values that will be stored in a  variable or constant. Whereas, constants are immutable.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707300536152\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What&#8217;s literal programming?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Literals are referred to as the constant values that are given to constant variables.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>A Python Literal is a syntax that will denote a value of a particular data type. Literals are constants that will be self-explanatory and don&#8217;t require to be evaluated or computed. Further, they will give values or directly use them in the expressions. However, literals are a system of symbols that have a fixed value &#8230; <a title=\"Python Literals\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-literals\/\" aria-label=\"More on Python Literals\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6954,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[72,57,888],"class_list":["post-6953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-programming","tag-python","tag-python-literals","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\/6953","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=6953"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6953\/revisions"}],"predecessor-version":[{"id":8899,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6953\/revisions\/8899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6954"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}