Python Data Types
Python Data Types
Numeric
Sequence Type
Boolean
Set
Dictionary
Binary Types( memoryview, bytearray, bytes)
This code assigns variable ‘x’ different values of various data types in
We use cookies to ensure
Python. you have the
It covers best browsing
string, experience
integer, on our website.
float, complex, By usinglist, tuple, range,
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
dictionary, set, frozenset,Policy boolean, bytes, bytearray, memoryview, and the
https://www.geeksforgeeks.org/python-data-types/ 1/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
1200+ have already taken up the challenge. It's your turn now! Get 90% refund
on course fees upon achieving 90% completion.Discover How
Python
x = "Hello World"
x = 50
x = 60.5
x = 3j
x = ["geeks", "for", "geeks"]
x = ("geeks", "for", "geeks")
x = range(10)
x = {"name": "Suraj", "age": 24}
x = {"geeks", "for", "geeks"}
x = frozenset({"geeks", "for", "geeks"})
x = True
x = b"Geeks"
x = bytearray(4)
x = memoryview(bytes(6))
x = None
https://www.geeksforgeeks.org/python-data-types/ 2/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Python
a = 5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Output:
https://www.geeksforgeeks.org/python-data-types/ 3/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Python List
Python Tuple
Creating String
Strings in Python can be created using single quotes, double quotes, or even
triple quotes.
Python
String1 = '''Geeks
For
Life'''
print("\nCreating a multiline String: ")
print(String1)
Output:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 4/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Example: This Python code demonstrates how to work with a string named
‘String1′. It initializes the string with “GeeksForGeeks” and prints it. It then
showcases how to access the first character (“G”) using an index of 0 and
the last character (“s”) using a negative index of -1.
Python
String1 = "GeeksForGeeks"
print("Initial String: ")
print(String1)
print("\nFirst character of String is: ")
print(String1[0])
print("\nLast character of String is: ")
print(String1[-1])
Output:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 5/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Initial String:
GeeksForGeeks
First character of String is:
G
Last character of String is:
s
Lists are just like arrays, declared in other languages which is an ordered
collection of data. It is very flexible as the items in a list do not need to be of
the same type.
Lists in Python can be created by just placing the sequence inside the square
brackets[].
Python
List = []
print("Initial blank List: ")
print(List)
List = ['GeeksForGeeks']
print("\nList with the use of String: ")
print(List)
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple values: ")
print(List[0])
print(List[2])
List = [['Geeks', 'For'], ['Geeks']]
We useprint("\nMulti-Dimensional
cookies to ensure you have the best browsing
List: ")
experience on our website. By using
our site, you acknowledge
print(List)
that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 6/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Output:
In order to access the list items refer to the index number. Use the index
operator [ ] to access an item in a list. In Python, negative sequence indexes
represent positions from the end of the array. Instead of having to compute
the offset as in List[len(List)-3], it is enough to just write List[-3]. Negative
indexing means beginning from the end, -1 refers to the last item, -2 refers
to the second-last item, etc.
Python
Output:
Geeks
Just like a list, a tuple is also an ordered collection of Python objects. The
only difference between a tuple and a list is that tuples are immutable i.e.
tuples cannot be modified after it is created. It is represented by a tuple
class.
Python
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)
Tuple1 = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)
We useTuple1 = ensure
cookies to (0, 1,you2, 3)the best browsing experience on our website. By using
have
Tuple2
our site, = ('python',
you acknowledge that you'geek')
have read and understood our Cookie Policy & Privacy
Tuple3 = (Tuple1, Tuple2) Policy
print("\nTuple with nested tuples: ")
https://www.geeksforgeeks.org/python-data-types/ 8/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
print(Tuple3)
Output:
The code creates a tuple named ‘tuple1′ with five elements: 1, 2, 3, 4, and
5. Then it prints the first, last, and third last elements of the tuple using
indexing.
Python
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 9/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Output:
Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise
python will throw an error.
Example: The first two lines will print the type of the boolean values True
and False, which is <class ‘bool’>. The third line will cause an error,
because true is not a valid keyword in Python. Python is case-sensitive,
which means it distinguishes between uppercase and lowercase letters. You
need to capitalize the first letter of true to make it a boolean value.
Python
print(type(True))
print(type(False))
print(type(true))
Output:
<class 'bool'>
We use <class
cookies to 'bool'>
ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 10/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Sets can be created by using the built-in set() function with an iterable
object or a sequence by placing the sequence inside curly braces, separated
by a ‘comma’. The type of elements in a set need not be the same, various
mixed-up data type values can also be passed to the set.
Example: The code is an example of how to create sets using different types
of values, such as strings, lists, and mixed values
Python
set1 = set()
print("Initial blank Set: ")
print(set1)
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
set1 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])
print("\nSet with the use of Mixed Values")
print(set1)
Output:
We use cookies to ensure you have the best browsing experience on our website. By using
our site,Initial blankthat
you acknowledge Set:
you have read and understood our Cookie Policy & Privacy
set() Policy
https://www.geeksforgeeks.org/python-data-types/ 11/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Example: This Python code creates a set named set1 with the
values “Geeks”, “For” and “Geeks”. The code then prints the initial set, the
elements of the set in a loop, and checks if the value “Geeks” is in the set
using the ‘in’ operator
Python
Output:
Initial set:
{'Geeks', 'For'}
Elements of set:
Geeks For
True
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Note – To know more about Policysets, refer to Python Sets.
https://www.geeksforgeeks.org/python-data-types/ 12/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Example: This code creates and prints a variety of dictionaries. The first
dictionary is empty. The second dictionary has integer keys and string
values. The third dictionary has mixed keys, with one string key and one
integer key. The fourth dictionary is created using the dict() function, and the
fifth dictionary is created using the [(key, value)] syntax
Python
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Dict = dict({1: 'Geeks', 2: 'For', 3: 'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
Dict = dict([(1, 'Geeks'), (2, 'For')])
We useprint("\nDictionary
cookies to ensure you havewith
the best browsing
each itemexperience on our")website. By using
as a pair:
our site, you acknowledge
print(Dict) that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 13/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Output:
Empty Dictionary:
{}
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with the use of Mixed Keys:
{1: [1, 2, 3, 4], 'Name': 'Geeks'}
Dictionary with the use of dict():
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with each item as a pair:
{1: 'Geeks', 2: 'For'}
In order to access the items of a dictionary refer to its key name. Key can be
used inside square brackets. There is also a method called get() that will
also help in accessing the element from a dictionary.
Python
Output:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 14/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Python
Output
Python
coordinates = (3, 5)
print(coordinates)
print("X-coordinate:", coordinates[0])
print("Y-coordinate:", coordinates[1])
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Output Policy
https://www.geeksforgeeks.org/python-data-types/ 15/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
(3, 5)
X-coordinate: 3
Y-coordinate: 5
Don't miss your chance to ride the wave of the data revolution! Every
industry is scaling new heights by tapping into the power of data. Sharpen
your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning
and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Previous Next
Similar Reads
Python | Get tuple element data types Python | Convert mixed data types
tuple list to string list
How To Convert Data Types in Python Python - Extract rows with Complex
3? data types
https://www.geeksforgeeks.org/python-data-types/ 16/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Kotlin Data Types Django model data types and fields list
Select Columns with Specific Data How to Convert to Best Data Types
Types in Pandas Dataframe Automatically in Pandas?
Complete Tutorials
Python Crash Course Python API Tutorial: Getting Started
with APIs
N nikhilagg…
Additional Information
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 17/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive
https://www.geeksforgeeks.org/python-data-types/ 18/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
https://www.geeksforgeeks.org/python-data-types/ 19/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
Class 9 Biology
Class 8 Social Science
Complete Study Material English Grammar
Colleges Companies
Indian Colleges Admission & Campus Experiences META Owned Companies
List of Central Universities - In India Alphabhet Owned Companies
Colleges in Delhi University TATA Group Owned Companies
IIT Colleges Reliance Owned Companies
NIT Colleges
IIIT Colleges
https://www.geeksforgeeks.org/python-data-types/ 20/21
1/30/24, 4:30 PM Python Data Types - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/python-data-types/ 21/21