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.
- 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.
- my_list=[i for i in input().split(" ")]
List can take any type of data as input.
Split method takes an input into list, when separated by a space.
input:Hello world
But,with input: Helloworld
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