@@ -468,3 +468,178 @@ def complex_function(arg1, arg2, kwarg1=u'bannana'):
468468 # Do something interesting here
469469 # It will only happen when the module is run
470470
471+
472+ ############################## SESSION03 ##############################
473+
474+ """
475+ git question:
476+ - create a pull request for the branch itself
477+ """
478+
479+ #SEQUENCE
480+ """
481+ sequence types:
482+ -srings
483+ -unicode strings
484+ -lists
485+ -tuples
486+ -bytearrays
487+ -buffers
488+ -array.arrays
489+ -xrange objects (almost)
490+
491+ indexing:
492+ -items in a sequence my be looked up with an index using the subscription
493+ operator "[]"
494+
495+ slicing:
496+ -returning multiple elements of a sequence
497+
498+ indexing vs. slicing:
499+ -a slice always returns a sequence
500+ -slicing does not return an error when you're out of bounds in a sequence
501+
502+ membership:
503+ -you can use "in" or "not in"
504+
505+ similar features with sequences:
506+ -min()
507+ -max()
508+ -.index()
509+ -.count()
510+ -all support iteration
511+
512+ lists:
513+ -you can build lists with "list()" (e.g. list("salim"))
514+ -lists can contain all different object types
515+ -ways to make a list bigger
516+ -append() <-- adds a single item to the end
517+ -insert() <-- adds a single item
518+ -extend() <-- add multiple items to the end
519+ -shrinking the list
520+ -pop()
521+ -remove()
522+ -use "del" with a slice
523+ -misc methods:
524+ -.reverse()
525+ -.sort()
526+ -generally, methods that change a mutable object return None. As opposed
527+ to methods on immuatble objects return a copy of the changed object.
528+ -list performance:
529+ -indexing is fast (i.e., Order 1)
530+ -x in s is slow (i.e., Order n)
531+ -looping is slow (i.e., Order n)
532+ -operating on the end of the list if fast (i.e., Order 1)
533+ -operating on the front or middle is slow (i.e., Order n)
534+ -this is because you need to move all the elements to the front
535+ tuples:
536+ -you don't actually need the "(" and ")"; tuples rely on the commas
537+ -therefore a tuple with one element is (1,); i.e., you need the comma
538+
539+
540+ lists vs. tuples
541+ -the difference between each is mutability
542+ -mutable: it can be changed in place
543+ -immutable: cannot be changed in place
544+
545+ immutable objects (that we know so far):
546+ -unicode
547+ -string
548+ -integer
549+ -float
550+ -tuple
551+
552+ mutable objects (that we know so far):
553+ -list
554+
555+ loops in python:
556+ -no indexing is required
557+ -you can use "enumerate()" if you want both the index and the elements of
558+ the object
559+ -to stop a loop early:
560+ -break: this stops and exits the loop
561+ -continue: this moves the loop back to the start
562+ -loops can have an "else" statement
563+ -else executes when you didn't break out of the loop early
564+ -"while" loops
565+ -these will execute until something is true
566+
567+ more string features:
568+ -split(): allows you to break a string into a list based on a delimiter
569+ -join(): concatenates strings in a list
570+ -upper(): uppercase
571+ -lower(): lowercase
572+ -isnumeric():
573+ -isalnum():
574+ -isalpha():
575+
576+ ordinal values
577+ -chr(): takes a number and converts to character
578+ -ord(): takes a string and coverts to a number
579+ """
580+ #example of indexing with strings
581+ s = "this is a string"
582+ s [0 ]
583+
584+ #example of indexing with lists
585+ list = [1 , 2 , 3 , 5 , 6 ]
586+ list [- 1 ] # will return 6
587+
588+ #example of slicing
589+ s [:5 ]
590+ s [5 :]
591+ s [:- 4 ]
592+
593+ """
594+ slicing lab
595+ -return a string with the first and last characters exchanged.
596+ -return a string with every other character removed
597+ -return a string with the first and last 4 characters removed, and every other char in between
598+ -return a string reversed (just with slicing)
599+ -return a string with the middle, then last, then first third in the new order
600+ """
601+
602+ #slicing lab
603+ s = "this is a string"
604+
605+ #return a string with the first and last characters exchanged.
606+ def first_last (s ):
607+ return s [- 1 ] + s [1 :- 1 ] + s [0 ]
608+
609+ #return a string with every other character removed
610+ def every_other (s ):
611+ return s [::2 ]
612+
613+ #return a string with the first and last 4 characters removed, and every other char in between
614+ def random (s ):
615+ return s [1 :- 4 :2 ]
616+
617+ #return a string reversed (just with slicing)
618+ def reversed (s ):
619+ return s [::- 1 ]
620+
621+ #return a string with the middle, then last, then first third in the new order
622+ def thirds (s ):
623+ s_len = len (s ) / 3
624+ first = s [:s_len ]
625+ last = s [- s_len :]
626+ middle = s [s_len :- s_len ]
627+ return middle + last + first
628+
629+ #making a copy of a list
630+ original = [1 , 2 , 3 ]
631+ new = original [:]
632+ """
633+ this creates a "shallow" copy. therefore, if you have nested lists, the sub lists
634+ will not be copies. to make a "deep" copy you can use a library or use loops.
635+ """
636+
637+ #this does not make a new list
638+ original = [1 , 2 , 3 ]
639+ new = original
640+
641+ #do not us a mutable object as the default object in a function
642+ def func (c , list = []): # this is wrong because it dones't create a function level list
643+ return list .append (c )
644+
645+
0 commit comments