Skip to content

Commit 25147be

Browse files
author
Anastomose
committed
Adding list lab, string functions, and class notes.
new file: ../../../Students/ebuer/list_lab.py new file: ../../../Students/ebuer/session_03/s3_notefile.txt new file: ../../../Students/ebuer/session_03/string_funcs.py
1 parent 683bf49 commit 25147be

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎Students/ebuer/list_lab.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#@python: 2
2+
3+
listo = ['Apples', 'Pears', 'Oranges', 'Peaches']
4+
print listo
5+
6+
usr_fruit = raw_input("Please add another fruit: ")
7+
listo.append(usr_fruit)
8+
print listo, '\n'
9+
10+
usr_number = raw_input('And now provide a number: ')
11+
list_value = int(usr_number)-1
12+
13+
if list_value < len(listo):
14+
print 'Value %i is %s\n' %(int(usr_number), listo[list_value])
15+
else:
16+
print "Sorry, there is no value there.\n"
17+
18+
print 'Let me add something for you, like an Avocado.'
19+
listo = ['Avocado'] + listo
20+
print listo
21+
22+
print 'And now a mango.'
23+
listo.insert(0, 'Mango')
24+
print '%s\n' %listo
25+
26+
for fruit in listo:
27+
if 'P' in fruit[0]:
28+
print fruit
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Session 03 notes from class
2+
ord () is the ordinal of a string value. This allows us to quickly alphabetize strings. Cap letters are indexed in front of lower case.
3+
4+
string.index() locates the first instance of a specified string passed to the operator.
5+
6+
string.count() acts as you'd expect it to with the count looking for the argument passed.
7+
8+
raw_input() takes a string that is presented to the user -- this function always returns a string, if you want an integer or float you must cast the value
9+
10+
float()
11+
int()
12+
etc
13+
14+
input() trys to make an intelligent guess as to what type the input is. This is a bad habit to get into using since it will accept bad inputs and attempt to run them, allowing for reverse injection attacks.
15+
16+
enumerate() will return the index for each value in a list
17+
18+
FLOW CONTROL
19+
break -- terminates loop
20+
21+
continue -- skips everything below the continue line and returns to the top of the loop
22+
23+
else on a "FOR" loop -- else is executed if the loop does not break, e.g. it runs to completion.
24+
25+
string.split(argument) -- splits string on argument passed
26+
string.join(argument) -- join list values into a string with argument separators
27+
28+
strings are immutible, so all string operations return a new string.
29+
30+
.isnumeric()
31+
.isalnum()
32+
.isalpha()
33+
etc
34+
35+
36+
ord() translates a character to ordinal value
37+
char() translates values into characters
38+
39+
string.format() -- most current formatting tool, pretty powerful
40+
41+
string function lab
42+
list lab
43+
string formatting lab
44+
homework
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#@python: 2
2+
3+
test_string = 'The quick brown fox jumped over the lazy dog'
4+
test_string2 = 'Relaxing in basins at the end of inlets \
5+
terminates the endless tests from the box'
6+
7+
8+
def switcher(str):
9+
return str[-1] + str[1:-1] + str[0]
10+
11+
12+
def skipper(str):
13+
return str[0:50:2]
14+
15+
16+
ans = skipper(test_string2)
17+
print (ans)
18+

0 commit comments

Comments
 (0)