You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _episodes/02-basics.md
+63-17Lines changed: 63 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -303,7 +303,7 @@ A string is a simple data type which holds a sequence of characters.
303
303
304
304
Strings are placed in quotes when they are being assigned, but the quotes don't count as part of the string value.
305
305
306
-
If you need to use quotes as part of your string you can arbitrarily use either single of double quotes to indicate the start and end of the string.
306
+
If you need to use quotes as part of your string you can arbitrarily use either single or double quotes to indicate the start and end of the string.
307
307
308
308
~~~
309
309
mystring = "Hello World"
@@ -445,6 +445,8 @@ So far we have seen three basic Python data types; Integer, Float and String. Th
445
445
We can define variables to be of type boolean by setting their value accordingly. Boolean variables are a good way of coding anything that has a binary range (eg: yes/no), because it's a type that computers know how to work with as we will see soon.
446
446
447
447
~~~
448
+
print(True)
449
+
print(False)
448
450
bool_val_t = True
449
451
print(type(bool_val_t))
450
452
print(bool_val_t)
@@ -454,16 +456,43 @@ print(bool_val_f)
454
456
~~~
455
457
{: .language-python}
456
458
457
-
We can also get variables of this type using comparison operators, basic ones in Python are `==` for "equal to", `!=` for "not equal to", and `>`, `<`, or `>=`, `<=`.
459
+
~~~
460
+
True
461
+
False
462
+
<class 'bool'>
463
+
True
464
+
<class 'bool'>
465
+
False
466
+
~~~
467
+
{: .output}
468
+
469
+
Following two lines of code will generate error because Python is case-sensitive. We need to use 'True' instead of 'true' and 'False' instead of 'false'.
470
+
471
+
~~~
472
+
print(true)
473
+
print(false)
474
+
~~~
475
+
{: .language-python}
476
+
477
+
~~~
478
+
NameError Traceback (most recent call last)
479
+
<ipython-input-115-b5911eeae48b> in <module>
480
+
----> 1 print(true)
481
+
2 print(false)
458
482
483
+
NameError: name 'true' is not defined
459
484
~~~
485
+
{: .output}
486
+
460
487
488
+
We can also get values of this type using comparison operators, basic ones in Python are `==` for "equal to", `!=` for "not equal to", and `>`, `<`, or `>=`, `<=`.
489
+
490
+
~~~
461
491
print('hello' == 'HELLO')
462
492
print('hello' is 'hello')
463
493
print(3 != 77)
464
494
print(1 < 2)
465
495
print('four' > 'three')
466
-
467
496
~~~
468
497
{: .language-python}
469
498
@@ -557,27 +586,44 @@ print(type(list4))
557
586
558
587
559
588
> ## Exercise
560
-
> We can index lists the same way we indexed strings before or using a boolean list of the same length.
561
-
>
589
+
> We can index lists the same way we indexed strings before. Complete the code below and display the value of `last_num_in_list` which is 11 and values of `odd_from_list` which are 5 and 11 to check your work.
562
590
> ~~~
563
591
> num_list = [4,5,6,11]
564
-
> ~~~
565
-
> {: .language-python}
566
-
>
567
-
> Using the number list defined above, complete the code below and display the values of `odd_from_list` and `last_num_in_list` to check your work.
568
-
>
569
-
> ~~~
570
-
> is_odd = [False, ___]
571
-
> odd_from_list =
572
-
> last_num_in_list =
573
-
>
592
+
>
593
+
> last_num_in_list = num_list[____]
594
+
> print(last_num_in_list)
595
+
>
596
+
> odd_from_list = [num_list[_____], ______]
597
+
> print(odd_from_list)
574
598
> ~~~
575
599
> {: .language-python}
576
600
> > ## Solution
577
601
> > ~~~
578
-
> > is_odd = [False, True, False, True]
579
-
> > odd_from_list = num_list[is_odd]
602
+
> > num_list = [4,5,6,11]
603
+
> >
580
604
> > last_num_in_list = num_list[-1]
605
+
> > print(last_num_in_list)
606
+
> >
607
+
> > odd_from_list = [num_list[1], num_list[3]]
608
+
> > print(odd_from_list)
609
+
> >
610
+
> >
611
+
> > # These are the basic ways of working using the core Python language. Usually there are multiple ways of doing the same work. Once we learn about more advanced Python, we would be able to write more varieties codes like the followings to print the odd numbers:
612
+
> > import numpy as np
613
+
> > num_list = [4,5,6,11]
614
+
> >
615
+
> > # Converting `num_list` list to an advanced data structure: `numpy array`
616
+
> > num_list_np_array = np.array(num_list)
617
+
> >
618
+
> > # Filtering the elements which produces a remainder of `1`, after dividing by `2`
0 commit comments