Python Data Types
Python Data Types
Variables can hold values of different data types. Python is a
dynamically typed language hence we need not define the type of the variable
while declaring it. The interpreter implicitly binds the value with its 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.
Consider the following example to define the values of different
data types and checking its type.
A=10
b="Hi Python"
c = 10.5
print(type(a));
print(type(b));
print(type(c));
Output:
<type 'int'>
<type 'str'>
<type 'float'>
Standard data types
A variable can hold different types of values.
For example, a person's name must be stored as a string whereas its id must be
stored as an integer.
Python provides various standard data types that
define the storage method on each of them. The data types defined in Python are
given below.
In this section of the tutorial, we will give a
brief introduction of the above data types. We will discuss each one of them in
detail later in this tutorial.
Numbers
Number stores numeric values. Python creates Number objects when a
number is assigned to a variable. For example;
1. a = 3 , b = 5 #a and b are number objects
Python supports 4 types of numeric data.
1. int
(signed integers like 10, 2, 29, etc.)
2. long (long
integers used for a higher range of values like 908090800L, -0x1929292L, etc.)
3. float
(float is used to store floating point numbers like 1.9, 9.902, 15.2, etc.)
4. complex
(complex numbers like 2.14j, 2.0 + 2.3j, etc.)
Python allows us to use a lower-case L to be used with long
integers. However, we must always use an upper-case L to avoid confusion.
A complex number contains an ordered pair, i.e., x + iy where x
and y denote the real and imaginary parts respectively).
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 there
are various inbuilt functions and operators provided.
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 repetition operator as the operation
"Python " *2 returns "Python Python ".
The following example illustrates the string handling in python.
str1 = 'hello javatpoint' #string str1
str2 = ' how are you' #string str2
print (str1[0:2]) #printing first two character using slice operator
print (str1[4]) #printing 4th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
OUTPUT
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
.
List
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.
Consider the following example.
l = [1, "hi", "python", 2]
print (l[3:]);
print (l[0:2]);
print (l);
print (l + l);
print (l * 3);
Output:
[2]
[1, 'hi']
[1, 'hi', 'python', 2]
[1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
[1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
Tuple
A tuple is similar to the list in many ways. Like lists, tuples
also contain the collection of the items of different data types. The items of
the tuple are separated with a comma (,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the size
and value of the items of a tuple.
Let's see a simple example of the tuple.
t = ("hi", "python", 2)
print (t[1:]);
print (t[0:1]);
print (t);
print (t + t);
print (t * 3);
print (type(t))
t[2] = "hi";
Output:
('python', 2)
('hi',)
('hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2)
<type 'tuple'>
Traceback (most recent call last):
File "main.py", line 8, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment
Dictionary
Dictionary is an ordered 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.
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'};
print("1st name is "+d[1]);
print("2nd name is "+ d[4]);
print (d);
print (d.keys());
print (d.values());
Output:
1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
[1, 2, 3, 4]
['Jimmy', 'Alex', 'john', 'mike']
Python Keywords
Python Keywords are special reserved words which
convey a special meaning to the compiler/interpreter. Each keyword have a
special meaning and a specific operation. These keywords can't be used as
variable. Following is the List of Python Keywords.
|
True
|
False
|
None
|
and
|
as
|
|
asset
|
def
|
class
|
continue
|
break
|
|
else
|
finally
|
elif
|
del
|
except
|
|
global
|
for
|
if
|
from
|
import
|
|
raise
|
try
|
or
|
return
|
pass
|
|
nonlocal
|
in
|
not
|
is
|
lambda
|
Python Literals
Literals can be defined as
a data that is given in a variable or constant.
Python support the
following literals:
I. String
literals:
String literals can be
formed by enclosing a text in the quotes. We can use both single as well as
double quotes for a String.
Eg:
"Aman" , '12345'
Types of
Strings:
There are two types of
Strings supported in Python:
a).Single line String-
Strings that are terminated within a single line are known as Single line
Strings.
Eg:
>>> text1='hello'
b).Multi line String- A
piece of text that is spread along multiple lines is known as Multiple line
String.
There are two ways to
create Multiline Strings:
1).
Adding black slash at the end of each line.
Eg:
>>> text1='hello\
user'
>>> text1
'hellouser'
>>>
2).Using triple quotation marks:-
Eg:
>>> str2='''''welcome
to
SSSIT'''
>>> print str2
welcome
to
SSSIT
>>>
II.Numeric
literals:
Numeric Literals are
immutable. Numeric literals can belong to following four different numerical
types.
|
Int(signed
integers)
|
Long(long
integers)
|
float(floating
point)
|
Complex(complex)
|
|
Numbers( can be both positive and negative) with no fractional
part.eg: 100
|
Integers of unlimited size followed by lowercase or uppercase L
eg: 87032845L
|
Real numbers with both integer and fractional part eg: -26.2
|
In the form of a+bj where a forms the real part and b forms the
imaginary part of complex number. eg: 3.14j
|
III.
Boolean literals:
A Boolean literal can have
any of the two values: True or False.
IV.
Special literals.
Python contains one special
literal i.e., None.
None is used to specify to
that field that is not created. It is also used for end of lists in Python.
Eg:
>>> val1=10
>>> val2=None
>>> val1
10
>>> val2
>>> print val2
None
>>>
V.Literal
Collections.
Collections such as tuples,
lists and Dictionary are used in Python.
List:
- List contain items of different
data types. Lists are mutable i.e., modifiable.
- The values stored in List are
separated by commas(,) and enclosed within a square brackets([]). We can
store different type of data in a List.
- Value stored in a List can be
retrieved using the slice operator([] and [:]).
- The plus sign (+) is the list
concatenation and asterisk(*) is the repetition operator.
Eg:
>>> list=['aman',678,20.4,'saurav']
>>> list1=[456,'rahul']
>>> list
['aman', 678, 20.4, 'saurav']
>>> list[1:3]
[678, 20.4]
>>> list+list1
['aman', 678, 20.4, 'saurav', 456, 'rahul']
>>> list1*2
[456, 'rahul', 456, 'rahul']
>>>
Comments
Post a Comment