Previous Variables in Python
In python programming language everything is an object and data type tells us which types of value the object is.
Data types are classified into five types in Python programming language, they are:-
Best Laptop for Web Development in 2021.
- NUMERIC DATATYPES
- BOOLEAN DATATYPES
- DICTIONARY
- SEQUENCE DATATYPE
- SET
type() method
Type method is used to know what type of data we are using.
Numeric Data Types
Numeric type consists of numerical value data types for example whenever we assign a decimal value or numbers to variables then it comes under numeric data types.
It contains :
- int
- float
- complex type
Int or Integer
It is a data type where the value given by the user can be positive or negative and can be of any length that means it can be a big integer or small integer but it should not be a fraction or a decimal value.
#Python Program to
# demonstrate int value
p=5556677
print(type(p))
q=-45
print(type(q))
#output
<class 'int'>
<class 'int'>
Here in the above example code both p and q are of same type int.
Float
This is the data type, where the value given by the user can be positive or negative and at any length it can be a large value or a small value, but the difference between an integer and a float is that the float accepts only fractional values or decimal values, all decimals fall under the float datatype
# Python program to
# demonstrate float values
a=1234.56
print('Type of a:-',type(a))
b=-8.97
print('Type of b:-',type(b))
c=1.4
print('Type of c:-',type(c))
#OUTPUT
Type of a:- <class 'float'>
Type of b:- <class 'float'>
Type of c:- <class 'float'>
Complex Number
Complex numbers are represented by complex class; it is a combination of real part and imaginary part.
# Python Program to
# demonstrate complex values
a=1+2j
print('Type of a:-',type(a))
b=-5+3j
print('Type of b:-',type(b))
c=-6-2j
print('Type of c:-',type(c))
#OUTPUT
Type of a:- <class 'complex'>
Type of b:- <class 'complex'>
Type of c:- <class 'complex'>
Boolean Data Type
Boolean data type is actually used in a code or in some statements to return either true or false.It only returns true or false based on the given condition.
bool() method
# Python Program to
# demonstrate bool values
a=4<1
print('a is ',bool(a))
b=3>2
print('b is ',bool(b))
#OUTPUT
a is False
b is True
From the above code we can say four(4) is not less than one(1) in a. so, a is returned as false and in b three(3) is greater than two(2) , that statement is correct. so, it is true.
Dictionary
Random, changeable and collection of items in Indexed. The elements are placed in curly brackets separated by commas and Dictionaries have {key: value} attached, values can be accessed using the keys. Also, we can use the index to access the values.
data={1:'Python',2:'Java',3:'C Lang'}
print(data)
print(type(data))
#OUTPUT
{1: 'Python', 2: 'Java', 3: 'C Lang'}
<class 'dict'>
Accessing Values
There are two methods to access values they are:-
- Using keys.
- Using get() method.
We can access the values in the dictionary using keys in the square brackets, if the given key is absent it shows an error.
data={1:'Python',2:'Java',3:'C Lang'}
print(data) #prints total dictionary
print(data[2]) #prints Java
print(data[5]) #prints error
#OUTPUT
{1: 'Python', 2: 'Java', 3: 'C Lang'}
Java
Traceback (most recent call last):
File "C:/Users/DELL/OneDrive/Desktop/demo.py", line 4, in <module>
print(data[5])
KeyError: 5
In the above code it prints total dictionary from the first print statement, and from the second print statement it prints 'Java' which is in the second key but the third print statement prints error because key five (5) is not present in the above dictionary that contains only 3 keys and 3 values.
Using get() method
There is a another method to access the values in dictionary that is get() method, Using get() method we can access the values if the key is not found it shows nothing (none).Here the example code is given observe carefully.
data={1:'Python',2:'Java',3:'C Lang'}
print(data) #prints total dictionary
print(data[2]) #prints Java
print(data.get(4)) #prints none
#OUTPUT
{1: 'Python', 2: 'Java', 3: 'C Lang'}
Java
None
If we observe in get() method the above example, first two print statements execute without errors but the third print statement prints "None" because the dictionary contains only 3 key:value pairs.
Adding Elements
We can add elements to the dictionary following the syntax:
Syntax : variable[key]=Value
data={1:'Python',2:'Java',3:'C Lang'}
print(data) #prints data contains 3 elements
data[4]='sql' #4th element added to the data
print(data) #prints data contains 4 elements
#OUTPUT
{1: 'Python', 2: 'Java', 3: 'C Lang'}
{1: 'Python', 2: 'Java', 3: 'C Lang', 4: 'sql'}
If we observe the above example given, the first print statement prints the data that contains three elements before we adding the fourth element. The second print statement prints data contains four elements because in the line 3 we added a fourth key and value name 'sql' so finally it prints 4 elements that we added to the dictionary called data.
Removing Elements
As we have added the elements to the data(dictionary) also we can delete the particular element or elements from the dictionary using a keyword called del.
data={1:'Python',2:'Java',3:'C Lang'}
print('Before adding the 4th element',data)
data[4]='sql' #4th element added to the data
print('After adding the 4th element',data)
del data[4] #delets the 4th key:value pair
print('After deleting the 4th element',data)
#OUTPUT
Before adding the 4th element {1: 'Python', 2: 'Java', 3: 'C Lang'}
After adding the 4th element {1: 'Python', 2: 'Java', 3: 'C Lang', 4: 'sql'}
After deleting the 4th element {1: 'Python', 2: 'Java', 3: 'C Lang'}
Previous Variables in Python
Comments
Post a Comment