Variables and Data Types
'''Variable is like a container that holds data. Very similar to how our containers in kitchen holds sugar, salt etc. Creating a variable is like creating a placeholder in memory and assigning it some value.'''
'''In Python, it is as easy as writing:
a = 1
b = True
c = "Sindhu"
d = None
These are four variables of different data types.'''
a = 1
print(a) #output=1
b = True
print(b) #output=True
c = "Sindhu"
print(c) #output=Sindhu
d = None
print(d) #output=None
'''What is data type?
Data type specifies the type of value a variale holds. This is required in programming to do various operations without causing an error.'''
print(type(a)) #output=<class 'int'> i.e. integer
print(type(b)) #output=<class 'bool'> i.e. boolean
print(type(c)) #output=<class 'str'> i.e. string
print(type(d)) #output=<class 'NoneType'> i.e. none type
#This code is used to find the data type of a variable.
'''
Types of Data Type:
1. Numeric Data: int, float, complex
int: 3, -8, 0
float: 7.349, -9.0, 0.0000001
complex: 6+2i
2. Text Data: str
"Hello World", "Python Programming"
3. Boolean Data:
Boolean data consists of values True or False
4. Sequenced Data: list, tuple
list: A list is an ordered collection of data with elements separated by a comma and enclosed within square brackets.Lists are mutable and can be modified after creation.
Example:[8,2,3,[-4,5],["apples", "bananas"]]
tuple: A tuple is an ordered collection of data with elements separated by a comma and enclosed within paraentheses. Tuple are immutable and cannot be modified after creation
Example: (("parrot" , "sparrow"), ("lion" , "tiger"))
5. Mapped Data: dict
dict: A dictionary is an unordered collection of data containing a key:value pair. The key:value pairs are enclosed within curly brackets.
Example: {"name":" Sindhu", "age":23, "Canvote":True}
'''
a1=24.9
print(a1) #output=24.9
print(type(a1)) #output=<class'float'>
a2=complex(24,9)
print(a2) #output=24+9j
print(type(a2)) #output=<class'complex'>
list1 = [8,2,3 ,[-4,5],["apple", "banana"]]
print(list1) #output=[8,2,3 ,[-4,5],['apple', 'banana']]
tuple1 = (("parrot", "sparrow"), ("lion", "tiger"))
print(tuple1) #output=(('parrot' , 'sparrow'), ('lion', 'tiger'))
dict1 = {"name":"Sindhu" ,"age":23, "Canvote": True}
print(dict1) #output={'name' : Sindhu" , 'age':23, 'Canvote': True}
'''In Python, everything is an object.'''
Comments
Post a Comment