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
For example, the following is equivalent to the above
405
413
~~~
406
414
mystring_clean = myString.replace(" ","")
407
415
print(mystring_clean.isalpha())
408
416
~~~
409
417
{: .language-python}
410
418
419
+
~~~
420
+
True
421
+
~~~
422
+
{: .output}
423
+
411
424
If you need to refer to a specific element (character) in a string,
412
425
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,
414
427
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).
415
428
416
429
~~~
@@ -512,7 +525,7 @@ False
512
525
> bool_val1 = 'TRUE'
513
526
> print('read as type ',___(bool_val1))
514
527
> print('value when cast to bool',___(bool_val1))
515
-
>
528
+
>
516
529
> bool_val2 = 'FALSE'
517
530
> print('read as type ',___(bool_val2))
518
531
> print('value when cast to bool',___(bool_val2))
@@ -588,37 +601,37 @@ print(type(list4))
588
601
> 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.
589
602
> ~~~
590
603
> num_list = [4,5,6,11]
591
-
>
604
+
>
592
605
> last_num_in_list = num_list[____]
593
606
> print(last_num_in_list)
594
-
>
607
+
>
595
608
> odd_from_list = [num_list[_____], ______]
596
609
> print(odd_from_list)
597
610
> ~~~
598
611
> {: .language-python}
599
612
> > ## Solution
600
613
> > ~~~
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
602
615
> > num_list = [4,5,6,11]
603
-
> >
616
+
> >
604
617
> > last_num_in_list = num_list[-1]
605
618
> > print(last_num_in_list)
606
-
> >
619
+
> >
607
620
> > odd_from_list = [num_list[1], num_list[3]]
608
621
> > print(odd_from_list)
609
622
> >
610
623
> >
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:
612
625
> > import numpy as np
613
626
> > num_list = [4,5,6,11]
614
-
> >
627
+
> >
615
628
> > # Converting `num_list` list to an advanced data structure: `numpy array`
616
629
> > num_list_np_array = np.array(num_list)
617
-
> >
630
+
> >
618
631
> > # Filtering the elements which produces a remainder of `1`, after dividing by `2`
0 commit comments