Skip to document

Python Data Types - qwerftghj

qwerftghj
Course

Programming with PYTHON (CM5101)

9 Documents
Students shared 9 documents in this course
Academic year: 2022/2023
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Government Polytechnic, Pune

Comments

Please sign in or register to post comments.

Preview text

Python Data Types

Variables can hold values, and every value has a data-type. Python

is a dynamically typed language; hence we do not need to define

the type of the variable while declaring it. The interpreter implicitly

binds the value with its type

Example :

a = 5

The variable a holds integer value five and we did not define its

type. Python interpreter will automatically interpret variables a as an

integer type.

Python enables us to check the type of the variable used in the

program. Python provides us the type() function, which returns the

type of the variable passed.

Numeric Data Type :

1) Numbers

Number stores numeric values. The integer, float, and complex values

belong to a Python Numbers data-type. Python provides the type() function

to know the data-type of the variable. Similarly, the isinstance() function is

used to check an object belongs to a particular class.

Python creates Number objects when a number is assigned to a variable. For

example;

Sequence Type

String

The string can be defined as the sequence of characters represented in the

quotation marks. In Python, we can use single, double, or triple quotes to

define a string.

String handling in Python is a straightforward task since Python provides

built-in functions and operators to perform operations in the string.

In the case of string handling, the operator + is used to concatenate two

strings as the operation "hello"+" python" returns "hello python".

The operator * is known as a repetition operator as the operation "Python"

*2 returns 'Python Python'.

List

Python Lists are similar to arrays in C. However, the list can contain data of

different types. The items stored in the list are separated with a comma (,)

and enclosed within square brackets [].

We can use slice [:] operators to access the data of the list. The

concatenation operator (+) and repetition operator (*) works with the list in

the same way as they were working with the strings.

Dictionary

Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table
where each key stores a specific value. Key can hold any primitive data type, whereas value is an
arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.
Consider the following example.

Boolean

Boolean type provides two built-in values, True and False. These values are used to determine the
given statement true or false. It denotes by the class bool. True can be represented by any non-zero
value or 'T' whereas false can be represented by the 0 or 'F'. Consider the following example.

Set

Python Set is the unordered collection of the data type. It is iterable,

mutable(can modify after creation), and has unique elements. In set, the

order of the elements is undefined; it may return the changed sequence of

the element. The set is created by using a built-in function set(), or a

sequence of elements is passed in the curly braces and separated by the

comma. It can contain various types of values. Consider the following

example.

Was this document helpful?

Python Data Types - qwerftghj

Course: Programming with PYTHON (CM5101)

9 Documents
Students shared 9 documents in this course
Was this document helpful?
Python Data Types
Variables can hold values, and every value has a data-type. Python
is a dynamically typed language; hence we do not need to define
the type of the variable while declaring it. The interpreter implicitly
binds the value with its type
Example :
a = 5
The variable a holds integer value five and we did not define its
type. Python interpreter will automatically interpret variables a as an
integer type.
Python enables us to check the type of the variable used in the
program. Python provides us the type() function, which returns the
type of the variable passed.