{"id":93,"date":"2019-01-10T19:18:43","date_gmt":"2019-01-10T19:18:43","guid":{"rendered":"https:\/\/kindsonthegenius.com\/python\/?p=93"},"modified":"2026-07-04T01:20:47","modified_gmt":"2026-07-04T01:20:47","slug":"04-python-data-types","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/python\/04-python-data-types\/","title":{"rendered":"Python &#8211; Data Types"},"content":{"rendered":"<!-- ktg-updated-banner -->\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated June 29, 2026.<\/strong> Refreshed for Python 3.12+ and current SEO best practices.<\/p>\n<\/div>\n\n\r\n<p class=\"wp-block-paragraph\">The Python programming language provides a set of data types that can be used to define a variable. Memory is allocated for a variable based on the data type of the variable. This is because, data types have different sizes which is in bytes.<\/p>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Variable Declaration<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Python does not provide an explicit way to declare a variable. It uses implicit variable declaration. For instance in language like Java, you must declare a variable before you can assign it a value.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In Python, the variable declaration is done automatically when a variable is assigned a value using the assignment operator (=). The examples below show variable assignment (with implicit declaration).<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code># Assignment and implicit data type declaration in Python\r\nindex = 10              # An integer data type\r\ndistance = 500.05       # A floating point data type\r\nfirstname = \"Kindson\"   # A string data\r\nscores = [67,87,98,54,82] # A list data type\r\n\r\nprint(index)\r\nprint(distance)\r\nprint(firstname)<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In the above program, index is an integer type, distance is a floating point type while firstname is a string type. If you run the above code, you will have :<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>10 \r\n500.05 \r\nKindson<\/code><\/pre>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Multiple Assignments<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A unique feature of Python is that it allows you to assign a value to more than one variable in the same line. This is accomplished using the code below:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>x = y = z = 200\r\nscore = average = 89.9<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In the first line three assignments was made. 200 is assigned to x, assigned to y, and assigned to z. This is equivalent to :<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>x = 200\r\ny = 200\r\nz = 200<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In the second line score is assigned a value of 89.9 and average is assigned a value of 89.9 as well<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Another way to achieve multiple assignment is to assign different values to different variables in the same. For the example below, name is assigned a value Kindson, score is assigned a value of 100 and age is assigned a value 36<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>name, score, age = \"Kindson\", 100, 36<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You simply use comma to separate the variable names and the values and then the variables are assigned receptively.<\/p>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Python Standard Data Types<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The Python Programming Language provided inbuilt data types which a have standard memory size associated with them. The 5 Python standard data types we are going to consider includes:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Numbers<\/li>\r\n<li>String<\/li>\r\n<li>Lists<\/li>\r\n<li>Tuples<\/li>\r\n<li>Dictionary<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Numbers in Python<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Numbers are data types used to store numeric values. To use a number data type simply assign a number to the variable.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>score = 89.4\r\ntotal = 200<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The above code create two number variables, score and total and assigns them values<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Python provides a function to delete reference to a number variable that has been created. This is done using del keyword. For example:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>del score\r\ndel total\r\ndel x, y, z<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The four different number types provided by Python are:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>int &#8211; signed or unsigned integer values<\/li>\r\n<li>long &#8211; long integers<\/li>\r\n<li>float &#8211; floating point values<\/li>\r\n<li>complex &#8211; complex numbers having a real and imaginary parts<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The table below give a summary of<\/p>\r\n\r\n\r\n\r\n<table class=\"wp-block-table is-style-stripes\">\r\n<tbody>\r\n<tr>\r\n<th>int<\/th>\r\n<th>long<\/th>\r\n<th>float<\/th>\r\n<th>complex<\/th>\r\n<\/tr>\r\n<tr>\r\n<td>300<\/td>\r\n<td>8976576L<\/td>\r\n<td>0.45<\/td>\r\n<td>5.56j<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>55<\/td>\r\n<td>-0x29656L<\/td>\r\n<td>45.20<\/td>\r\n<td>63.j<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>-524<\/td>\r\n<td>055L<\/td>\r\n<td>-52.8<\/td>\r\n<td>5.087e-24j<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Python Strings<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In Python, strings are identified as a set of characters enclosed in quotes, which may be either single or double quotes. Substrings of a particular string can be formed using the slice operator [ ] and [:].<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A substring is specified with index staring from 0 for the first character of the string. A index of -1 is also used to indicate the last character on the string.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Two strings can be joined together using the concatenation operator (+). Strings can be repeted or multiplied using the repetition operator (*). Lets take some examples:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>word = \"Python Programming\"\r\nprint(word)              # Prints out the entire string\r\nprint(word[0])           # Prints the first character in the string\r\nprint(word[3:6])         # Print the 4th to the 7th character\r\nprint(word[3:]           # Prints the 4th to the last character\r\nprint(word + \" Tutorial\" # Attaches Tutorial to the end of the string\r\nprint(word * 3)          # Prints the string 3 times<\/code><\/pre>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Python Lists<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Lists are part of Python collection data types. A list in Python contains different values separated by comma and enclosed in square braces []. Items in a list could be of the same or of different data types.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Just like in strings, individual elements of a list can be accessed using the slice operator [ ] or [ ]. Starting from the first element of the list, the index is 0. Starting from the last element, the index is -1.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The concatenation operator (+) and the repetition operator (*) applies in list just like in strings. The program code below illustrates the use of lists in Python.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>list1= [ 'wxyz', 0.85, 52, '100', 780.2 ]\r\nlist2 = [999, 'Kindson']\r\n\r\nprint(list1)         # Prints whole list\r\nprint(list1[0])      # Prints first item in of the list\r\nprint(list1[1:4])     # Prints elements beginning from 2nd till 5th \r\nprint(list1[2:])     # Prints elements beginning from 3rd element\r\nprint(list2 * 2)      # Prints list twice\r\nprint(list1 + list2)  # Combines bot list1 and list2<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">If the above code is executed, the output would be:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>[ 'wxyz', 0.85, 52, '100', 780.2 ]\r\nwxyz\r\n[0.85, 52, '100', 780.2]\r\n[52, '100', 780.2]\r\n[999, 'Kindson'999, 'Kindson']\r\n['wxyz', 0.85, 52, '100', 780.2, 999, 'Kindson' ]<\/code><\/pre>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Python Tuples<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Tuples in Python are similar to lists with the following two difference:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Tuples are specified using rounded brackets (). Not square braces!.<\/li>\r\n<li>Tuples are immutable &#8211; this means that items in a tuple cannot be modified.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Let&#8217;s take some examples<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>tuple1= ( 'wxyz', 0.85, 52, '100', 780.2 )\r\ntuple2= (999, 'Kindson')<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Every other operations that apply to a list applies to a tuple. But take not that the operations below would succeed in a list but would generate an error in a tuple.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>list1[0] = 'abcd'      # Would succeed because lists a mutable\r\ntuple1[0] = 'abcd'     # The would fail because tuples are immutable<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Python Dictionary<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Python Dictionary are used to store key value pairs just like in a hash table. Each entry in a dictionary has a key and a value. Keys and values in a dictionary can be of any type.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A typical example of a dictionary in Python is the days of the week where each week day is assigned a number each corresponding to the day name. This is shown below<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>weekdays = {\r\n1: \"Monday\",\r\n2: \"Tuesday\",\r\n3: \"Wednesday\",\r\n4: \"Thursday\",\r\n5: \"Friday\",\r\n6: \"Saturday\",\r\n7: \"Sunday\"\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The following operations are all valid for a dictionary in Python:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>print(weekdays[1])       # Prints Monday\r\nprint(weekdays)          # Prints the complete dictionary\r\nprint(weekdays.keys())   # Prints all the keys, 1, 2, 3,4,5,6,7\r\nprint(weekdays.values()) # Prints all the values: Monday, . . , Sunday<\/code><\/pre>\r\n\r\n\r\n<hr \/>\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Data Type Conversion in Python<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Python provides functions needed to convert between the data types. For example, you may want to covert a user input in string to an integer to be able to use it for calculation. The table below gives a list of Python data type conversion functions<\/p>\r\n\r\n\r\n\r\n<table class=\"wp-block-table is-style-stripes\">\r\n<tbody>\r\n<tr>\r\n<th>S#<\/th>\r\n<th>Function and Description<\/th>\r\n<\/tr>\r\n<tr>\r\n<td>1<\/td>\r\n<td><strong>int(x [,base])<\/strong>Converts x to an integer. Base specifies the base if x is a string.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>2<\/td>\r\n<td><strong>long(x [,base] )<\/strong> -Converts x to a long integer. Base specifies the base if x is a string.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>3<\/td>\r\n<td><strong>float(x)<\/strong>Converts x to a floating-point number.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>4<\/td>\r\n<td><strong>complex(real [,imag])<\/strong>&#8211; Creates a complex number.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>5<\/td>\r\n<td><strong>str(x)\u00a0&#8211;<\/strong>Converts object x to a string representation.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>6<\/td>\r\n<td><strong>repr(x)\u00a0&#8211;<\/strong>Converts object x to an expression string.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>7<\/td>\r\n<td><strong>eval(str)\u00a0&#8211;<\/strong>Evaluates a string and returns an object.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>8<\/td>\r\n<td><strong>tuple(s)\u00a0&#8211;<\/strong>Converts s to a tuple.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>9<\/td>\r\n<td><strong>list(s)\u00a0&#8211;<\/strong>Converts s to a list.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>10<\/td>\r\n<td><strong>set(s)\u00a0&#8211;<\/strong>Converts s to a set.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>11<\/td>\r\n<td><strong>dict(d)\u00a0&#8211;\u00a0<\/strong>Creates a dictionary. d need to be a sequence of (key,value) tuples.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>12<\/td>\r\n<td><strong>frozenset(s)\u00a0&#8211;<\/strong>Converts s to a frozen set.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>13<\/td>\r\n<td><strong>chr(x)\u00a0&#8211;\u00a0<\/strong>Converts an integer type to a character type.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>14<\/td>\r\n<td><strong>unichr(x)\u00a0&#8211;\u00a0<\/strong>Converts an integer type to a Unicode character.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>15<\/td>\r\n<td><strong>ord(x)\u00a0&#8211;\u00a0<\/strong>Converts a single character type to its integer value.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>16<\/td>\r\n<td><strong>hex(x)\u00a0&#8211;\u00a0<\/strong>Converts an integer type to a hexadecimal string.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>17<\/td>\r\n<td><strong>oct(x)\u00a0&#8211;\u00a0<\/strong>Converts an integer type to an octal string.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\n\n<!-- ktg-lesson-nav -->\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/03-python-variable-and-data-types\/\">Lesson 3: Python &#8211; Basic Syntax<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/05-python-operators\/\">Lesson 5: Python &#8211; Operators<\/a><\/p>\n<\/nav>\n\n<!-- ktg-alkademy-cta -->\n<div class=\"ktg-alkademy-cta\" style=\"margin:2em 0;padding:1.25em;border-left:4px solid #2563eb;background:#f8fafc;\">\n<p><strong>Want live Python classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led Python and data science courses with hands-on projects.<\/p>\n<\/div>\n\n<!-- ktg-author-trust -->\n<div class=\"ktg-author-trust\" style=\"margin:2em 0;padding:1.25em;border-top:1px solid #e2e8f0;\">\n<p>Kindson Munonye is a software engineer and technical author specializing in Python, data science, and machine learning. He publishes free step-by-step Python tutorials and live classes on Alkademy.\n\n<a href=\"https:\/\/github.com\/KindsonTheGenius\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> \u00b7 <a href=\"https:\/\/www.linkedin.com\/in\/kindson\" target=\"_blank\" rel=\"noopener noreferrer\">LinkedIn<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/about\/\">About<\/a> \u00b7 <a href=\"https:\/\/www.alkademy.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a><\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Updated June 29, 2026. Refreshed for Python 3.12+ and current SEO best practices. The Python programming language provides a set of data types that can &hellip; <\/p>\n","protected":false},"author":395,"featured_media":287,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":571,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/93\/revisions\/571"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/287"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}