Sunday, May 16, 2021

Data types in python

Data types in python.

Python has so many in-built data types- int,float,complex,set,frozenset,string,boolean,list,tuple,dictionary,byte,bytearray,memoryview.

 

1. Lists: Used to get series of data as input.Written within square brackets.[]

Declaration:  my_list=[]

 

If you are familiar with C,C++ or Java , here are some differences between list and arrays while getting input.

  1. int array[101]; This is the way of declaring array in C. As we can see, we have specified the data type as int and size as 101. So this array can take only integer values. And can take only 101 integer values.
  2. my_list=[i for i in input().split(" ")]  
       Here we do not mention data type as well as size.

       List can take any type of data as input.

       Split method takes an input into list, when separated by a space.

       input:Hello world          

      then list will be ["Hello","world"]  Here,length=2

        But,with input: Helloworld

       list will contain single element ["Helloworld"]  Here,length=1                                            


2.Tuples:Whenever data is comma separated, it will be taken as tuples.

Declaration: my_tuple=()

Tuples are immutable - Values in tuples cannot be changed.

my_tuple=(10,11,12,13) 

 

3.Dictionary: Dictionary datatype stores data as key,:value pair.

Represented within flower brackets.


Declaration: my_dictionary={}

my_dictionary={"name":"Jack",

                            "age":24,

                            "winning_place":1

                          

 

4.String: Strings are series of characters.Represented within double quotes.

Declaration: my_string=" "

this creates an empty string.

 

  • my_string=input() gets input from user
  • strip() method remove leading and trailing spaces in the input.

The above example is a single line statement.

For entering multiple line sentence, use triple quotes.

Example:  my_string='''This is 

                                      multiple line                        

                                      sentence.'''

       

No comments:

Post a Comment

Anaconda Installation

In this post we will discuss about Anaconda and its installation. Anaconda comes with number of applications(jupyter notebook included), dat...