0% found this document useful (0 votes)
18 views

Different Datatypes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Different Datatypes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Different Data types

Understanding Types of Data Types


Computer programming is all about processing data. In computer programming, the data is always represented in the
binary form (0's and 1's), i.e. groups of Bits called as Bytes.

In the real world, we come across different types of data like age of a person(integer), number of rooms in a house
(integer), price(floating-point number), height (floating-point number), Names of people, places and things (strings),
inventory list (list) etc.

Data types categorize data for efficient processing in Python and all programming languages. They assign specific
characteristics to data, enhancing accuracy and control in operations.

The data type of the data determines :

possible values it can be assigned.

possible operations that can be performed. (Arithmetic operations can be applied on numeric data and not strings)

the format in which it is stored in the memory.

the amount of memory allocated to store the data.

Some of the built-in data types supported in Python are:

Number

String

List

Tuple

Set

Dictionary

In Python, each value possesses a data type. A variable in Python is capable of holding a value of any data type.

The same variable in Python can refer to data of different data types at different times. Thus, variables in Python are not
strongly typed, implying that you don't have to explicitly declare the data type of a variable when creating it.

Let us consider an example:

a = 5 # variable a now refers to Integer data type

a = 90.45 # variable a now refers to Float data type

a = "India" # variable a now refers to String data type

a = [5, 90.45, "India"] # variable a now refers to List data type

We use the built-in function type() to determine the type of data that a variable is referring to at any given point in time.

The syntax of the type() function is as follows :

type(variable_name)
returns the data type of variable_name

Variable Assignment Output of print(type(a))

a = 345 <class, 'int'>

a = 34.56 <class, 'float'>

a = "India" <class, 'str'>

a = [5, 90.45, "India"] <class, 'list'>

Select all the correct Statements from the given options.

Numbers

Numbers
Numbers

We have three different categories of numbers in Python. They are :

int

float

complex

1.int:

It stands for integer. This Python data type stores signed integers.

In Python an integer can be of any length, with the only limitation being the available memory.

Example 1:

# type() function finds the class the variable belongs.

a = -7

print(type(a)) # will print output as follows

<class 'int'>

Example 2:

a = 12536984596965565656236534754587821564

print(type(a)) # will print output as follows

<class 'int'>

2.float:

It stands for floating-point numbers. This Python data type stores floating-point real values.

For example : An int can only store the number 20, but float can also store numbers with decimal fractions like 20.25.

Example:

a = 3.0

print(type(a)) # will print output as follows


<class 'float'>

3.complex:

It stands for complex numbers. A complex number is a combination of a real number and an imaginary number.

It takes the form of a + bj Here, a is the real part and b*j is the imaginary part.

Example

a = 2 + 3j # It is important to note that there should not be any space between 3


and j

print(type(a)) # will print output as follows

<class 'complex'>

Write the missing code in the given program to know which class the given variable a belongs to.

Strings

String data type


In Python, a string is a sequence of characters enclosed inside a pair of single quotes(‘) or double quotes(“). Even triple
quotes (''') are used to represent multi-line strings.

The computer doesn’t see letters at all. Every letter you use is represented by a number in memory.

For example, the letter A is actually the number 65. This is called encoding. There are two types of encoding for characters
– ASCII and Unicode.

ASCII uses 8 bits for encoding whereas Unicode uses 32 bits. Python uses Unicode for character representation.

An individual character within a string is accessed using an index.

Index starts from 0 to n-1, where n is the number of characters in the string.

Python allows negative indexing in strings. The index of -1 refers to the last item in the string, -2 refers to the second last
item and so on.

In Python, a string of length 1 is treated as a character.

Ways of creating strings in Python are:

1. Using single quotes (' ')

str = 'Welcome To Python World'

print(str)# will print output as follows

Output :

Welcome To Python World


If single quote (') is part of the string, then the characters of the string are enclosed between double quotes(").

sentence = "Welcome to Python's world"

print (sentence) # will print output as follows

Output :

Welcome to Python's world

If single quote (') is part of the string, and the characters are enclosed between single quotes, then a backslash (\) should
be added before (') of the string.

The other way of having a single quote in the string content and enclosing the string within single quotes is using a
backslash(\)

sentence = 'Welcome to Python\'s world' # observe a backslash (\) before (')

print (sentence) # will print output as follows

Output :

Welcome to Python's world

2. Using double quotes (" ")

str = "Welcome to the world of Python"

print(str) # will print output as follows

Output:

Welcome to the world of Python

3. Using triple double quotes(""" """)

We use triple double quotes(""" """) to create multi-line strings in Python.

str = """Welcome to the world of Python

I love Python and Python is very easy to learn

Practice makes man perfect"""

print(str) # will print output as follows

Output :

Welcome to the world of Python

I love Python and Python is very easy to learn


Practice makes man perfect

We can also use triple single quotes(''' ''') to create multi-line strings in Python.

str = '''Welcome to the world of Python

I love Python and Python is very easy to learn

Practice makes man perfect'''

print(str) # will print output as follows

Output :

Welcome to the world of Python

I love Python and Python is very easy to learn

Practice makes man perfect

Note: Using triple single quotes or triple double quotes, we can also create
single line strings.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy