Machine learning & data Science
Affordable, Specific & loaded with information to start your career in machine learning & data science.
Welcome to this post where we are going to talk about data types in python 3.X. We have already gone through python installation, variables & comments. If you need to refer to them, please visit;
Scope
- Numbers
- Strings
- Boolean
- List
In the next post, I will address tuples & dictionaries.
You can scroll down and download the jupyter notebook file at the bottom of the post.
Python Data Types
First data type in python is Numbers.
Numbers
Python primarily have got three types of inbuilt number data type;
- Int
- float
- complex
In addition to the above mentioned 3 numeric data types, Boolean is the sub-type of int data type. We have already talked about int(integer) and float(floating numbers) in the last post.
Int stands for integer.
Integer is a whole number, without fraction like 1,24,125 or 1000. If there is no decimal it is int.
But if there is decimal value, it is NOT an int but a floating number.
Float stands for floating numbers. For example 1.3, 24.5, 125.001 & 1000.001 etc.
These are common numbers which you have already seen in your like like prices of the products or services you buy in your daily lives.
But there is third kind of number which you might not have seen before.
It is called as complex. Complex numbers are not widely used and are represented by two part;
complex_number= a +bj
Complex numbers have two parts, real & imaginary part, which are each a floating-point number along with j which is the imaginary number. It is the square root of -1.
Complex number will always have a j in their denotation.
Python is very efficient when it comes to handling numbers. You can directly run number based commands in the console and it will return results.
Let's look at some of the examples dealing with numbers in python to get better understanding.
I am going to run following simple calculations in anaconda prompt. Please note you will have to first enter "python" in the anaconda prompt and then enter. After that you can run these commands.
Some of the most common Math Operation in Python
Result | Notes |
x + y | sum of x and y |
x - y | difference of x and y |
x * y | product of x and y |
x / y | quotient of x and y |
x // y | floored quotient of x and y |
x % y | remainder of x / y |
-x | x negated |
+x | x unchanged |
abs(x) | absolute value or magnitude of x |
int(x) | x converted to integer |
float(x) | x converted to floating point |
complex(re, im) | a complex number with real part re, imaginary partim. im defaults to zero. |
c.conjugate() | conjugate of the complex number c |
divmod(x, y) | the pair (x // y, x % y) |
pow(x, y) | x to the power y |
x ** y | x to the power y |
Strings Data Type
Just like numbers, strings can be easily manipulated in Python. Let's first go through some of the important properties of python strings;
- Python strings can be enclosed with single (' ') or double (" ") quotes.
- Single and double quotes will produce same result.
- You can use backslash ( \ ) to escape if there are single quotes in your python string, for example string contain words like (don't or won't). You can use backslash like;
don\'t
won\'t
- print function is used to print the string.
- You can also use r to print the raw strings like;
print(r'C:\some\name')
- You can use \n to create new lines in a string like;
one\name will produce;
one
ame
- Double quotes """...""" or single quotes '''...''' can be used to format lines when strings is spanned over multiple lines
- You can use + sign for concatenation of the strings.
- You can use * (multiply) sign to repeat the string.
- Strings can be indexed, and the correct order is "left to right". For Ex;
A k h i l e n d r a
0 1 2 3 4 5 6 7 8 9
Now if you;
print("akhilendra[0])
you will get- A
To print it from the right, use negative indexing, therefore if you print;
print("akhilendra[-2])
you will get- r
Please note that the above example is used for illustration purpose, you will need to assign it properly before printing a string, something like;
abc='akhilendra'
print(abc[0])
second character from right
- Python strings cannot be changed that is, they are immutable.
Before looking at more strings functions and manipulation techniques, let's have a look at "List".
Python List
List are one of the most important and most versatile data types in Python.
If you are coming from different programming languages, then you might be aware of "Array".
List are like arrays of Python.
Those of you who are not aware of arrays, List are like variables/object in which you can store multiple values separated by comma.
You can create list in python by;
Mylist = [value1,value2,value3......]
You can access individual values in array by using their index number starting from 0 from the left.
In the last section about string, I have mentioned that "Strings cannot be changed, that is they immutable", in case of list, they are mutable.
You can edit the content of list and assign, add or remove values within list.
Example 1:
List_1= [1,2,3,4,5,6,56,67,33,456,345,12,34,10,13,11,23,21]
#this is our list,let's print value which is at 5 index value,i.e. 6th value
print (List_1[5])
#output
6
Please note that name of our list start with capital & as mentioned earlier, python is case sensitive therefore if you don't type the name correctly your list won't work.
Example 2:
Let's use the list created under Example 1 and remove certain values.
By using remove, you can specify the value which you want to remove.
Instead of using the index value, you can directly specify the value.
Remove method removes the first occurrence of the object
Syntax is;
listname.remove(valueofobjecttoberemoved)
List_1= [1,2,3,4,5,6,56,67,33,456,345,12,34,10,13,11,23,21]
Lets remove 23 and assign the list to a new variable
List_1.remove(23)
And now if you print List_1, there won't be 23 there.
Example 3:
Let's replace a existing value in the list with the new one.
You just need to specify the index number and assign the value just as you do with variables
List_1= [1,2,3,4,5,6,56,67,33,456,345,12,34,10,13,11,21]
Let's replace 56 with 112, 56 is the seventh value i.e. index number 6
List_1[6]=112
You can now print List_1 to confirm the updates.
Example 4:
You can also add new value at the end of the list using append().
You can directly provide the value in the argument or provide a formula where the output will be inserted in the list
List_1= [1, 2, 3, 4, 5, 6, 112, 67, 33, 456, 345, 12, 34, 10, 13, 11, 21]
List_1.append(1023)
List_1.append(563 * 4)
print(List_1)
#output will have 1023 & 2252 added to it
[1, 2, 3, 4, 5, 6, 112, 67, 33, 456, 345, 12, 34, 10, 13, 11, 21, 1023, 2252]
Example 5:
Let's count the number of items in list by using len.
You can use len with the syntax len(listname) to count the number of items within list
List_1= [1, 2, 3, 4, 5, 6, 112, 67, 33, 456, 345, 12, 34, 10, 13, 11, 21, 1023, 2252]
print (len(List_1))
# Output
19
Example 6:
Let's create a list with values up to 20 using range function.
list1=list(range(20))
print(list1)
#output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Example 7:
Let's use extend and range to extend our list.
We will first create a list with certain values.
After that we will use extend method to extend it where we will use range to provide a range, in this example it is 6-20.
Please note that range will insert values till last value but not the last value itself. In our case, it will extend list till 19.
list2=[1,2,3,5]
list2.extend(range(6,20))
print(list2)
#output
[1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Example 8:
So far we have seen examples where values are inserted at the end of the list.
Now we will use "insert" to insert value at the defined indexes/serial number
With "insert" you can not only a insert values, but also specify the position.
Let's create a list and see how we can insert these items wherever we want
list2=list('acdfh')
Now we realized that we have missed b
We will use insert to add b after a, in this case index value/position is 1
list3.insert(1,'b')
print(list3)
#output
['a', 'b', 'c', 'd', 'f', 'h']
Speak Your Mind