-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_00_basic.py
More file actions
149 lines (104 loc) · 2.84 KB
/
lesson_00_basic.py
File metadata and controls
149 lines (104 loc) · 2.84 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''
Multi-line comments go between 3 quotation marks.
You can use single or double quotes.
'''
# One-line comments are preceded by the pound symbol
# BASIC DATA TYPES
x = 5 # creates an object
print(type(x)) # check the type: int (not declared explicitly)
type(x)
# LISTS
nums = [5, 5.0, 'five'] # multiple data types
print(nums)
print(type(nums))
print(len(nums))
print(nums[0])
nums[0] = 6
print(nums[0])
nums.append(7)
print(nums)
nums.remove('five')
print(sorted(nums))
print(nums)
print(sorted(nums, reverse=True))
# list slicing [start:end:step]
weekdays = ['mon', 'tues', 'wed', 'thurs', 'fri']
print(weekdays[0]) # element 0
print(weekdays[0:3]) # element 0-2
print(weekdays[:3]) # element 0-2
print(weekdays[3:]) # element 3, 4
print(weekdays[-1]) # last element
print(weekdays[::2]) # every second element
print(weekdays[::-1]) # backwards
days = weekdays + ['sat', 'sun']
print(days)
# FUNCTIONS
def give_me_five():
return 5
print(give_me_five())
def calc(x, y, op='add'):
if op == 'add':
return x+y
elif op == 'subtract':
return x-y
else:
print('Valid operations: add, substract')
print(calc(5, 3, 'add'))
print(calc(5, 3, 'subtract'))
print(calc(5, 3, 'multiply'))
print(calc(5, 3))
# Write a function that takes two parameters (hours and rate), and
# return the total pay.
def compute_pay(hours, rate):
return hours * rate
print(compute_pay(40, 10.50))
def compute_more_pay(hours, rate):
if hours <= 40:
return hours * rate
else:
return 40 * rate + (hours-40) * (rate*1.5)
print(compute_more_pay(30, 10))
print(compute_more_pay(45, 10))
def both_ends(s):
if len(s) < 2:
return ''
else:
return s[:2] + s[-2:]
print(both_ends('spring'))
print(both_ends('cat'))
print(both_ends('a'))
# FOR LOOPS
# range returns a list of integers
print(range(0, 3))
for i in range(5):
print(i)
# print each element in uppercase
fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
print(fruits[i].upper())
# better for loop
for fruit in fruits:
print(fruit.upper())
# EXERCISE: Write a program that prints the numbers form 1 to 100. But for
# multiples of 3 print 'fizz' instead of the number, and for the multiples of 5
# print 'buzz'. For numbers which are multiples of both 3 and 5 print 'fizzbuzz'.
def fizz_buzz():
nums = range(1, 100)
for num in nums:
if num % 15 == 0:
print(num, 'fizzbuzz')
elif num % 3 == 0:
print(num, 'fizz')
elif num % 5 == 0:
print(num, 'buzz')
print(fizz_buzz())
def front_x(words):
lista = []
listb = []
for word in words:
if word[0] == 'x':
lista.append(word)
else:
listb.append(word)
return sorted(lista) + sorted(listb)
print(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']))