alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - Things to know before proceed - Datatype Tutorial

Every variable in python has a datatype.  Python interpreter will automatically detect the data type of variable.

In python, we can also use type() to check the data type of a variable i.e which class a variable belongs to.

Similarly, isinstance() is used to check whether an object belongs to a particular class or not.

a=20

b="Hello Fresherbell"

c=20.339

d=True

print(type(a))

print(type(b))

print(type(c))

print(type(d))

Run Program

 

Output

<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>

 

Type Of Datatype in python

  • Number

Python numbers are categorized in int, float, and complex

a=20

b=20.339

c=7+8j

print("a is type of ",type(a))

print("b is type of ",type(b))

print("c is type of ",type(c))

print("c is a complex type : ",isinstance(c,complex))

Run Program

 

  • List

A list is a set of ordered sequences. It is a data type, which is used to store different datatype. The item in the list are separated with a comma “,” and square brackets “[]”.

a  = [1, 4.022, "Hello Fresherbell", True]

print(type(a))

print(a)

Run Program

 

We can also slice the list using the ' : ' operator.  Slicing is used to get a particular value from the list. And its indexing starts from 0.

For example, if we want a value starting from the 2nd index

a  = [1, 4.022, "Hello Fresherbell", True]

print(type(a))

print (a[2:])

Run Program

 

Output:

<type 'list'>

['Hello Fresherbell', True]

 

The list can also be concatenated using the + operator

a  = [1, 4.022, "Hello Fresherbell", True]

print(type(a))

print (a + a) 

Run Program

 

Output

<type 'list'>

[1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True]

 

The list can also be repeated using the * operator

a  = [1, 4.022, "Hello Fresherbell", True]

print(type(a))

print (a * 3) 

Run Program

 

 

Output:

<type 'list'>

[1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True]

 

 

  • Tuple

A tuple is similar to a list. It is a set of ordered sequences. It is a data type, which is used to store different datatype. The item in the tuple are separated with a comma “,” and parenthesis“()”.

 

But in a tuple, we cannot modify the value and data after it is been declared.

a  = (1, 4.022, "Hello Fresherbell", True)

print(type(a))

print (a) 

Run Program

 

Output

<type 'tuple'>

(1, 4.022, 'Hello Fresherbell', True)

 

Use Of Slicing in the tuple

a  = (1, 4.022, "Hello Fresherbell", True)

print(type(a))

print (a[3:])

Run Program

Output:

<type 'tuple'>

(True,)

 

Tuple cannot be modify

a  = (1, 4.022, "Hello Fresherbell", True)

print(type(a))

print (a)

a[2]  = False

Run Program

 

It will throw an error

TypeError: 'tuple' object does not support item assignment

 

Tuple can also be concatenated using + operator

a  = (1, 4.022, "Hello Fresherbell", True)

print(type(a))

print (a + a)

Run Program

 

Output

 

<type 'tuple'> (1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True)

 

Tuple can also be repeated using * operator

a  = (1, 4.022, "Hello Fresherbell", True)

print(type(a))

print (a * 2)

Run Program

 

Output:

<type 'tuple'>

(1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True)

 

  • Dictionary

Dictionary is a set of unordered key-value pairs of items. It is a data type, which is used to store a specific value with its key. The item in the dictionary are separated with a comma “,” and curly brackets “{}”.

 

For example

a  = {1:"Hello",2:2,3:4.02,4:"fresherbell",5:True}

print(type(a))

print (a)

print (a[3])

Run Program

 

 

Output

<type 'dict'>

{1: 'Hello', 2: 2, 3: 4.02, 4: 'fresherbell', 5: True}

4.02

 

Dictionary has a pre-defined function for getting keys and values separately


a  = {1:"Hello",2:2,3:4.02,4:"fresherbell",5:True}

print(type(a))

print (a.keys())

print (a.values())

Run Program

Output

<type 'dict'>

[1, 2, 3, 4, 5]

['Hello', 2, 4.02, 'fresherbell', True]

 

  • Boolean

Boolean has two built-in values i.e True and False. It denotes by class bool. It should start with a capital letter

a  = True

b  = False

print(type(a))

print(type(b))

Run Program

 

Output

<type 'bool'>

<type 'bool'>

 

  • String

The string is formed using multiple Unicode characters. It can be used within a single quote or double quote.

a = "Welcome to Python" #for single line we can use ''  or ""

print(a)

a = '''Welcome to

Fresherbell'''          #for multi line we can use '''  or """

print(a)

Run Program

 

Output:

Welcome to Python

Welcome to

Fresherbell

 

The string is immutable. Therefore we cannot change the value of the string once it's declared.

a = "Welcome to Fresherbell"

a[0]="D"

print(a)

Run Program

 

Output:

a[0]="D"

TypeError: 'str' object does not support item assignment

 

We can also use the slicing operator [] in the string for example

a = "Welcome to Fresherbell"

print(a[0])

print(a[11:])

Run Program

 

Output:

W

Fresherbell

 

  • Set

Set is an unordered collection of unique items. It is iterable and mutable. The item in the set are separated with a comma “,” and curly brackets “{}”.

a = {3,"Hello",2,7,"Fresherbell",9}

print(a)

print(type(a))

Run Program

Output:

{2, 3, 7, 9, 'Fresherbell', 'Hello'}

<class 'set'>

 

Set eliminate duplicate items in it.

a = {3,3,3,2,2,7,8,8,8,9}

print(a)

print(type(a))

Run Program

 

Output:

{2, 3, 7, 8, 9}

<class 'set'>

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.