Skip to content

Commit 8cc17d8

Browse files
authored
Update 02-basics.md
fixes #108, #130 Thank you @YangDawu and @eldobbins for elaborating the issues Thank you @OmaymaS, @Kilatsat, @evebohnett, and @kevswanberg for ideas about the new solutions
1 parent a49655b commit 8cc17d8

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

_episodes/02-basics.md

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ A string is a simple data type which holds a sequence of characters.
303303

304304
Strings are placed in quotes when they are being assigned, but the quotes don't count as part of the string value.
305305

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.
307307

308308
~~~
309309
mystring = "Hello World"
@@ -445,6 +445,8 @@ So far we have seen three basic Python data types; Integer, Float and String. Th
445445
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.
446446

447447
~~~
448+
print(True)
449+
print(False)
448450
bool_val_t = True
449451
print(type(bool_val_t))
450452
print(bool_val_t)
@@ -454,16 +456,43 @@ print(bool_val_f)
454456
~~~
455457
{: .language-python}
456458

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)
458482
483+
NameError: name 'true' is not defined
459484
~~~
485+
{: .output}
486+
460487

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+
~~~
461491
print('hello' == 'HELLO')
462492
print('hello' is 'hello')
463493
print(3 != 77)
464494
print(1 < 2)
465495
print('four' > 'three')
466-
467496
~~~
468497
{: .language-python}
469498

@@ -557,27 +586,44 @@ print(type(list4))
557586
558587
559588
> ## 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.
562590
> ~~~
563591
> 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)
574598
> ~~~
575599
> {: .language-python}
576600
> > ## Solution
577601
> > ~~~
578-
> > is_odd = [False, True, False, True]
579-
> > odd_from_list = num_list[is_odd]
602+
> > num_list = [4,5,6,11]
603+
> >
580604
> > 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`
619+
> > odd_from_list = num_list_np_array[num_list_np_array%2 == 1]
620+
> > print(odd_from_list)
621+
> >
622+
> > # or, Using a concept called `masking`
623+
> > # Create a boolean list `is_odd` of the same length of `num_list` with `True` at the position of the odd values.
624+
> > is_odd = [False, True, False, True] # Mask array
625+
> > odd_from_list = num_list_np_array[is_odd] # only the values at the position of `True` remain
626+
> > print(odd_from_list)
581627
> > ~~~
582628
> > {: .language-python}
583629
> {: .solution}

0 commit comments

Comments
 (0)