Python Data Types :
Python is a dynamically typed language, we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.
Python enables us to check the 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.
Examples :
a=20
b="Hai Python"
c=12.9
print(type(a)) O/p- int
print(type(b)) O/p- str
print(type(c)) O/p- float
Standard Data types :
A variable can hold different types of values. like 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. following are data types defined in python.
- Numbers
- Sequence Type
- Boolean
- Set
- Dictionary
Numbers :
Numbers stores numeric values. The integer, Float and complex values belongs to Python numbers data-type. Python provides the type() function to know the data type of the variables.
isInstance() - it is used to check an object belongs to a particular class.
Python creates number objects when a number is assigned to a variable.
Example:
print("the type of a",type(a))
print("The type of b",type(b))
print("The type of c",type(c))
print("c is a complex number", isinstance(2+4j,complex))
Output-
The type of a int
The type of b float
The type of c complex
c is complex number: True
Python supports three types of numeric data.
Int - Integer value can be any length such as integers 1,2,23,59...-150 etc. Python has no restriction on the length of an integer. Its value belongs to int.
Float - Float is used to store floating point numbers like 1.5, 9.9, 43.4 etc. It is accurate up to 15 decimal points.
Complex - A complex number contains an ordered pair i.e x+iy where x and y denote the real and imaginary parts, respectively. The complex numbers like 3.13j, 4.0+3.4j etc.
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 and triple quotes to define a string.
String handling in python is straightforward task since Python provides built-in functions and operators to perform operations in the string.
+ - The + operator is used to concatenated two strings as the operation "Hi"+"Python" it returns Hi Python.
* - The operator * is known as a repetition operator as the operation "Python"*2 it returns ' Python Python'.
Example :
print(str)
M=""' A multiline
Output -
A multiple
String
No comments:
Post a Comment
Note: only a member of this blog may post a comment.