Skip to content

Commit 13e0f2c

Browse files
authored
Merge pull request #158 from vinisalazar/patch-5
Episode 2: split output box of string methods demonstration.
2 parents d6dfd90 + a74b249 commit 13e0f2c

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

_episodes/02-basics.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,26 @@ print(myString) # you would have to assign it to a new variab
375375
~~~
376376
{: .language-python}
377377

378+
~~~
379+
True
380+
0
381+
THE QUICK BROWN FOX
382+
The quick brown fox
383+
~~~
384+
{: .output}
385+
378386
The methods starting with 'is...' return a boolean value of either True or False
379387

380388
~~~
381-
print(myString.isalpha())
389+
print(myString.isalpha())
382390
~~~
383391
{: .language-python}
384392

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

387400
In the example below, we can use the `replace()` method to remove the spaces and then check to see if the result `isalpha`
@@ -391,27 +404,26 @@ print(myString.replace(" ","").isalpha())
391404
~~~
392405
{: .language-python}
393406

407+
~~~
408+
True
409+
~~~
410+
{: .output}
411+
394412
For example, the following is equivalent to the above
395413
~~~
396414
mystring_clean = myString.replace(" ","")
397415
print(mystring_clean.isalpha())
398416
~~~
399417
{: .language-python}
400418

401-
402419
~~~
403420
True
404-
0
405-
THE QUICK BROWN FOX
406-
The quick brown fox
407-
False
408-
True
409421
~~~
410422
{: .output}
411423

412424
If you need to refer to a specific element (character) in a string,
413425
you can do so by specifying the index of the character in `[]`
414-
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,
415427
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).
416428

417429
~~~
@@ -513,7 +525,7 @@ False
513525
> bool_val1 = 'TRUE'
514526
> print('read as type ',___(bool_val1))
515527
> print('value when cast to bool',___(bool_val1))
516-
>
528+
>
517529
> bool_val2 = 'FALSE'
518530
> print('read as type ',___(bool_val2))
519531
> print('value when cast to bool',___(bool_val2))
@@ -589,37 +601,37 @@ print(type(list4))
589601
> 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.
590602
> ~~~
591603
> num_list = [4,5,6,11]
592-
>
604+
>
593605
> last_num_in_list = num_list[____]
594606
> print(last_num_in_list)
595-
>
607+
>
596608
> odd_from_list = [num_list[_____], ______]
597609
> print(odd_from_list)
598610
> ~~~
599611
> {: .language-python}
600612
> > ## Solution
601613
> > ~~~
602-
> > # 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
603615
> > num_list = [4,5,6,11]
604-
> >
616+
> >
605617
> > last_num_in_list = num_list[-1]
606618
> > print(last_num_in_list)
607-
> >
619+
> >
608620
> > odd_from_list = [num_list[1], num_list[3]]
609621
> > print(odd_from_list)
610622
> >
611623
> >
612-
> > # 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:
613625
> > import numpy as np
614626
> > num_list = [4,5,6,11]
615-
> >
627+
> >
616628
> > # Converting `num_list` list to an advanced data structure: `numpy array`
617629
> > num_list_np_array = np.array(num_list)
618-
> >
630+
> >
619631
> > # Filtering the elements which produces a remainder of `1`, after dividing by `2`
620632
> > odd_from_list = num_list_np_array[num_list_np_array%2 == 1]
621633
> > print(odd_from_list)
622-
> >
634+
> >
623635
> > # or, Using a concept called `masking`
624636
> > # Create a boolean list `is_odd` of the same length of `num_list` with `True` at the position of the odd values.
625637
> > is_odd = [False, True, False, True] # Mask array

0 commit comments

Comments
 (0)