Python/C3/Basic-datatypes-and-operators/English-timed
Visual Cue | Narration |
---|---|
0:00 | Hello friends and welcome to the tutorial on 'Basic Data types and operators' in Python. |
0:07 | At the end of this tutorial, you will be able to,
|
0:28 | First we will explore python data structures in the domain of numbers. |
0:34 | There are three built-in data types in python to represent numbers. |
0:38 | These are:
|
0:43 | Let us first invoke our ipython interpreter |
0:48 | So type ipython in your command and hit enter |
0:52 | Lets first talk about int. |
0:54 | So type a=13 |
1:00 | Then type a and you can see the output. |
1:03 | Now, we have our first int variable a. |
1:07 | If we now see |
1:08 | Type command type within brackets a then we see the output int |
1:17 | This means that 'a' is a type of int. |
1:22 | There are lot of functions associated with the int datatype, to manipulate it in different ways. |
1:30 | These can be explored by doing, a dot tab |
1:40 | int datatype can hold integers of any size lets see this by an example. |
1:48 | So type b= couple of 9 or some data, then type b and you can see the output. |
2:02 | As you can see, even when we put a value of 9 repeated 20 times, python did not complain. |
2:12 | This is because python's int data-type can hold integers of any size. |
2:17 | Let us now look at the float data-type. |
2:21 | Decimal numbers in python are represented by the float data-type |
2:25 | So type p = 3 point 141592,then type p. |
2:41 | Then if you notice the value of output of p isn't exactly equal to p. |
2:48 | This is because computer saves floating point values in a specific format. |
2:54 | There is always an approximation. |
2:57 | This is why we should never rely on equality of floating point numbers in a program. |
3:05 | The last data type in the list is complex number |
3:09 | So type c = 3 point 2 plus 4 point 6j |
3:18 | it's just a combination of two floats the imaginary part being defined by j notation instead of i. |
3:25 | Complex numbers have a lot of functions specific to them. |
3:29 | Let us look at these |
3:31 | So type c dot tab, so can get the output. |
3:37 | So let's try some of them c.real then c.imag |
3:49 | c.real gives the real part of the number and c.imag the imaginary. |
3:56 | We can get the absolute value using the function abs within bracket c |
4:06 | Now pause the video here, and try out the following exercise and resume the video. |
4:15 | Find the absolute value of 3+4j <pause> Switch to terminal for solution |
4:27 | So type abs within bracket 3+4j |
4:34 | Thus we get the absolute value of the expression. |
4:37 | Let us do 1 more exercise of a similar type. |
4:39 | Pause the video here, and try out the following exercise and resume the video. |
4:45 | What is the data type of number 29 that is a couple of 999999999999999999? after 20 So Is it not int? |
5:00 | The solution is on your screen. |
5:02 | The data type of this number is long though it is an integer. |
5:07 | But big integers are internally stored in python as Long data type. |
5:13 | Python also has Boolean as a built-in type. |
5:18 | To Try it out, just type in the terminal t=True. |
5:28 | note that T in true is capitalized. |
5:34 | You can apply different Boolean operations on t now for example |
5:40 | Try out f = not t enter
f f or t f and t |
5:59 | The results are self explanatory. |
6:01 | What if you want to apply one operator before another. |
6:05 | Well you can use parenthesis for precedence. |
6:10 | Lets write some piece of code to check this out. |
6:13 | So type a =False
b=True c=True |
6:24 | To check how precedence changes with parenthesis, we will try two expressions and their evaluation. |
6:33 | The first one within bracket a and b or c and hit enter |
6:44 | This expression gives the value True where as the expression a and within brackets b or c |
7:01 | and that gives the value False. |
7:04 | Let's now look at some operators available in Python to manipulate these data types. |
7:09 | Python uses 'plus' sign for addition |
7:13 | So type 23 + 74 you can see the added value |
7:19 | And 'minus' sign for subtraction |
7:24 | 23 - 56 |
7:27 | '*' (star) sign for multiplication |
7:33 | '/'(back slash) for division |
7:37 | For example we can try out 384 by 16
8 by 3 and for the decimal one we can try 8 point 0 by 3. |
7:57 | Note that, when we did 8/3 the first case results in an integer output as both the operands are integer however when 8 point 0/3 is used the answer is float as one of the operands is float. |
8:16 | Let us move ahead with the operators. '%' (percentage) sign for modulo operation |
8:23 | So type 87 modulo 6 and hit enter |
8:29 | and two stars for a exponent. |
8:32 | So type 7 star star 8 and hit enter |
8:38 | It is said one wishes to use the current value of variable in which the result is stored in the expression, one can do that by putting the operator before equal to. |
8:49 | So you can say a=73
a star=34 |
8:59 | The above expression is same as, you can try out by typing a=a star 34 and hit enter |
9:12 | and a backslash =23 |
9:21 | Pause the video here, and try out the following exercise and resume the video. |
9:25 | Using python find the square root of 3. |
9:29 | The solution is on your screen. |
9:33 | 3 star star 0.5 gives the square root of 3. |
9:39 | Now, Is 3 star star 1 slash 2 and 3 star star 0 point 5 same? /no |
9:54 | Now switch to terminal. |
9:57 | And let us try both these operations. |
10:00 | So type 3 star star 0 point 5 and hit enter
3 star star 1 slash 2 and hit enter |
10:15 | As you can see,the first operation gives an integer, whereas the second one gives a float. |
10:19 | Hence,though both mean the same,they give different outputs. |
10:24 | Let us now discuss sequence data types in Python. |
10:27 | Sequence data types are those in which elements are kept in a sequential order and all the elements are accessed using index numbers. |
10:38 | The sequence data types in Python are
|
10:43 | The list type is a container that holds a number of other objects, in the given order. |
10:51 | We create our first list by typing |
10:55 | So type num underscore list = within square bracket 1 comma 2 comma 3 comma 4
num underscore list |
11:08 | Items enclosed in square brackets separated by comma constitutes a list. |
11:14 | Lists can store data of any type in them. |
11:18 | We have a list something like var underscore list = within square bracket 1 comma 1 point 2 comma then again within square bracket 1 comma 2
var underscore list |
11:45 | Lets look at another sequence data type, strings |
11:49 | So you can type greeting underscore string= within double quotes hello |
12:01 | greeting_string is now a string variable with the value "hello" |
12:07 | Python strings can actually be defined in three different ways |
12:12 | k=within single quote write Single quote
l=within double quotes Let's see how to include a single quote m=within double quotes Let's see how to include both |
12:34 | As you can see, single quotes are used as delimiters usually. |
12:38 | When a string contains a single quote, double quotes are used as delimiters. |
12:44 | When a string quote contains both single and double quotes, triple quotes are used as delimiters. |
12:55 | The last in the list of sequence data types is tuple. |
12:59 | To create a tuple we use normal brackets '(' unlike square bracket '[' for lists. |
13:07 | So you can type num underscore tuple = within bracket 1 to 8 separated by comma |
13:18 | Because of their sequential property there are certain functions and operations we can apply to all of them. |
13:25 | The first one is accessing. |
13:28 | They can be using index numbers. |
13:31 | So type in terminal num underscore list within square bracket 2
num underscore list within square bracket -1 greeting underscore string within square bracket 1 greeting underscore string within square bracket 3 greeting underscore string within square bracket -2 num underscore tuple within square bracket 2 num underscore tuple within square bracket -3 |
14:08 | Indexing starts from 0, from left to right and from -1 when accessing lists in reverse. |
14:14 | Thus num underscore list[2] refers to the third element 3. and greetings [-2] is the second element from the end , that is 'l'. |
14:26 | Addition gives a new sequence containing both sequences |
14:30 | So type num underscore list + var underscore list in terminal.
Then a underscore string= within double quote another string greeting underscore string+a underscore string t2= 3 4 6 7 separated by commas in bracket num underscore tuple+t2 |
15:17 | len function gives the length |
15:20 | So you can type len within bracket num underscore list
len within bracket greeting underscore string len within bracket num underscore tuple |
15:43 | We can check the container ship of an element using the 'in' keyword |
15:50 | So you can type in terminal and check so that you can see the difference.So type
3 in num underscore list Then type in single quotes 'H' in greeting underscore string 2 in num underscore tuple |
16:15 | We see that it gives True and False accordingly. |
16:18 | Find maximum using max function and minimum using min |
16:23 | Check that by typing in terminal
max within bracket num underscore tuple min within bracket greeting underscore string |
16:39 | Get a sorted list |
16:42 | So you can type sorted within bracket num underscore list |
16:50 | As a consequence of their order, we can access a group of elements in a sequence, together. |
16:57 | This is called slicing and striding. |
17:00 | First lets discuss Slicing, |
17:02 | Given a list j= within square bracket 1 to 6 |
17:09 | Lets say we want elements starting from 2 and ending in 5. |
17:14 | For this we can do j within square bracket 1 colon 4 |
17:24 | The syntax for slicing is, sequence variable name, square bracket, first element index, colon, second element index. |
17:36 | The first element however is not included in the resultant list |
17:39 | So you can type j within square bracket colon 4 |
17:49 | If first element is left blank default is from beginning and if last element is left blank it means till the end. |
18:14 | So you can type in the terminal
j within square bracket 1 colon j within square bracket colon |
18:30 | This effectively is the whole list. |
18:33 | Striding is similar to slicing except that the step size here is not one. |
18:39 | So let us see an example |
18:41 | So type new underscore num underscore list=1 to 10 separated by commas within square bracket
new underscore num underscore list within square bracket 1 colon 8 colon 2 then on the other line within square bracket 2 4 6 8 separated by commas that will be the output. |
19:07 | The, colon two, added in the end signifies all the alternate elements. |
19:15 | This is why we call this concept striding because we move through the list with a particular stride or step. |
19:23 | The step in this example being 2. |
19:25 | We have talked about many similar features of lists, strings and tuples. |
19:32 | But there are many important features in lists that differ from strings and tuples. |
19:37 | Lets see this by example. |
19:40 | Type new underscore num underscore list within square bracket[1]=9
greeting underscore string within square bracket 1 =within single quote k |
19:59 | We can see an error |
20:03 | As you can see while the first command executes with out a problem there is an error on the second one. |
20:10 | Now lets try new underscore tuple within square bracket 1=5 |
20:19 | Its the same error. |
20:23 | This is because strings and tuples share the property of being immutable. |
20:28 | We cannot change the value at a particular index just by assigning a new value at that position. |
20:34 | We have looked at different types but we need to convert one data type into another. |
20:39 | Well lets one by one go through methods by which we can convert one data type to other |
20:45 | So type i=34
d=float within bracket i d |
21:00 | Python has built in functions int, float and complex to convert one number type data structure to another. |
21:08 | So type dec=2.34
dec underscore con=int(dec) dec underscore con |
21:29 | As you can see the decimal part of the number is simply stripped to get the integer. |
21:35 | So type com=2.3+4.2j
float within bracket com |
21:56 | So here we are getting another error |
22:05 | So here the error can be solved by your side. |
22:10 | Similarly we can convert list to tuple and tuple to list. |
22:15 | For that type lst= 3 4 5 6 separated by comma
tup=tuple within bracket lst Then type lst Then type tupl=3 comma 23 comma 4 comma 56 in bracket lst=list within bracket tuple Then type tuple |
22:54 | However converting a string to a list and a list to a string is an interesting problem. |
22:59 | Let's say we have a string some string= within double quote Is there a way to split on these spaces.
some string.split() function, so it will spilt.Hit by command. |
23:28 | This produces a list with the string split at white space. |
23:33 | Similarly we can split on some other character. |
22:37 | So you can type other string=within double quote Tim comma Amy comma Stewy comma Boss |
23:48 | How do we split on comma , simply pass it as argument |
23:56 | So you can type on the terminal other string.split within bracket comma in single quote and hit enter |
24:06 | join function does the opposite. |
24:10 | Joins a list to make a string. |
24:14 | So you can type l1=within square bracket List comma joined comma on comma commas,all within single quote
Then type , within single quote comma .join within bracket l1 |
25:02 | Thus we get a list joined on commas. |
25:05 | Similarly we can do spaces. |
25:07 | So you can type l2=Within square brackets in single quote Now comma on comma spaces.
And we can type in single quote space.join within bracket l2, so you will get out put. |
25:27 | Note that the list has to be a list of strings to apply join operation. |
25:32 | Pause the video here, try out the following exercise and resume the video. |
25:38 | Check if 3 is an element of the list [1 comma 7 comma 5 comma 3 comma 4]. In case</nowiki> it is change it to 21. |
25:56 | Switch to the terminal for solution |
26:00 | Type l=[1 comma 7 comma 5 comma 3 comma 4
3 in l l in square bracket 3=21 l |
26:22 | Let us solve one more exercise. |
26:24 | Pause the video here, do the exercise and resume the video. |
26:31 | Convert the string "Elizabeth is queen of England" to "Elizabeth is queen" |
26:39 | Switch to the terminal for solution. |
26:43 | So s=Elizabeth is queen of England in double quotes
stemp=s.split() within single quote space.join within bracket stemp within square bracket colon 3 and hit enter |
27:07 | As you can see, we have easily removed the unwanted words. |
27:11 | This brings us to the end of the tutorial. |
27:14 | In this tutorial, we have learnt to, 1. Understand the number Data types -- integer,float and complex. |
27:21 | 2. Know the boolean data type and operators +, backslash, percent that is modulo and * for multiplication . |
27:33 | 3. use the sequence data types -- List,String and Tuple. |
27:36 | 4. Slice sequences by using the row and column numbers. |
27:41 | 5. Split and join a list using split() and join() function respectively. |
27:49 | 6. Convert to string to tuple and vice-versa. |
27:54 | Here are some self assessment questions for you to solve |
27:58 | 1. What is the major difference between tuples and lists? |
28:02 | 2. Split this string on white spaces |
28:05 | string="Split this string on white spaces" |
28:09 | And the answers, |
28:12 | 1. The major difference between tuples and lists is that Tuples are immutable while lists are not. |
28:20 | 2. To split the string on white space, we use the function `` split`` without any argument |
28:26 | string.split() function |
28:29 | Hope you have enjoyed this tutorial and found it useful. |
28:34 | Thank You. |