CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

08 April, 2022

Python Data Types

 Programing Coderfunda     April 08, 2022     Python     No comments   

 

Python Data Types

Variables can hold values, and every value has a data-type. Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.

  1. a = 5  

The variable a holds integer value five and we did not define its type. Python interpreter will automatically interpret variables a as an integer type.

Python enables us to check the type of the variable used in the program. Python provides us the type() function, which returns the type of the variable passed.

Consider the following example to define the values of different data types and checking its type.

  1. a=10  
  2. b="Hi Python"  
  3. c = 10.5  
  4. print(type(a))  
  5. print(type(b))  
  6. print(type(c))  

Output:

<type 'int'>
<type 'str'>
<type 'float'>

Standard data types

A variable can hold different types of values. For example, a person's name must be stored as a string whereas its id must be stored as an integer.

Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.

  1. Numbers
  2. Sequence Type
  3. Boolean
  4. Set
  5. Dictionary
Python Data Types

In this section of the tutorial, we will give a brief introduction of the above data-types. We will discuss each one of them in detail later in this tutorial.

Numbers

Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. Python provides the type() function to know the data-type of the variable. Similarly, the isinstance() function is used to check an object belongs to a particular class.

Python creates Number objects when a number is assigned to a variable. For example;

  1. a = 5  
  2. print("The type of a", type(a))  
  3.   
  4. b = 40.5  
  5. print("The type of b", type(b))  
  6.   
  7. c = 1+3j  
  8. print("The type of c", type(c))  
  9. print(" c is a complex number", isinstance(1+3j,complex))  

Output:

The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True

Python supports three types of numeric data.

  1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc. Python has no restriction on the length of an integer. Its value belongs to int
  2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate upto 15 decimal points.
  3. complex - A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts, respectively. The complex numbers like 2.14j, 2.0 + 2.3j, etc.

Sequence Type

String

The string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string.

String handling in Python is a straightforward task since Python provides built-in functions and operators to perform operations in the string.

In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python".

The operator * is known as a repetition operator as the operation "Python" *2 returns 'Python Python'.

The following example illustrates the string in Python.

Example - 1

  1. str = "string using double quotes"  
  2. print(str)  
  3. s = '''''A multiline 
  4. string'''  
  5. print(s)  

Output:

string using double quotes
A multiline
string

Consider the following example of string handling.

Example - 2

  1. str1 = 'hello javatpoint' #string str1    
  2. str2 = ' how are you' #string str2    
  3. print (str1[0:2]) #printing first two character using slice operator    
  4. print (str1[4]) #printing 4th character of the string    
  5. print (str1*2) #printing the string twice    
  6. print (str1 + str2) #printing the concatenation of str1 and str2    

Output:

he
o
hello javatpointhello javatpoint
hello javatpoint how are you

List

Python Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].

We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Consider the following example.

  1. list1  = [1, "hi", "Python", 2]    
  2. #Checking type of given list  
  3. print(type(list1))  
  4.   
  5. #Printing the list1  
  6. print (list1)  
  7.   
  8. # List slicing  
  9. print (list1[3:])  
  10.   
  11. # List slicing  
  12. print (list1[0:2])   
  13.   
  14. # List Concatenation using + operator  
  15. print (list1 + list1)  
  16.   
  17. # List repetation using * operator  
  18. print (list1 * 3)  

Output:

[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]

Tuple

A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().

A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple.

Let's see a simple example of the tuple.

  1. tup  = ("hi", "Python", 2)    
  2. # Checking type of tup  
  3. print (type(tup))    
  4.   
  5. #Printing the tuple  
  6. print (tup)  
  7.   
  8. # Tuple slicing  
  9. print (tup[1:])    
  10. print (tup[0:1])    
  11.   
  12. # Tuple concatenation using + operator  
  13. print (tup + tup)    
  14.   
  15. # Tuple repatation using * operator  
  16. print (tup * 3)     
  17.   
  18. # Adding value to tup. It will throw an error.  
  19. t[2] = "hi"  

Output:

<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    t[2] = "hi";
TypeError: 'tuple' object does not support item assignment

Dictionary

Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type, whereas value is an arbitrary Python object.

The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.

Consider the following example.

  1. d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}     
  2.   
  3. # Printing dictionary  
  4. print (d)  
  5.   
  6. # Accesing value using keys  
  7. print("1st name is "+d[1])   
  8. print("2nd name is "+ d[4])    
  9.   
  10. print (d.keys())    
  11. print (d.values())    

Output:

1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])

Boolean

Boolean type provides two built-in values, True and False. These values are used to determine the given statement true or false. It denotes by the class bool. True can be represented by any non-zero value or 'T' whereas false can be represented by the 0 or 'F'. Consider the following example.

  1. # Python program to check the boolean type  
  2. print(type(True))  
  3. print(type(False))  
  4. print(false)  

Output:

<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined

Set

Python Set is the unordered collection of the data type. It is iterable, mutable(can modify after creation), and has unique elements. In set, the order of the elements is undefined; it may return the changed sequence of the element. The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma. It can contain various types of values. Consider the following example.

  1. # Creating Empty set  
  2. set1 = set()  
  3.   
  4. set2 = {'James', 2, 3,'Python'}  
  5.   
  6. #Printing Set value  
  7. print(set2)  
  8.   
  9. # Adding element to the set  
  10.   
  11. set2.add(10)  
  12. print(set2)  
  13.   
  14. #Removing element from the set  
  15. set2.remove(2)  
  16. print(set2)  

Output:

{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • First Python ProgramFirst Python ProgramIn this Section, we will discuss the basic syntax of Python, we will run a simple program to print Hello World on the co… Read More
  • Python Data Types Python Data TypesVariables can hold values, and every value has a data-type. Python is a dynamically typed language; hence we do not need to def… Read More
  • Python KeywordsPython KeywordsPython Keywords are special reserved words that convey a special meaning to the compiler/interpreter. Each keyword has a special meanin… Read More
  • How to Install Python (Environment Set-up) How to Install Python (Environment Set-up)In order to become Python developer, the first step is to learn how to install or update Python on a l… Read More
  • Python VariablesPython VariablesVariable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.In … Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Laravel auth login with phone or email
          <?php     Laravel auth login with phone or email     <? php     namespace App \ Http \ Controllers \ Auth ;         use ...
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Auto-translate Application Strings with Laratext - 5/16/2025
  • Simplify Factory Associations with Laravel's UseFactory Attribute - 5/13/2025
  • Improved Installation and Frontend Hooks in Laravel Echo 2.1 - 5/15/2025
  • Filter Model Attributes with Laravel's New except() Method - 5/13/2025
  • Arr::from() Method in Laravel 12.14 - 5/14/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda