Difference between revisions of "Ruby/C2/Logical-and-other-Operators/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 13: | Line 13: | ||
|- | |- | ||
| 00:09 | | 00:09 | ||
− | | | + | | '''Logical Operators ''' |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | | | + | |'''Parallel assignment''' and |
|- | |- | ||
| 00:13 | | 00:13 | ||
− | | | + | | '''Range Operators '''. |
|- | |- | ||
| 00:15 | | 00:15 | ||
− | |Here we are using: | + | |Here, we are using: |
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | | + | | '''Ubuntu Linux''' version '''12.04''' |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | | + | | ''' Ruby 1.9.3 ''' |
|- | |- | ||
Line 41: | Line 41: | ||
|- | |- | ||
| 00:29 | | 00:29 | ||
− | |You must also be familiar with '''irb'''. | + | |You must also be familiar with ''''irb''''. |
|- | |- | ||
Line 65: | Line 65: | ||
|- | |- | ||
| 00:51 | | 00:51 | ||
− | | | + | | double ampersand (&&) that is '''and''' |
|- | |- | ||
| 00:54 | | 00:54 | ||
− | | | + | | double '''pipe''' that is '''or''' |
|- | |- | ||
| 00:56 | | 00:56 | ||
− | | | + | | Exclamation mark (!) that is '''not'''. |
|- | |- | ||
| 01:00 | | 01:00 | ||
− | | | + | | '&&' (double ampersand) and '''and''' evaluate to '''true''' only if both the expressions are '''true.''' |
|- | |- | ||
Line 117: | Line 117: | ||
|- | |- | ||
| 01:49 | | 01:49 | ||
− | | We get the output as | + | | We get the output as "true". |
|- | |- | ||
Line 161: | Line 161: | ||
|- | |- | ||
| 02:35 | | 02:35 | ||
− | | We get the output as | + | | We get the output as "false". |
|- | |- | ||
Line 173: | Line 173: | ||
|- | |- | ||
| 02:49 | | 02:49 | ||
− | | So, we get output as | + | | So, we get the output as "false". |
|- | |- | ||
| 02:53 | | 02:53 | ||
− | | ''' | + | | double '''pipe''' and '''or''' evaluate to '''true''', if either '''expression''' is '''true.''' |
|- | |- | ||
Line 205: | Line 205: | ||
|- | |- | ||
| 03:26 | | 03:26 | ||
− | |We get output as | + | |We get output as "true". |
|- | |- | ||
| 03:29 | | 03:29 | ||
− | |Here | + | |Here expression 1, that is '''10>6''', is '''true'''. |
|- | |- | ||
Line 245: | Line 245: | ||
|- | |- | ||
| 04:10 | | 04:10 | ||
− | | Since both the expressions are '''false''', we get output as | + | | Since both the expressions are '''false''', we get output as "false". |
|- | |- | ||
| 04:15 | | 04:15 | ||
− | |'' | + | |'!' (exclamation mark ) and '''not''' operators return the opposite value of the expression. |
|- | |- | ||
Line 261: | Line 261: | ||
|- | |- | ||
| 04:30 | | 04:30 | ||
− | | Difference in the two forms is '''precedence. ''' | + | | Difference in the two forms is '''precedence.''' |
|- | |- | ||
| 04:33 | | 04:33 | ||
− | | Symbolic ''' | + | | Symbolic '''not''', that is '!', has higher '''precedence.''' |
|- | |- | ||
Line 281: | Line 281: | ||
|- | |- | ||
| 04:47 | | 04:47 | ||
− | |We get the output as | + | |We get the output as "true". |
|- | |- | ||
Line 301: | Line 301: | ||
|- | |- | ||
| 05:06 | | 05:06 | ||
− | |We get the output as | + | |We get the output as "false". |
|- | |- | ||
Line 321: | Line 321: | ||
|- | |- | ||
| 05:29 | | 05:29 | ||
− | |Let's declare three variables | + | |Let's declare three variables 'a', 'b', 'c' using '''parallel assignment. ''' |
|- | |- | ||
Line 333: | Line 333: | ||
|- | |- | ||
| 05:47 | | 05:47 | ||
− | |Here, | + | |Here, 10 will be assigned to variable 'a', |
|- | |- | ||
| 05:52 | | 05:52 | ||
− | | | + | |20 will be assigned to variable 'b' |
|- | |- | ||
| 05:54 | | 05:54 | ||
− | | | + | |30 will be assigned to variable 'c'. |
|- | |- | ||
Line 353: | Line 353: | ||
|- | |- | ||
| 06:10 | | 06:10 | ||
− | |We will learn about '''arrays''' in detail in the upcoming tutorials. | + | |We will learn about '''arrays''' in detail, in the upcoming tutorials. |
|- | |- | ||
Line 381: | Line 381: | ||
|- | |- | ||
| 06:37 | | 06:37 | ||
− | |30 is displayed. | + | | 30 is displayed. |
|- | |- | ||
Line 393: | Line 393: | ||
|- | |- | ||
| 06:50 | | 06:50 | ||
− | |Type '''puts | + | |Type: '''puts space within double quotes a equal to hash within curly brackets a comma within double quotes b equal to hash within curly brackets b''' |
|- | |- | ||
Line 469: | Line 469: | ||
|- | |- | ||
| 08:20 | | 08:20 | ||
− | |'''Ranges''' are used to identify whether a value falls within a particular range | + | |'''Ranges''' are used to identify whether a value falls within a particular range too. |
|- | |- | ||
Line 493: | Line 493: | ||
|- | |- | ||
| 08:50 | | 08:50 | ||
− | |'''Inclusive operator''' includes both begin and end values in a range. | + | |'''Inclusive operator''' includes both, begin and end values, in a range. |
|- | |- | ||
| 08:57 | | 08:57 | ||
− | |Here '''to_a''' method is used to convert a '''range''' to a list. | + | |Here '''to_a''' '''method''' is used to convert a '''range''' to a list. |
|- | |- | ||
Line 505: | Line 505: | ||
|- | |- | ||
| 09:05 | | 09:05 | ||
− | |Here you can see the values 1 and 10 are included in the range. | + | |Here, you can see the values 1 and 10 are included in the range. |
|- | |- | ||
| 09:11 | | 09:11 | ||
− | |Now we will see an '''exclusive range''' operator. | + | |Now, we will see an '''exclusive range''' operator. |
|- | |- | ||
Line 533: | Line 533: | ||
|- | |- | ||
| 09:45 | | 09:45 | ||
− | |Now let's check whether 5 lies in the range of 1 to 10. | + | |Now, let's check whether 5 lies in the range of 1 to 10. |
|- | |- | ||
Line 545: | Line 545: | ||
|- | |- | ||
| 10:02 | | 10:02 | ||
− | | | + | |Equality operator is used to check whether a value lies in the range. |
|- | |- | ||
| 10:07 | | 10:07 | ||
− | |We get the output as | + | |We get the output as "true" since 5 lies in the range 1 to 10. |
|- | |- | ||
Line 569: | Line 569: | ||
|- | |- | ||
| 10:34 | | 10:34 | ||
− | |'''Range Operator Inclusive operator '''(..) and Exclusive operator(...)'''. | + | |'''Range Operator- Inclusive operator '''(..) and '''Exclusive operator(...)'''. |
|- | |- | ||
Line 601: | Line 601: | ||
|- | |- | ||
| 11:03 | | 11:03 | ||
− | | | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
|11:05 | |11:05 | ||
− | | | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
| 11:09 | | 11:09 | ||
− | |For more details, please write to: | + | |For more details, please write to:contact@spoken-tutorial.org |
− | contact@spoken-tutorial.org | + | |
|- | |- | ||
Line 622: | Line 621: | ||
|- | |- | ||
| 11:25 | | 11:25 | ||
− | |More information on this mission is available at: | + | |More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
− | spoken hyphen tutorial dot org slash NMEICT hyphen Intro. | + | |
|- | |- |
Latest revision as of 15:14, 27 March 2017
Time | Narration |
00:02 | Welcome to this Spoken Tutorial on Logical & Other Operators. |
00:06 | In this tutorial, we will learn: |
00:09 | Logical Operators |
00:11 | Parallel assignment and |
00:13 | Range Operators . |
00:15 | Here, we are using: |
00:17 | Ubuntu Linux version 12.04 |
00:20 | Ruby 1.9.3 |
00:23 | To follow this tutorial, you must know how to use Terminal and Text editor in Linux. |
00:29 | You must also be familiar with 'irb'. |
00:33 | If not, for relevant tutorials, please visit our website. |
00:38 | Logical operators are also known as Boolean operators |
00:42 | because they evaluate parts of an expression |
00:45 | and return a true or false value. |
00:48 | Logical Operators are- |
00:51 | double ampersand (&&) that is and |
00:54 | double pipe that is or |
00:56 | Exclamation mark (!) that is not. |
01:00 | '&&' (double ampersand) and and evaluate to true only if both the expressions are true. |
01:07 | Second expression is evaluated only if the first is true. |
01:12 | Difference in the two forms is precedence. |
01:15 | Symbolic and that is&&(double ampersand) has higher precedence. |
01:20 | Let's see some examples now. |
01:22 | We will use irb for this. |
01:25 | Open the terminal by pressing Ctrl, Alt and T keys simultaneously. |
01:31 | Type "irb" and press Enter to launch interactive Ruby. |
01:36 | Type: 3 greater than 2 space double ampersand space 4 less than 5. |
01:47 | Press Enter. |
01:49 | We get the output as "true". |
01:53 | Here, expression1 that is 3>2 is true. |
01:59 | Expression 2 that is 4<5 is also true. |
02:03 | Since both the expressions are true, we get output as true. |
02:08 | Now, press Up-Arrow key to get the previous command. |
02:12 | And replace the double ampersand symbol with the word "and". |
02:17 | Press Enter. |
02:19 | We get the same result. |
02:22 | Now, press up-arrow key again to get the previous command. |
02:27 | In expression1, replace greater-than sign with less-than, |
02:32 | press Enter |
02:35 | We get the output as "false". |
02:38 | This is because 3 < 2 is false. |
02:43 | Since the first expression is false, the second expression will not be evaluated. |
02:49 | So, we get the output as "false". |
02:53 | double pipe and or evaluate to true, if either expression is true. |
02:59 | Second expression is evaluated only if first is false. |
03:04 | Difference in the two forms is precedence. |
03:07 | Symbolic or i.e double pipe has higher precedence. |
03:11 | Now, let's try some examples. |
03:15 | Type: 10 greater than 6 space double pipe space 12 less than 7 |
03:23 | Press Enter. |
03:26 | We get output as "true". |
03:29 | Here expression 1, that is 10>6, is true. |
03:35 | Since the first expression is true, second expression will not be evaluated. |
03:40 | So, we get the output as true. |
03:42 | Now, press the Up-Arrow key to get the previous command. |
03:46 | In expression 1 replace greater than sign with less than sign |
03:52 | and replace pipe symbol with the word or. |
03:57 | Press Enter. |
04:00 | Here, expression1 that is 10<6 is false. |
04:05 | Expression 2 that is 12<7 is also false. |
04:10 | Since both the expressions are false, we get output as "false". |
04:15 | '!' (exclamation mark ) and not operators return the opposite value of the expression. |
04:20 | If the expression is true, exclamation mark operator will return a false value. |
04:27 | It will return true if the expression is false. |
04:30 | Difference in the two forms is precedence. |
04:33 | Symbolic not, that is '!', has higher precedence. |
04:37 | Let's try out the "not" operator. |
04:40 | First, type: 10 double equal to 10 |
04:45 | Press Enter . |
04:47 | We get the output as "true". |
04:50 | To invert the result of above expression, |
04:53 | let's add the "not" operator before the expression. |
04:57 | Type: exclamation mark within brackets 10 double equal to 10 . |
05:04 | Press Enter. |
05:06 | We get the output as "false". |
05:10 | Press Ctrl+L simultaneously to clear the irb console. |
05:15 | Next, let us learn about parallel assignment. |
05:20 | Multiple variables can be initialized with a single line of Ruby code, through parallel assignment. |
05:26 | Let's switch to the terminal. |
05:29 | Let's declare three variables 'a', 'b', 'c' using parallel assignment. |
05:36 | Type: a comma b comma c equal to 10 comma 20 comma 30 |
05:45 | and press Enter. |
05:47 | Here, 10 will be assigned to variable 'a', |
05:52 | 20 will be assigned to variable 'b' |
05:54 | 30 will be assigned to variable 'c'. |
05:56 | The right-hand side acts as an array. |
06:01 | If we list multiple variables on the left hand side then the array is unpacked and assigned into the respective variables. |
06:10 | We will learn about arrays in detail, in the upcoming tutorials. |
06:14 | For now, let's check whether the assignment is done properly. |
06:20 | Type 'a' and press Enter. |
06:23 | Value 10, stored in variable 'a', is displayed. |
06:28 | Type 'b' and press Enter. |
06:31 | We get 20. |
06:33 | Type 'c' and press Enter. |
06:37 | 30 is displayed. |
06:40 | Parallel assignment is also useful for swapping the values stored in two variables. |
06:45 | Let us swap the values of variables 'a' and 'b'. |
06:50 | Type: puts space within double quotes a equal to hash within curly brackets a comma within double quotes b equal to hash within curly brackets b |
07:11 | Press Enter. |
07:13 | We get the output as a=10 |
07:16 | b=20. |
07:20 | Now, let's swap 'a' and 'b'. |
07:23 | To do so, type: |
07:25 | a comma b equal to b comma a |
07:31 | Press Enter. |
07:33 | Press Up-Arrow key twice to get the puts command and press Enter. |
07:39 | We get the output as- |
07:41 | a=20 |
07:44 | b=10 . |
07:47 | We will now learn about range in Ruby. |
07:50 | The values in a range can be numbers, characters, strings or objects. |
07:58 | Ranges are used to express a sequence. |
08:02 | Sequence range is used to create a range of successive values. |
08:06 | It consists of a start value, range of values and an end value. |
08:13 | (..) two dot operator creates inclusive range. |
08:16 | (...) three dot operator creates an exclusive range. |
08:20 | Ranges are used to identify whether a value falls within a particular range too. |
08:26 | We do this using (===) the equality operator. |
08:30 | Let us try out some examples on ranges. |
08:33 | Let's switch to terminal. |
08:36 | Type: within brackets 1 two dots 10 then dot to underscore a |
08:46 | Two dot operator creates inclusive range. |
08:50 | Inclusive operator includes both, begin and end values, in a range. |
08:57 | Here to_a method is used to convert a range to a list. |
09:03 | Press Enter. |
09:05 | Here, you can see the values 1 and 10 are included in the range. |
09:11 | Now, we will see an exclusive range operator. |
09:16 | Type: within brackets 1 three dots 10 then dot to underscore a. |
09:27 | Three dot operator creates an exclusive range. |
09:31 | Exclusive range operator excludes the end value from the sequence. |
09:37 | Press Enter. |
09:39 | Here, the end value 10 is not included in the range. |
09:45 | Now, let's check whether 5 lies in the range of 1 to 10. |
09:50 | Type: within brackets 1 two dots 10 three times equal to and then 5. |
10:00 | Press Enter. |
10:02 | Equality operator is used to check whether a value lies in the range. |
10:07 | We get the output as "true" since 5 lies in the range 1 to 10. |
10:14 | This brings us to the end of this Spoken Tutorial. |
10:17 | In this tutorial, we have learnt: |
10:20 | Logical operator i.e double ampersand, double pipe and exclamation mark operators. |
10:27 | Parallel assignment Ex: a, b, c = 10, 20, 30 |
10:34 | Range Operator- Inclusive operator (..) and Exclusive operator(...). |
10:39 | As an assignment, |
10:41 | declare two variables using parallel assignment and |
10:45 | check whether their sum lies between 20 and 50. |
10:49 | Watch the video available at the following link. |
10:52 | It summarizes the Spoken Tutorial project. |
10:56 | If you do not have good bandwidth, you can download and watch it. |
11:00 | The Spoken Tutorial project team: |
11:03 | Conducts workshops using spoken tutorials. |
11:05 | Gives certificates to those who pass an online test. |
11:09 | For more details, please write to:contact@spoken-tutorial.org |
11:15 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
11:19 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
11:25 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
11:34 | This script has been contributed by the spoken tutorial team, IIT Bombay. |
11:38 | And this is Shalini Nair, signing off. Thank you. |