SlideShare a Scribd company logo
BASIC DATA TYPES IN PYTHON
PROF. SUNIL D. CHUTE
HEAD DEPT OF COMPUTER SCIENC
M.G. COLLEGE ARMORI
BASIC DATA TYPES IN PYTHON
 Data types are the classification or
categorization of data items. It represents the
kind of value that tells what operations can
be performed on a particular data. Since
everything is an object in Python
programming, data types are actually classes
and variables are instance (object) of these
classes.
One way to categorize these basic data types is in
one of four groups:
 Numeric: int, float and the less frequently
encountered complex
 Sequence: str (string), list and tuple
 Boolean: (True or False)
 Dictionary: dict(dictionary) data type, consisting
of (key, value) pairs
NOTE: It's important to point out that Python usually doesn't require you
to specify what data type you are using and will assign a data type to
your variable based on what it thinks you meant.
An equally important thing to point out is that Python is a
"loosely/weakly typed" programming language, meaning that a variable
can change its type over the course of the program's execution, which
isn't the case with "strongly typed" programming languages (such as
Java or C++).
Basic data types in python
NUMERIC DATA TYPES
These data types are fairly straight-forward and represent
numeric values. These can be decimal values, floating point
values or even complex numbers.
 Integer Data Type - int
The int data type deals with integers values. This means
values like 0, 1, -2 and -15, and not numbers like 0.5, 1.01, -
10.8, etc.
If you give Python the following code, it will conclude that a is
an integer and will assign the int data type to it:
>>> x = 5
>>> type(x)
<class 'int'>
We could have been more specific and said something along these
lines, to make sure Python understood our 5 as an integer, though,
it'll automatically do this exact same thing under the hood:
>>> x = int(5)
>>> type(x)
<class 'int‘>
It's worth noting that Python treats any sequence of numbers (without a
prefix) as a decimal number. This sequence, in fact, isn't constrained.
That is to say, unlike in some other languages like Java, the value of
the int doesn't have a maximum value - it's unbounded.
The sys.maxsize may sound counterintuitive then, since it implies that that's
the maximum value of an integer, though, it isn't.
>>> x = sys.maxsize
>>> x
2147483647
This appears to be a 32-bit signed binary integer value, though, let's see
what happens if we assign a higher number to x:
>>> x = sys.maxsize
>>> x+1
2147483648
In fact, we can even go as far as:
>>> y = sys.maxsize + sys.maxsize
>>> y
4294967294
The only real limit to how big an integer can be is the memory
of the machine you're running Python on.
 Prefixing Integers
What happens when you'd like to pack a numeric value in a
different form? You can prefix a sequence of numbers and tell
Python to treat them in a different system.
More specifically, the prefixes:
 0b or 0B - Will turn your integer into Binary
 0o or 0O - Will turn your integer into Octal
 0x or 0X - Will turn your integer into Hexadecimal
 # Decimal value of 5
>>> x = 5
>>> x
5
 # Binary value of 1
>>> x = 0b001
>>> x
1
 # Octal value of 5
>>> x = 0o5
>>> x
5
 # Hexadecimal value of 10
>>> x = 0x10
>>> x
16
FLOATING POINT DATA TYPE - FLOAT
 The float type in Python designates a floating-point number. float values
are specified with a decimal point. Optionally, the
character e or E followed by a positive or negative integer may be
appended to specify scientific notation
Ex.1 >>> 4.2 4.2
>>> type(4.2)
<class 'float'>
Ex.2 >>> 4.
4.0
Ex.3 >>> .2
0.2
Ex.4>>> .4e7 4000000.0
>>> type(.4e7)
<class 'float'>
Ex.5>>> 4.2e-4
COMPLEX NUMBERS
 Complex numbers are specified as <real
part>+<imaginary part>j.
Ex. >>> 2+3j
(2+3j)
>>> type(2+3j)
<class 'complex'>
STRINGS
 Strings are sequences of character data. The string
type in Python is called str.
 String literals may be delimited using either single or
double quotes. All the characters between the opening
delimiter and matching closing delimiter are part of the
string:
Ex.>>> print("I am a string.")
I am a string.
>>> type("I am a string.")
<class 'str'>
Ex.>>> print('I am too.')
I am too.
>>> type('I am too.')
<class 'str'>
 A string in Python can contain as many characters as you
