Problem Solving and Python Programming: UNIT II: Data Types, Expressions, Statements

Values and Types

Python Programming

• In Python there are five types of data type that are used commonly and those are -

Values and Types

• Data types are used to define type of variable.

• In Python there are five types of data type that are used commonly and those are -


Fig. 2.7.1 Data types in Python

 

1) Numeric: 

Python numeric data type is used to hold numeric values like;

• int - holds signed integers of non-limited length.

• long- holds long integers

• float- holds floating precision numbers and it's accurate upto 15 decimal places.

• complex- holds complex numbers.

In Python we need not to declare datatype while declaring a variable like C or C++. We can simply just assign values in a variable. But if we want to see what type of numerical value is it holding right now, we can use type(). For example -



2) String :

• String is a collection of characters.

• In Python, we can use single quote, double quote or triple quote to define a string.

•We can use two operators along with the string one is + and another is *.

• The + operator is used to concatenate the two strings. While * operator is used as a repetition operation. Following execution illustrates it -


 

3) List:

• It is similar to array in C or C++ but it can simultaneously hold different types of data in list.

• It is basically an ordered sequence of some data written using square brackets([]) and commas(,)

For example -


 

4) Tuple :

• Tuple is a collection of elements and it is similar to the List. But the items of the tuple are separated by comma and the elements are enclosed in () parenthesis.

• Tuples are immutable or read-only. That means we can not modify the size of tuple or we cannot change the value of items of the tuple.

• Here are examples of tuples -


 

5) Dictionary:

• Dictionary is a collection of elements in the form of key:value pair.

• The elements of dictionary are present in the curly brackets.

• For example -


 

Problem Solving and Python Programming: UNIT II: Data Types, Expressions, Statements : Tag: Engineering Python : Python Programming - Values and Types