Skip to content

Commit 819e43c

Browse files
committed
Merge pull request UWPCE-PythonCert#42 from salimhamed/master
Session 3 Homework looks good.
2 parents c1b32a4 + 7a06bb7 commit 819e43c

7 files changed

Lines changed: 443 additions & 1 deletion

File tree

‎Students/salim/notes/class_notes.py‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

‎Students/salim/notes/vim_notes.txt‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
e""MODES"""
22
ESC "Normal Mode
33
i "insert mode (see more on insert mode below)
4-
"visual mode (allows you to select text)
4+
v "visual mode (allows you to select text)
55
V "visual mode lines (allows you to select lines)
6+
<crtl + v> "block visual mode
7+
8+
9+
"""BUFFERS"""
10+
:bn "next buffer
11+
:bp "previous buffer
12+
:bd "delete buffer
13+
Crtl-^ "switch to last buffer
14+
:ls "show all buffers
615

716

817
"""WINDOWS"""
-12 KB
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
# print original list
4+
a_list = ['Apples', 'Pears', 'Oranges', 'Peaches']
5+
print a_list
6+
7+
# ask user to add a fruit
8+
user_input = raw_input('Please provide another fruit: ')
9+
10+
# add the fruit to the list and print the new list
11+
a_list.append(user_input)
12+
print a_list
13+
14+
# ask user for a number and print the corresponding fruit
15+
user_number = int(raw_input('Please provide a number: '))
16+
print 'This number is %d, which matches fruit %s.' % (user_number, a_list[user_number - 1])
17+
18+
# add another fruit to the start of list using "+"
19+
a_list = ['Bananna'] + a_list
20+
print a_list
21+
22+
# add another fruit to the start of list using "insert"
23+
a_list.insert(0, 'Avacado')
24+
print a_list
25+
26+
# display all fruits that start with "P"
27+
p_list = []
28+
for fruit in a_list:
29+
if fruit[0].lower() == 'p':
30+
p_list.append(fruit)
31+
print p_list
32+
33+
# remove last fruit from list
34+
del a_list[-1]
35+
print a_list
36+
37+
# ask user for fruit to delete, then delete
38+
user_delete = raw_input('Type a Fruit to delete: ')
39+
a_list.remove(user_delete)
40+
41+
# loop through and ask the user if they like each fruit
42+
for fruit in a_list:
43+
user_delete = raw_input('Do you like %s? ' % fruit.lower())
44+
45+
# correct the entry if it's not yes or no
46+
while not (user_delete.lower() == 'yes' or user_delete.lower() == 'no'):
47+
user_delete = raw_input('Please enter "Yes" or "No". Try again.')
48+
49+
# delete the fruit from the list if answered "No"
50+
if user_delete.lower() == 'no':
51+
a_list.remove(fruit)
52+
53+
print a_list
54+
55+
# make a copy of the list
56+
copy_list = a_list[:]
57+
58+
# reverse letters
59+
for idx, fruit in enumerate(copy_list):
60+
copy_list[idx] = fruit[::-1]
61+
62+
# delete last item of original list
63+
del a_list[-1]
64+
65+
# print both lists
66+
print 'Original List: %s' % a_list
67+
print 'Copy List: %s' % copy_list

0 commit comments

Comments
 (0)