Skip to main content

DATA TYPES IN PYTHON | (Numeric,Boolean, Dictionary)

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.

  1. NUMERIC DATATYPES
  2. BOOLEAN DATATYPES
  3. DICTIONARY
  4. SEQUENCE DATATYPE
  5. 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'>

In the Above  code a,b, and c are of type 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.
Using keys
        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'}






















Comments

Popular posts from this blog

Types of Operating System

 Types of Operating System Batch Operating system Time sharing Operating system Distributed Operating system Network Operating system Real Time Operating system Multi programming/Processing/Tasking Operating system Mobile Operating System Batch Operating System               In a Batch operating system the similar jobs are grouped together into batches with the help of some operators and these batches are executed one by one. For example, suppose we have ten programs, some are written in c++ programming language, Some are written in c programming language and the rest are written java programming language. Now, every time we run these programs one by one, we have to load the compiler of that particular language and then run the code. But if we do a batch of these ten programs, The advantage with this approach is that for the c++ batch, you only need to load the compiler once. Similarly, for Java and C , the compiler only needs to load once and the entire batch is executed. The followin

Python Variables

variables,the name itself implies that a variable is something which can change.  Variables are like containers for storing  our data values in memory. we can change the data in variables, in python everything is an object and variables are like names given to the object, by labeling them we can easily access them. In other high level languages like C programming language, c++ programming language, and java programming language. etc.,   we need to declare the datatype of the variable , but in python it will automatically recognize the type of data you are storing in a variable. ** Remember that a variable is a name which is given to a value not to the memory. Declaring a variable is very easy you need to assign a value or a string using '=' operator. >>> p=9 >>> print(p) 9 Assigning a single value to multiple variables We can assign a single value to multiple variables.  >>> a=b=c=56 >>> a 56 >>> b 56 >>> c 56 Assigning diff

Even or Odd Number and Factorial of a Number in Python Programming

1. To Check if the given number is even or odd If a number is completely divided by 2 then it is even number. When the number is divided by 2, we use the remainder operator %  to calculate the remainder. If the remainder is not zero, the number is odd. SOURCE CODE:- n= int ( input ( "enter any number:- " )) if (n% 2 == 0 ): print (n , 'is even number' ) else : print (n , 'is odd number' ) OUTPUT 1st Run enter any number:- 2 2 is even number 2nd Run enter any number:- 3 3 is odd number In the above code we use "Simple if" statement.  In the above program, we ask the user for input and check if the number is odd or even. 2.Factorial number Factorial of a number defined as the product of all numbers from 1 to given number. For example  5!(5 factorial)=5*4*3*2*1 is 120. Note = factorial of 0 is 1 SOURCE CODE n= int ( input ( "Enter any number:- " )) fact= 1 for i in range ( 1 , n+ 1 ): fact=fact*i print ( 'factorial of' ,

Functions of Operating System

 Operating System is an interface between the user and the hardware and enables the interaction of a computer's hardware and software.          Also, an operating system is software that performs all the basic functions of file management, storage management, process management, managing input and output, and controlling peripherals such as disk, drivers and printers. Functions of Operating System Device Management  File Management Memory Management Process Management Mastermind Storage Management Handling input/output operations Security Functions of Operating System Device Management          Operating system manages device communication via their respective drivers. It does the following activities for device management: Keeps tracks of all devices, input/output controller is responsible for this task. Decides which process gets the device when and for how much time Allocates the device in an efficient way. De-allocates devices. File Management           The operating system al

Computer System Architecture

 Based on number of general purpose processors computer systems are classified into three types.They are: Single Processor Systems. Multiprocessor Systems. Clustered Systems. Single Processor Systems From the name it is very clear that it has only single processor. A major central processing unit that can execute a set of general utility instructions, including user process instructions. There are some other special purpose processors present that performs device specific task. In the single processor system apart from the main central processing unit(CPU) there are also other processors which are present which does not do the general purpose task but it performs some device specific task. It means that we have a certain devices in our computers like keyboard, disk, etc. for all this they may be some microprocessor  which is specified to do a specific task related to that device like for example keyboard, when we press a key on our keyboard the keystroke has to be converted to some kin

Contact form

Name

Email *

Message *