Python3- Data types1
Python
DATA TYPES:-
In python, every value has a datatypes. Everything is the object in python programming, datatypes are actually classes and variables are instances(object) of these classes.
1. Numeric
2. Boolean
3. Sequence type
1.Strings
2.List
3.Tuple
4. Set
5. Dictionaries.
NUMBERS(NUMERIC):-
In this numeric values are stores in variables which are immutable (the values assigned to a variables can be changed).
* Integers(int) ,float point numbers(float) and complex numbers(complex) are involved in this.
* type() function used to know which class a variable or value belongs to .
* isinstance() function used to check if an object belongs to particular class or not.
* complex() function used to convert integers to complex
eg:-
1.
a=2
print(a, "is of type", type(a)) #type(a) returns the class of a value(a)
Output:- 2 is of type <class 'int'>
2.
a=2.5
print(a,"is of type",type(a))
Output:- 2.5 is of type <class 'float'>
3.
a=3+4j
print(a,"is complex number?")
print(isinstance(a,complex)) #isinstance() returns True if condition is true else False
print(a,"is of type" ,type(a))
Output:- 3+4j is complex number?
True
3+4j is of type <class 'complex' >
BOOLEAN :-
It represents the truth values (True and False).
* If condition is true returns True else returns False.
eg:-
a=True #a is a boolean type.
print(type(a))
Output:- <class 'bool'>
PYTHON STRINGS :-
String is the sequence of Unicode characters .(means it can store special characters and non English characters)
*We can use single or double quotes to represent strings.
eg:-
s= "This is Python Strings." #Here we can use single or double quotes, it returns same output in python
print(s)
print(type(s))
Output:- This is Python Strings.
< class 'str' > #str stands for string.
*If string spans more than one line we can use multi-line strings ,it can denoted using triple quotes,''' or """.
eg:-
s ="""This is Python
Strings."""
print(s)
print(type(s))
Output:- This is Python
Strings.
< class 'str' >
* A String in python consists of a series or sequence of characters-letters, numbers, and special characters.
* Strings can be indexed-often synonymously called Subscripted as well.
* Similar to C, the first character of a string has the index 0.
eg:-
a="PYTHON"
print(a) #returns a value.
print(a[0]) #returns first letter in a value.
print(a[len(a)-1]) #returns last letter ,len() returns the length of the value (a[5]).
print(a[0:4]) #returns the letters from 0 to 3 (index 4 can't be considered).
print(a[-1]) #returns last letter .
print(a[:]) #returns a value because no limits .
print(a[4]) #returns the letter of index 4.
Output:-
PYTHON
P
N
PYTH
N
PYTHON
O
* str() function used to convert integers to strings.
* int() function used to convert strings to integers.
eg:-
a=1
print(type(a))
a=str(a)
print(a)
print(type(a))
Output:-
< class 'int' >
1
< class 'str' >
* ord() function returns the number representing the Unicode code of a specified character(ASCII values) .
syntax:::- ord(character)
eg:-
x=ord("h")
print(x)
Output:-
104
Comments
Post a Comment