-
Notifications
You must be signed in to change notification settings - Fork 1
/
py-tutorial.py
129 lines (109 loc) · 3.5 KB
/
py-tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# WHILE LOOP
#
# In this problem, write a function named "my_while_loop" that accepts an
# iterable of strings as a parameter and returns a new list with strings from
# the original list that are longer than five characters.
#
# The function must use a while loop in its implementation.
# TEST DATA
# test = ["nope", "yes this one", "not", "uhuh", "here's one", "narp"]
# print(my_while_loop(test)) # > ["yes this one", "here's one"]
#
# test = ["plop", "", "drop", "zop", "stop"]
# print(my_while_loop(test)) # > []
#
# test = []
# print(my_while_loop(test)) # > []
#NOTES:
# (method) append: (__object: Any) -> None :Append object to the end of the list.
# len: [ function ] Return the number of items in a container.
# (function) len: (__obj: Sized) -> int
# the list, which can be written as a list of comma-separated values (items) between square brackets.
# Lists might contain items of different types,
# but usually the items all have the same type.
squares = [1, 4, 9, 16, 25]
#Like strings (and all other built-in sequence types),
# lists can be indexed and sliced:
print(squares[0]) # indexing returns the item
print(squares[-1])
# [Running] python -u "c:\Users\15512\Google Drive\a-A-September\misc\Non-App-Academy-Exploration\python\my-intro-BG\0Intro2Python-all.py"
# 1
# 25
print("----------------------------While LOOP-----------------------------------")
#**********************************************************************
def my_while_loop(lst):# (function) my_while_loop: (lst) -> List
new_lst = [] # (variable) new_lst: List
i = 0
while i < len(lst):
if len(lst[i]) > 5:
new_lst.append(lst[i])
i += 1
print("i",i)
return new_lst
test = ["nope", "yes this one", "not", "uhuh", "here's one", "narp"]
print(my_while_loop(test))
test = ["plop", "", "drop", "zop", "stop"]
print(my_while_loop(test))
test = []
print(my_while_loop(test))
# [Running] python -u "c:\Users\15512\Google Drive\a-A-September\misc\Non-App-Academy-Exploration\python\my-intro-BG\0Intro2Python-all.py"
# 1
# 25
# i 1
# i 2
# i 3
# i 4
# i 5
# i 6
# ['yes this one', "here's one"]
# i 1
# i 2
# i 3
# i 4
# i 5
# []
# []
#*****************************************************************************
# FOR LOOP
#
# In this problem, write a function named "my_for_loop" that accepts an
# iterable of strings as a parameter and returns a new list with strings from
# the original list that are longer than five characters. The function must use
# a for loop in its implementation.
#
print("----------------------------FOR LOOP-----------------------------------")
# WRITE YOUR FUNCTION HERE
def my_for_loop(lst):
new_lst = []
for item in lst:
if len(item) > 5:
new_lst.append(item)
return new_lst
# TEST DATA
test = ["nope", "yes this one", "not", "uhuh", "here's one", "narp"]
print(my_for_loop(test)) # > ["yes this one", "here's one"]
test = ["plop", "", "drop", "zop", "stop"]
print(my_for_loop(test)) # > []
test = []
print(my_for_loop(test)) # > []
# 1
# 25
# ----------------------------While LOOP-----------------------------------
# ('i', 1)
# ('i', 2)
# ('i', 3)
# ('i', 4)
# ('i', 5)
# ('i', 6)
# ['yes this one', "here's one"]
# ('i', 1)
# ('i', 2)
# ('i', 3)
# ('i', 4)
# ('i', 5)
# []
# []
# ----------------------------FOR LOOP-----------------------------------
# ['yes this one', "here's one"]
# []
# []