python-basics
Day13
I installed the latest version of Python from the official website, https://www.python.org/downloads, and verified the version with "python —version."
Python data types are used to define a variable's type. It specifies the type of data that will be stored in a variable. The data stored in memory can be of many types. A person's age, for example, is stored as a numeric value, while his or her address is stored as alphanumeric characters.
Python has various built-in data types, which we will discuss in this blog:
1)Numeric data types
2)String data types
3)Sequence types
4)Mapping data type
5)Boolean type
6)Set data types
1) Numeric data types:- The numeric data type in Python represents the data that has a numeric value. A numeric value can be an integer, a floating number, or even a complex number.
for example :- var1 = 1 var2 = 15 var3 = 3.856
PermalinkPython supports four different numerical types −
i) int: This value is represented by the int class. It contains positive or negative whole numbers (without fractions or decimals). In Python, there is no limit to how long an integer value can be.
ii) float: This value is represented by the float class. It is a real number with a floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.
iii) complex:- Complex number is represented by a complex class. It is specified as (real part) + (imaginary part)i.
For example – 2+3i
2) String data types:- Python Strings are identified as a contiguous set of characters represented in quotation marks. Python allows for either a pair of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 at the beginning of the string and working their way from -1 at the end. The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator in Python.
For example: str_demo = "Hello, world!"
print (str_demo) #print complete string
print (str_demo[0]) #print 0th character of string
print (str_demo[2:5]) #print characters starting with 3rd to 5th(exclusive)
print (str_demo * 2) #prints string two times
print (str_demo + " Hello Devops") # string concatenation
This will produce the following result:
Hello, world!
H
llo
llo world!
Hello, world!Hello, world!
Hello, world! Hello Devops
3)Sequence types:-
i) List Data Type:- Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.
for example:-
list_demo = [ 'abcd', 123, 4.5, 'pqr', 982.545 ]
list2 = ['Hello', 123]
print (list_demo) # Prints complete list
print (list_demo[0]) # print first element of list
print (list_demo[1:3]) #prints element index of 1 to 3(exclusive) print (list_demo + list2) # prints a concatenated list
This will produce the following output −:
['abcd', 123 , 4.5, 'pqr', 982.545]
abcd
[123, 4.5]
['abcd', 123 , 4.5, 'pqr', 982.545, 'hello', 123]
ii) Tuple Data Type:- tuple is another sequence data type that is similar to a list. A Python tuple consists of several values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
for example:-
tuple_list = ( 'abcd', 123, 4.5, 'pqr', 982.545 )
print (tuple_list) # Prints the complete tuple
print (tuple_list[1]) # Prints 2nd element of the tuple
print (tuple_list[2:4]) # Prints elements of the tuple index of 2 to 4(exclusive)
iii) Ranges Data Types:- range() is an in-built function in Python that returns a sequence of numbers starting from 0 and increments to 1 until it reaches a specified number.
for example:- Following is a program which uses for loop to print numbers from 0 to 5:- for i in range(5): print(i)
This produces the following result:-
0
1
2
3
4
5
Again, let's modify the program to print the number starting from 1 but with an increment of 2 instead of 1:
for i in range(1, 10, 2): print(i)
This produces the following result −:
1
3
5
7
9
4)Mapping data type:-
i) Dictionary Data Type:- A dictionary in Python is an unordered collection of data values, used to store data values like a map, unlike other Data Types that hold only a single value as an element, a Dictionary holds a key: value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon: whereas each key is separated by a ‘comma’.
for example:-
Dict_demo = {}
Dict_demo[1] = "Hello"
Dict_demo[2] = 123
Dict_demo[3] = "world"
Dict_demo["four"]= [1,2,3,4]
print(Dict_demo) # prints complete dictionary
print(Dict_demo[1]) # prints value for key 1 print(Dict_demo["four") # prints value for key "four" print(Dict_demo.keys()) # prints all keys print(Dict_demo.values()) # prints all values
This produces the following output:-
{1: "Hello", 2: 123, 3: "world", "four": [1,2,3,4]}
Hello
[1, 2, 3, 4]
dict_keys([1, 2, 3, 'four'])
dict_values(['Hello', 123, 'world', [1, 2, 3, 4]])
5) Boolean Data Types:- boolean type is one of the built-in data types which represents one of the two values either True or False. Python bool() function allows you to evaluate the value of any expression and returns either True or False based on the expression.
for example:-
a = True print(a) # Prints value of a
print(type(a)) # Prints datatype of a
This produces the following output:-
True
<class 'bool'>
6)Set data types:- In Python, a Set is an unordered collection of data types that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.
for example:-
set1 = set([1, 2, 'Hello', 4, 'World', 6])
print(set1) # prints set
This produces the following output:-
{1, 2, 'Hello', 4, 'World', 6}