{"id":7023,"date":"2024-03-19T06:42:24","date_gmt":"2024-03-19T06:42:24","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7023"},"modified":"2024-03-19T06:42:24","modified_gmt":"2024-03-19T06:42:24","slug":"python-numbers","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-numbers\/","title":{"rendered":"Python Numbers"},"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=\"#what-is-a-numeric-data-type\">What is a Numeric data type?<\/a><\/li><li ><a href=\"#what-is-python-int-type\">What is Python Int Type?<\/a><\/li><li ><a href=\"#what-is-python-float-type\">What is Python Float Type?<\/a><\/li><li ><a href=\"#what-is-python-complex-type\">What is Python Complex Type?<\/a><\/li><li ><a href=\"#what-is-type-conversion-in-python\">What is Type Conversion in Python?<\/a><\/li><li ><a href=\"#random-numbers-in-python\">Random Numbers in Python<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-numbers-fa-qs\">Python Numbers- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.skillvertex.com\/blog\/python-comments\/\" data-type=\"post\" data-id=\"7009\">Python<\/a> consists of built-in support for storing and processing numeric data which is the Python Numbers. It is required to work with the numbers in every Python application. This article has listed the various types of  Python Numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-numeric-data-type\">What is a Numeric data type?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The numeric data type can store numeric values. So, they are considered immutable data types, which refer to the value of several data types that can vary and hence, impact the newly allocated object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Types of Numeric <a href=\"https:\/\/www.skillvertex.com\/blog\/data-types-in-c\/\" data-type=\"post\" data-id=\"1842\">Data Types<\/a>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a.<a href=\"https:\/\/www.skillvertex.com\/blog\/integer-promotions-in-c\/\" data-type=\"post\" data-id=\"1968\"> int<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b. Float<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c.Complex<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-int-type\">What is Python Int Type?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.skillvertex.com\/blog\/python-virtual-environment\/\" data-type=\"post\" data-id=\"6920\">Python<\/a> int denotes a whole number, which will include the negative numbers but it doesn&#8217;t mean the fractions. In Python, it doesn&#8217;t have any limit on the integer value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 1<\/strong>&#8211; Creating int and checking type in Python <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating an integer\nmy_integer = 42\n\n# Checking the type\ntype_of_my_integer = type(my_integer)\n\n# Output\nprint(\"My Integer:\", my_integer)\nprint(\"Type of My Integer:\", type_of_my_integer)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nMy Integer: 42\nType of My Integer: &lt;class 'int'&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example 2: <a href=\"https:\/\/www.skillvertex.com\/blog\/python-arithmetic-operators\/\" data-type=\"post\" data-id=\"6958\">Arithmetic Operation<\/a>s on Int Type In Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n # Creating integer variables\na = 10\nb = 5\n\n# Addition\nsum_result = a + b\n\n# Subtraction\ndifference_result = a - b\n\n# Multiplication\nproduct_result = a * b\n\n# Division\ndivision_result = a \/ b  # Result will be a float in Python 3.x\n\n# Floor Division (result is the quotient without the remainder)\nfloor_division_result = a \/\/ b\n\n# Modulus (result is the remainder after division)\nmodulus_result = a % b\n\n# Exponentiation\nexponentiation_result = a ** b\n\n# Output\nprint(\"a + b =\", sum_result)\nprint(\"a - b =\", difference_result)\nprint(\"a * b =\", product_result)\nprint(\"a \/ b =\", division_result)\nprint(\"a \/\/ b =\", floor_division_result)\nprint(\"a % b =\", modulus_result)\nprint(\"a ** b =\", exponentiation_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a + b = 15\na - b = 5\na * b = 50\na \/ b = 2.0\na \/\/ b = 2\na % b = 0\na ** b = 100000<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-float-type\">What is Python Float Type?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python Float number is referred to as the real number which consists of a floating-point representation.  So, it will be denoted by a decimal number. Hence, this character e or E will be followed by positive or negative integers and will indicate a scientific notation. Examples of numbers that will be identified as floats are 0.5 and  &nbsp;-7.823457.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However,  it can be formed directly by typing the number with a decimal number. Extra zeros that are present on the number&#8217;s end will be neglected automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example 1: Creating Float and Checking Type in Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a float\nmy_float = 3.14\n\n# Checking the type\ntype_of_my_float = type(my_float)\n\n# Output\nprint(\"My Float:\", my_float)\nprint(\"Type of My Float:\", type_of_my_float)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My Float: 3.14\nType of My Float: &lt;class 'float'&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Thus, it is understood that float is created by dividing these two integers. A float will be formed through the execution of the two floats.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example 2- Arithmetic operations on the Float type in Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 5.5\nb = 3.2\n \n# Addition\nc = a + b\nprint(\"Addition:\", c)\n \n# Subtraction\nc = a-b\nprint(\"Subtraction:\", c)\n \n# Division\nc = a\/b\nprint(\"Division:\", c)\n \n# Multiplication\nc = a*b\nprint(\"Multiplication:\", c)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My Float: 3.14\nType of My Float: &lt;class 'float'&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-complex-type\">What is Python Complex Type?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Complex number consists of a number that has real and imaginary parts. Such as 2+3j is considered a complex number, in which  2 is the real component and 3 will be multiplied by j and will be identified as an imaginary part.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example 1- Creating Complex and checking types in Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a complex number\nmy_complex = 2 + 3j\n\n# Checking the type\ntype_of_my_complex = type(my_complex)\n\n# Output\nprint(\"My Complex:\", my_complex)\nprint(\"Type of My Complex:\", type_of_my_complex)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My Complex: (2+3j)\nType of My Complex: &lt;class 'complex'&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example 2 &#8211; Arithmetic operations on the complex type<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating complex numbers\ncomplex_num_1 = 2 + 3j\ncomplex_num_2 = 1 - 1j\n\n# Addition\nsum_result = complex_num_1 + complex_num_2\n\n# Subtraction\ndifference_result = complex_num_1 - complex_num_2\n\n# Multiplication\nproduct_result = complex_num_1 * complex_num_2\n\n# Division\ndivision_result = complex_num_1 \/ complex_num_2\n\n# Output\nprint(\"Complex Number 1:\", complex_num_1)\nprint(\"Complex Number 2:\", complex_num_2)\n\nprint(\"\\nAddition:\", sum_result)\nprint(\"Subtraction:\", difference_result)\nprint(\"Multiplication:\", product_result)\nprint(\"Division:\", division_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Complex Number 1: (2+3j)\nComplex Number 2: (1-1j)\n\nAddition: (3+2j)\nSubtraction: (1+4j)\nMultiplication: (5+1j)\nDivision: (1+2j)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-type-conversion-in-python\">What is Type Conversion in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, it is possible to turn one number into another with the help of two methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Using Arithmetic Operations:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Operations such as addition and subtraction are used to convert the type of number automatically. One of the operands will be float. Then, this method won&#8217;t be performed on complex numbers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example  1 &#8211; Type Conversion using the arithmetic operations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1.6\nb = 5\n \nc = a + b\n \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>6.6\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Using the built-in functions<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example 1- Type conversion using the Built-in functions in Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating variables of different types\ninteger_value = 42\nfloat_value = 3.14\nstring_value = \"123\"\n\n# Using built-in functions for type conversion\nconverted_integer = int(float_value)\nconverted_float = float(string_value)\nconverted_string = str(integer_value)\n\n# Output\nprint(\"Original Integer Value:\", integer_value)\nprint(\"Converted Float Value:\", converted_integer)\nprint(\"Type of Converted Integer:\", type(converted_integer))\n\nprint(\"\\nOriginal Float Value:\", float_value)\nprint(\"Converted String Value:\", converted_float)\nprint(\"Type of Converted Float:\", type(converted_float))\n\nprint(\"\\nOriginal String Value:\", string_value)\nprint(\"Converted Integer Value:\", converted_string)\nprint(\"Type of Converted String:\", type(converted_string))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Integer Value: 42\nConverted Float Value: 3\nType of Converted Integer: &lt;class 'int'&gt;\n\nOriginal Float Value: 3.14\nConverted String Value: 123.0\nType of Converted Float: &lt;class 'float'&gt;\n\nOriginal String Value: 123\nConverted Integer Value: 42\nType of Converted String: &lt;class 'str'&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"random-numbers-in-python\">Random Numbers in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python will create a random module for producing a pseudo-random number. Hence, this module can make random numbers, and choose a random element from a sequence in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example 1- To create random value in Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\n\n# Generating a random integer between a specified range\nrandom_integer = random.randint(1, 100)\n\n# Generating a random float between 0 and 1\nrandom_float = random.random()\n\n# Generating a random value from a list\nmy_list = &#91;\"apple\", \"banana\", \"cherry\", \"date\"]\nrandom_value_from_list = random.choice(my_list)\n\n# Output\nprint(\"Random Integer:\", random_integer)\nprint(\"Random Float:\", random_float)\nprint(\"Random Value from List:\", random_value_from_list)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Random Integer: 73\nRandom Float: 0.864826264695416\nRandom Value from List: cherry\n<\/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, Python provides straightforward and versatile support for numbers, offering built-in data types like integers, floats, and complex numbers. You can easily perform arithmetic operations, type conversions, and generate random values using simple and intuitive syntax. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-numbers-fa-qs\">Python Numbers- 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-1707808559836\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are Python numbers?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It denotes the numeric data types in Python<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707808573835\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What is int () in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  Python int () function will turn the specified value into the integer value.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707808580868\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How do you declare numbers in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Python doesn&#8217;t have any command to declare a variable.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python consists of built-in support for storing and processing numeric data which is the Python Numbers. It is required to work with the numbers in every Python application. This article has listed the various types of Python Numbers. What is a Numeric data type? The numeric data type can store numeric values. So, they are &#8230; <a title=\"Python Numbers\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-numbers\/\" aria-label=\"More on Python Numbers\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7028,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,901,900],"class_list":["post-7023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-data-type","tag-python-numbers","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\/7023","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=7023"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7023\/revisions"}],"predecessor-version":[{"id":8258,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7023\/revisions\/8258"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7028"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}