wish. The only limit is your machine’s memory resources.
A string can also be empty:
Ex. >>> ''
''
 What if you want to include a quote character as part of
the string itself? Your first impulse might be to try
something like this:
Ex.>>> print('This string contains a single quote (')
character.')
SyntaxError: invalid syntax
Ex. >>> mystring = "This is not my first String"
>>> print (mystring);
This is not my first String
 to join two or more strings.
Ex.>>> print ("Hello" + "World");
HelloWorld
Ex. >>> s1 = "Name Python "
>>> s2 = "had been adapted "
>>> s3 = "from Monty Python"
>>> print (s1 + s2 + s3)
Name Python had been adapted from Monty
Python
 use input() function
Ex.>>> n = input("Number of times you
want the text to repeat: ")
Number of times you want the text to
repeat:
>>> print ("Text"*n);
TextTextTextTextText
 Check existence of a character or a sub-
string in a string
Ex.>>> "won" in "India won the match"
True
PYTHON LIST DATA TYPE
 List is an ordered sequence of items. It is one of
the most used datatype in Python and is very
flexible. All the items in a list do not need to be
of the same type.
 Declaring a list is pretty straight forward. Items
separated by commas are enclosed within
brackets [ ].
Ex. a = [1, 2.2, 'python']
We can use the slicing operator [ ] to extract an
item or a range of items from a list. The index
starts from 0 in Python.
Basic data types in python
PYTHON TUPLE DATA TYPE
 Tuple is an ordered sequence of items same
as a list. The only difference is that tuples are
immutable. Tuples once created cannot be
modified.
 Tuples are used to write-protect data and are
usually faster than lists as they cannot
change dynamically.
 It is defined within parentheses () where
items are separated by commas.
Ex. t = (5,'program', 1+3j)
 We can use the slicing operator [] to extract
items but we cannot change its value.
PYTHON SET DATA TYPE
 Set is an unordered collection of unique
items. Set is defined by values separated by
comma inside braces { }. Items in a set are
not ordered.
 A set is created by placing all the items
(elements) inside curly braces {}, separated
by comma, or by using the built-
in set() function.
 It can have any number of items and they
may be of different types (integer, float, tuple,
string etc.). But a set cannot have mutable
Basic data types in python
Basic data types in python
PYTHON DICTIONARY DATA TYPE
 Python dictionary is an unordered collection
of items. Each item of a dictionary has
a key/value pair.
 It is generally used when we have a huge
amount of data. Dictionaries are optimized
for retrieving data. We must know the key to
retrieve the value.
 In Python, dictionaries are defined within
braces {} with each item being a pair in the
form key:value. Key and value can be of any
type.
Basic data types in python
CONVERSION BETWEEN DATA TYPES
Basic data types in python

More Related Content

PPTX
Data types in python
PDF
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
PPT
Interpixel redundancy
PPTX
Titanic survivor prediction ppt (5)
PDF
CLASS 10 IT PRACTICAL FILE.pdf
PPTX
Python for kids - 1.pptx
PPTX
Python Data Structures and Algorithms.pptx
PPTX
Presentation on python
Data types in python
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Interpixel redundancy
Titanic survivor prediction ppt (5)
CLASS 10 IT PRACTICAL FILE.pdf
Python for kids - 1.pptx
Python Data Structures and Algorithms.pptx
Presentation on python

What's hot (20)

PPTX
Fundamentals of Python Programming
PPT
programming with python ppt
PPTX
PDF
Operators in python
PPTX
Values and Data types in python
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
PPTX
Introduction to python
PDF
Datatypes in python
PPTX
Variables in python
PDF
Object oriented approach in python programming
PDF
Introduction to python programming
PPTX
Conditional and control statement
PPTX
Strings in c++
PPT
FUNCTIONS IN c++ PPT
PPTX
Pointers in C Programming
PPTX
Python Flow Control
PDF
Python exception handling
PDF
Strings in Python
PDF
Introduction to Python
Fundamentals of Python Programming
programming with python ppt
Operators in python
Values and Data types in python
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Introduction to python
Datatypes in python
Variables in python
Object oriented approach in python programming
Introduction to python programming
Conditional and control statement
Strings in c++
FUNCTIONS IN c++ PPT
Pointers in C Programming
Python Flow Control
Python exception handling
Strings in Python
Introduction to Python
Ad

Similar to Basic data types in python (20)

PDF
Python cheat-sheet
PPTX
Introduction on basic python and it's application
PPTX
Presentation on python data type
PPTX
Python 3.pptx
PPTX
python
ODP
Python slide.1
PPTX
Python PPT2
DOCX
unit 1.docx
PDF
Python Interview Questions PDF By ScholarHat.pdf
PPTX
Learn about Python power point presentation
PPTX
Introduction to Basics of Python
PPTX
Python basics
PPTX
Python Session - 3
PPTX
PYTHON PPT.pptx python is very useful for day to day life
PDF
Python quick guide
PPTX
Introduction To Programming with Python-3
PDF
E-Notes_3720_Content_Document_20250107032323PM.pdf
PPTX
Python (Data Analysis) cleaning and visualize
PPTX
Improve Your Edge on Machine Learning - Day 1.pptx
PDF
TOPIC-2-Expression Variable Assignment Statement.pdf
Python cheat-sheet
Introduction on basic python and it's application
Presentation on python data type
Python 3.pptx
python
Python slide.1
Python PPT2
unit 1.docx
Python Interview Questions PDF By ScholarHat.pdf
Learn about Python power point presentation
Introduction to Basics of Python
Python basics
Python Session - 3
PYTHON PPT.pptx python is very useful for day to day life
Python quick guide
Introduction To Programming with Python-3
E-Notes_3720_Content_Document_20250107032323PM.pdf
Python (Data Analysis) cleaning and visualize
Improve Your Edge on Machine Learning - Day 1.pptx
TOPIC-2-Expression Variable Assignment Statement.pdf
Ad

More from sunilchute1 (10)

PPTX
Programming construction tools
PPTX
Introduction to data structure
PPTX
Sorting method data structure
PPTX
Introduction to data structure
PPTX
Input output statement
PPTX
Call by value and call by reference in java
PPTX
Java method
PPTX
Constructors in java
PPTX
C loops
PPT
Basic of c language
Programming construction tools
Introduction to data structure
Sorting method data structure
Introduction to data structure
Input output statement
Call by value and call by reference in java
Java method
Constructors in java
C loops
Basic of c language

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Business Ethics Teaching Materials for college
PDF
RMMM.pdf make it easy to upload and study
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
master seminar digital applications in india
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Institutional Correction lecture only . . .
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Supply Chain Operations Speaking Notes -ICLT Program
Week 4 Term 3 Study Techniques revisited.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
Business Ethics Teaching Materials for college
RMMM.pdf make it easy to upload and study
Microbial diseases, their pathogenesis and prophylaxis
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
master seminar digital applications in india
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.

Basic data types in python

  • 1. BASIC DATA TYPES IN PYTHON PROF. SUNIL D. CHUTE HEAD DEPT OF COMPUTER SCIENC M.G. COLLEGE ARMORI
  • 2. BASIC DATA TYPES IN PYTHON  Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
  • 3. One way to categorize these basic data types is in one of four groups:  Numeric: int, float and the less frequently encountered complex  Sequence: str (string), list and tuple  Boolean: (True or False)  Dictionary: dict(dictionary) data type, consisting of (key, value) pairs NOTE: It's important to point out that Python usually doesn't require you to specify what data type you are using and will assign a data type to your variable based on what it thinks you meant. An equally important thing to point out is that Python is a "loosely/weakly typed" programming language, meaning that a variable can change its type over the course of the program's execution, which isn't the case with "strongly typed" programming languages (such as Java or C++).
  • 5. NUMERIC DATA TYPES These data types are fairly straight-forward and represent numeric values. These can be decimal values, floating point values or even complex numbers.  Integer Data Type - int The int data type deals with integers values. This means values like 0, 1, -2 and -15, and not numbers like 0.5, 1.01, - 10.8, etc. If you give Python the following code, it will conclude that a is an integer and will assign the int data type to it: >>> x = 5 >>> type(x) <class 'int'> We could have been more specific and said something along these lines, to make sure Python understood our 5 as an integer, though, it'll automatically do this exact same thing under the hood:
  • 6. >>> x = int(5) >>> type(x) <class 'int‘> It's worth noting that Python treats any sequence of numbers (without a prefix) as a decimal number. This sequence, in fact, isn't constrained. That is to say, unlike in some other languages like Java, the value of the int doesn't have a maximum value - it's unbounded. The sys.maxsize may sound counterintuitive then, since it implies that that's the maximum value of an integer, though, it isn't. >>> x = sys.maxsize >>> x 2147483647 This appears to be a 32-bit signed binary integer value, though, let's see what happens if we assign a higher number to x: >>> x = sys.maxsize >>> x+1 2147483648
  • 7. In fact, we can even go as far as: >>> y = sys.maxsize + sys.maxsize >>> y 4294967294 The only real limit to how big an integer can be is the memory of the machine you're running Python on.  Prefixing Integers What happens when you'd like to pack a numeric value in a different form? You can prefix a sequence of numbers and tell Python to treat them in a different system. More specifically, the prefixes:  0b or 0B - Will turn your integer into Binary  0o or 0O - Will turn your integer into Octal  0x or 0X - Will turn your integer into Hexadecimal
  • 8.  # Decimal value of 5 >>> x = 5 >>> x 5  # Binary value of 1 >>> x = 0b001 >>> x 1  # Octal value of 5 >>> x = 0o5 >>> x 5  # Hexadecimal value of 10 >>> x = 0x10 >>> x 16
  • 9. FLOATING POINT DATA TYPE - FLOAT  The float type in Python designates a floating-point number. float values are specified with a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation Ex.1 >>> 4.2 4.2 >>> type(4.2) <class 'float'> Ex.2 >>> 4. 4.0 Ex.3 >>> .2 0.2 Ex.4>>> .4e7 4000000.0 >>> type(.4e7) <class 'float'> Ex.5>>> 4.2e-4
  • 10. COMPLEX NUMBERS  Complex numbers are specified as <real part>+<imaginary part>j. Ex. >>> 2+3j (2+3j) >>> type(2+3j) <class 'complex'>
  • 11. STRINGS  Strings are sequences of character data. The string type in Python is called str.  String literals may be delimited using either single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the string: Ex.>>> print("I am a string.") I am a string. >>> type("I am a string.") <class 'str'> Ex.>>> print('I am too.') I am too. >>> type('I am too.') <class 'str'>
  • 12.  A string in Python can contain as many characters as you wish. The only limit is your machine’s memory resources. A string can also be empty: Ex. >>> '' ''  What if you want to include a quote character as part of the string itself? Your first impulse might be to try something like this: Ex.>>> print('This string contains a single quote (') character.') SyntaxError: invalid syntax Ex. >>> mystring = "This is not my first String" >>> print (mystring); This is not my first String
  • 13.  to join two or more strings. Ex.>>> print ("Hello" + "World"); HelloWorld Ex. >>> s1 = "Name Python " >>> s2 = "had been adapted " >>> s3 = "from Monty Python" >>> print (s1 + s2 + s3) Name Python had been adapted from Monty Python
  • 14.  use input() function Ex.>>> n = input("Number of times you want the text to repeat: ") Number of times you want the text to repeat: >>> print ("Text"*n); TextTextTextTextText  Check existence of a character or a sub- string in a string Ex.>>> "won" in "India won the match" True
  • 15. PYTHON LIST DATA TYPE  List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.  Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ]. Ex. a = [1, 2.2, 'python'] We can use the slicing operator [ ] to extract an item or a range of items from a list. The index starts from 0 in Python.
  • 17. PYTHON TUPLE DATA TYPE  Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified.  Tuples are used to write-protect data and are usually faster than lists as they cannot change dynamically.  It is defined within parentheses () where items are separated by commas. Ex. t = (5,'program', 1+3j)
  • 18.  We can use the slicing operator [] to extract items but we cannot change its value.
  • 19. PYTHON SET DATA TYPE  Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.  A set is created by placing all the items (elements) inside curly braces {}, separated by comma, or by using the built- in set() function.  It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have mutable
  • 22. PYTHON DICTIONARY DATA TYPE  Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair.  It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.  In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.