Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Python Programming. No Comments on Types in Python Programming
0
(0)

This article provides a comprehensive overview of different types in Python programming language along with relevant examples.

 

Introduction to Types in Python

In general, the structure of a Python program is as follows:

  • Programs are composed of modules.
  • Modules contain statements.
  • Statements contain expressions.
  • Expressions create and process objects.

 

In Python, everything is an object. Including values. Even simple numbers qualify, with values (e.g., 99), and supported operations (addition, subtraction, and so on). In Python, data takes the form of objects—either built-in objects that Python provides, or objects we create using Python classes or external language tools such as C extension libraries.

 

Following table shows fundamental Python’s built-in object types and some of the syntax used to code their literals—that is, the expressions that generate these objects:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Object Type Examples
Numbers 12, 2.67, 6+8j, 0b1011
Strings ‘hai’, “hello”, “Python’s Features”, str(‘Python’)
Lists [1,2,3], [1,2,’three’], list(range(10)), list(‘hai’)
Tuples (1,2,3), (1,2,’three’), tuple(range(10)), tuple(‘hai’)
Sets {1,2,3}, set(‘hai’)
Dictionaries {‘Mon’:1, ‘Tue’:2, ‘Wed’:3}, dict(hours=10)

 

Following are some more types available in Python:

 

Object Type Examples
Files open(‘abc.txt’)
Functions def, lambda
Modules import, __module__
Classes objects, types, metaclasses
None None
Booleans True, False

 

In this article we will learn about Number and Boolean types. We will learn about other types in other articles.

 

Numbers

 

Numbers include the following:

  • integers
  • floating-point numbers
  • complex numbers
  • decimals with fixed precision
  • rational numbers with numerator and denominator
  • sets

 

Using third party extensions we have more types like matrices, vectors, etc.

 

Following are examples of numeric literals and constructors:

 

Literal Description
12, -887, 0, 999999999999999999999998 Integers (unlimited size)
1.23, 1., 3.14e-10, 4E210, 4.0e+210 Floating-point numbers
0o177, 0x9ff, 0b101010 Octal, hex, and binary literals
6+4j, 2.0+9.0j, 7J Complex number literals
Decimal(‘13.0’), Fraction(4, 7) Decimal and fraction extension types
bool(X), True, False Boolean type and constants

 

Floating-point numbers are implemented as C doubles in standard Cpython. The functions hex, oct, and bin can be used to convert integers to hexadecimal, octal, and binary formats respectively.

 

Complex numbers are internally implemented as pairs of floating-point numbers. Complex numbers can be ended with j or J. Complex numbers can also be created with complex(real, imag) function call.

 

We can use built-in functions like: pow, abs, round, int, hex, bin, etc on numbers. We can also use utility modules like random, math as follows:

 

>>> import random
>>> random.random()
0.865758460309634
>>> random.choice(range(100))
96

 

>>> import math
>>> math.sqrt(7)
2.6457513110645907

 

Decimals

 

Decimals are fixed precision floating point numbers. Decimals can be precise up to n digits after the decimal point. For example, 0.1+0.1+0.1-0.3 gives 5.551115123125783e-17. In such cases, we can use decimal as follows:

 

>>> from decimal import Decimal
>>> x=Decimal('0.1')
>>> y=Decimal('0.3')
>>> x+x+x-y
Decimal('0.0')

 

Default precision for decimal is 28 digits. We can set the precision as follows:

 

>>> import decimal
>>> decimal.Decimal(1) / decimal.Decimal(7) 
Decimal('0.1428571428571428571428571429')

 

#Set precision to 4 digits
>>> decimal.getcontext().prec = 4 
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal('0.1429')

 

Fraction

 

Fraction objects are used to implement rational numbers. It keeps both numerator and denominator explicitly. Following are examples for working with fractions in Python:

 

>>> from fractions import Fraction
>>> x=Fraction(2,7)
>>> y=Fraction(3,7)
>>> x+y 
Fraction(5, 7)
>>> x*y
Fraction(6, 49)
>>> print(x)
2/7

 

Booleans

 

Python provides Boolean data type called bool with values True and False. True represents integer 1 and False represents integer 0. bool is a sub class of integer class. Following are examples on Booleans:

 

>>> True == 1
True

>>> True is 1
False

>>> True and False
False

>>> True + 10
11

>>> False * 9
0

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *