Python data types, String data type and Number data types
<Comment Arithmetic Operators >
Data type used to reserve some memory space for variable. Python variables do not need an explicit declaration. Hence, the variable s are declared itself at the time of assigning value to the variable.
Finding Data Type
We can get data type of any variable or any object by using type() function.
For example:
>> a=3
>> print(type(a))
<class 'int'>
>> b='hello'
>> print (type(b))
<class 'str'>
Python supports different data types
- Number data type
- String Data Type
- Boolean Data Type
- Set Data Type
- Dictionary Data Type
In this page, we are going to discuss the basic data types used in the python programming.
Python Number data type
Number data type assigns numerical value to the variable.
Python supports four different number data types.
Data type | Description | Example |
---|---|---|
int | Integer data type | >>> no =73 >>>print(no) 73 |
float | Floating point data type | >>>print(124.23) 124.23 |
complex | Complex number | >>>val= 2.34j >>>A=3+4j |
long | Long integer | >>>123456L |
Note: A complex number consists of two parts x+jy that real and imaginary parts A long number is identified by the letter l or L. Usually, the upper case letter 'L' is used with a long inter number.
Python String Data Type
- Python string is a sequence of characters enclosed in the quotation marks.
- The Python string allows either pair of single(' ') or double(" ") quotes marks.
- We can get the substring by using the slice operator ([],[:]).
- The string is read by using indexes starting at 0 indexing and ending at n-1 indexing.
- The string uses plus( + ) sing as a concatenation operator and asterisk ( * ) as a repetition operator.
For example: >> print('how are you' ) How are you >>st='hello ' >> print(st[0]) # print the first letter h >> print(st[0:2]) # print the letter start at 0 index and end at 1(2-1) indexing. he >> print(st+'\t welcome') hello welcome >> print(st*2) hellohello
- The strings are an immutable sequence of data types. That means,you cannot change the string value.
# For example:
If you want to assign a new value ('w') to the index p[0],you will get TypeError
>> p[0]='w'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Boolean Data Type
- Python supports the Boolean data type.
- The Boolean data type has two values: True and False.
- The technical name of Boolean data type is bool.
- If you want to use conditional expressions or comparison operators more often in your program, these statements will return the result as Boolean values.
The following example shows how to assign Boolean values to the variables in the program.
>>a = True
>> b = False
>> print(a,b)
True False
The bool() function is used to check if the value is True or False.
>>bool(0)
False
>>bool(10)
True
>>bool('hello')
True
>>bool('')
False
>>bool(' ') # space given between single quotes
True
>>bool([])
False
<Comment Arithmetic Operators >