Skip to content

Commit a74b249

Browse files
committed
Episode 2: split output box of string methods demonstration. Code review requested in #135.
1 parent 8235a77 commit a74b249

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

_episodes/02-basics.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,21 @@ True
380380
0
381381
THE QUICK BROWN FOX
382382
The quick brown fox
383-
False
384-
True
385383
~~~
386384
{: .output}
387385

388386
The methods starting with 'is...' return a boolean value of either True or False
389387

390388
~~~
391-
print(myString.isalpha())
389+
print(myString.isalpha())
392390
~~~
393391
{: .language-python}
394392

393+
~~~
394+
False
395+
~~~
396+
{: .output}
397+
395398
the example above returns False because the space character is not considered to be an Alphanumeric value.
396399

397400
In the example below, we can use the `replace()` method to remove the spaces and then check to see if the result `isalpha`
@@ -401,16 +404,26 @@ print(myString.replace(" ","").isalpha())
401404
~~~
402405
{: .language-python}
403406

407+
~~~
408+
True
409+
~~~
410+
{: .output}
411+
404412
For example, the following is equivalent to the above
405413
~~~
406414
mystring_clean = myString.replace(" ","")
407415
print(mystring_clean.isalpha())
408416
~~~
409417
{: .language-python}
410418

419+
~~~
420+
True
421+
~~~
422+
{: .output}
423+
411424
If you need to refer to a specific element (character) in a string,
412425
you can do so by specifying the index of the character in `[]`
413-
you can also use indexing to select a substring of the string. In Python,
426+
you can also use indexing to select a substring of the string. In Python,
414427
indexes begin with `0` (see [Index Operator: Working with the Characters of a String](http://www.interactivepython.org/courselib/static/thinkcspy/Strings/IndexOperatorWorkingwiththeCharactersofaString.html) for a visual).
415428

416429
~~~
@@ -512,7 +525,7 @@ False
512525
> bool_val1 = 'TRUE'
513526
> print('read as type ',___(bool_val1))
514527
> print('value when cast to bool',___(bool_val1))
515-
>
528+
>
516529
> bool_val2 = 'FALSE'
517530
> print('read as type ',___(bool_val2))
518531
> print('value when cast to bool',___(bool_val2))
@@ -588,37 +601,37 @@ print(type(list4))
588601
> 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.
589602
> ~~~
590603
> num_list = [4,5,6,11]
591-
>
604+
>
592605
> last_num_in_list = num_list[____]
593606
> print(last_num_in_list)
594-
>
607+
>
595608
> odd_from_list = [num_list[_____], ______]
596609
> print(odd_from_list)
597610
> ~~~
598611
> {: .language-python}
599612
> > ## Solution
600613
> > ~~~
601-
> > # Solution 1: Basic ways of solving this exercise using the core Python language
614+
> > # Solution 1: Basic ways of solving this exercise using the core Python language
602615
> > num_list = [4,5,6,11]
603-
> >
616+
> >
604617
> > last_num_in_list = num_list[-1]
605618
> > print(last_num_in_list)
606-
> >
619+
> >
607620
> > odd_from_list = [num_list[1], num_list[3]]
608621
> > print(odd_from_list)
609622
> >
610623
> >
611-
> > # Solutions 2 and 3: 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:
624+
> > # Solutions 2 and 3: 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:
612625
> > import numpy as np
613626
> > num_list = [4,5,6,11]
614-
> >
627+
> >
615628
> > # Converting `num_list` list to an advanced data structure: `numpy array`
616629
> > num_list_np_array = np.array(num_list)
617-
> >
630+
> >
618631
> > # Filtering the elements which produces a remainder of `1`, after dividing by `2`
619632
> > odd_from_list = num_list_np_array[num_list_np_array%2 == 1]
620633
> > print(odd_from_list)
621-
> >
634+
> >
622635
> > # or, Using a concept called `masking`
623636
> > # Create a boolean list `is_odd` of the same length of `num_list` with `True` at the position of the odd values.
624637
> > is_odd = [False, True, False, True] # Mask array

0 commit comments

Comments
 (0)