-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.py
279 lines (202 loc) · 5.38 KB
/
first.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/python3
#print string
print("Hello, world!")
#Print string
print("Hello, world!")
print('Hello, world!')
print("""This string runs
multiple lines!""")
print("This string is "+"awesome!")#we can also concatenate
print('\n')#new line
print('Test that new line out.')
print("\n")
#Math
print(50 + 50) #add
print(50 - 50) #subtract
print(50 * 50) #multiply
print(50 / 50) #divide
print(50 + 50 - 50 * 50 / 50) #PEMDAS
print(50 ** 2) #exponents
print(50 % 6) #modulo - takes what is left over
print(50 / 6) #division with decimals
print(50 // 6) #no remainder
print("\n")
#Variables and Methods
quote = "All is fair in love and war."
print(quote)
print(quote.upper()) #uppercase
print(quote.lower()) #lowercase
print(quote.title()) #title case
print(len(quote)) #counts characters
name = "Heath" #string
age = 33 #int
gpa = 3.7 #float - has a decimal
print(int(age))
print(int(30.1))
print(int(30.9)) #- Will it round? No!
print("My name is " + name + " and I am " + str(age) + " years old.")
age +=1
print(age)
birthday = 1
age += birthday
print(age)
print("\n")
#Functions
print("Here is an example function:")
def who_am_i(): #this is a function without parameters
name = "Heath"
age = 30 #local variable
print("My name is " + name + " and I am " + str(age) + " years old.")
who_am_i()
#adding parameters
def add_one_hundred(num):
print(num + 100)
add_one_hundred(100)
#multiple parameters
def add(x,y):
print(x + y)
add(7,7)
def multiply(x,y):
return x * y
multiply(7,7)
print(multiply(7,7))
def square_root(x):
print(x ** .5)
square_root(64)
def nl():
print('\n')
nl()
nl()
#Boolean expressions (True or False)
print("Boolean expressions:")
bool1 = True
bool2 = 3*3 == 9
bool3 = False
bool4 = 3*3 != 9
print(bool1,bool2,bool3,bool4)
print(type(bool1))
bool5 = "True"
print(type(bool5))
nl()
#Relational and Boolean operators
greater_than = 7 > 5
less_than = 5 < 7
greater_than_equal_to = 7 >=7
less_than_equal_to = 7 <= 7
test_and = True and True #True
test_and2 = True and False #False
test_or = True or True #True
test_or2 = True or False #True
test_not = not True #False
nl()
#Conditional Statements
def drink(money):
if money >= 2:
return "You've got yourself a drink!"
else:
return "No drink for you!"
print(drink(3))
print(drink(1))
def alcohol(age,money):
if(age >= 21) and (money >= 5):
return "We're getting a drink!"
elif (age >= 21) and (money < 5):
return "Come back with more money."
elif (age < 21) and (money >= 5):
return "Nice try, kid!"
else:
return "You're too poor and too young!"
print(alcohol(21,5))
print(alcohol(21,4))
print(alcohol(20,5))
print(alcohol(20,4))
nl()
#Lists - Have brackets []
movies = ["When Harry Met Sally", "The Hangover", "The Perks of Being a Wallflower", "The Exorcist"]
print(1)
print(movies[1]) #returns the second item in the list - index / indices
print(2)
print(movies[0]) #returns the first item in the list
print(3)
print(movies[1:3]) #returns the first number given until right before last number given
print(4)
print(movies[1:4]) #returns all #from 1
print(5)
print(movies[1:]) #returns everything from number to end of list
print(6)
print(movies[:1]) #everything before 1
print(7)
print(movies[:2])
print(8)
print(movies[-1]) #grabs last item
print(9)
print(len(movies)) #counts items in list
movies.append("JAWS")
print(movies) #appends to end of list
movies.insert(2, "Hustle")
print(movies)
movies.pop() #removes last(first?) item
print(movies)
movies.pop(0) #removes first item
print(movies)
amber_movies = ['Just Go With It', '50 First Dates']
our_favorite_movies = movies + amber_movies
print(our_favorite_movies)
grades = [["Bob", 82], ["Alice", 90], ["Jeff", 73]]
bobs_grade = grades[0][1]
print(bobs_grade)
grades[0][1] = 83
print(bobs_grade)
nl()
#Tuples - Do not change, ()
grades = ("a", "b", "c", "d", "f")
#grades.pop, grades.append won't work - not mutable
print(grades[1])
nl()
#For loops - start to finish of an iterate
vegetables = ["cucumber", "spinach", "cabbage"]
for x in vegetables:
print(x)
#While loops - execute as long as true
i = 1
while i < 10:
print(i)
i += 1
nl()
#ADVANCED STRINGS
my_name = "Heath"
print(my_name[0]) #first letter
print(my_name[-1]) #last letter
sentence = "This is a sentence."
print(sentence[:4])
print(sentence.split()) #delimeter - default is a space
sentence_split = sentence.split()
sentence_join = ' '.join(sentence_split)
print(sentence_join)
quote = "He said, 'give me all your money'" # show example
quote = "He said, \"give me all your money\""
print(quote)
too_much_space = " hello "
print(too_much_space.strip())
print("A" in "Apple") #returns true
print("a" in "Apple") #returns false - case sensitive
letter = "A"
word = "Apple"
print(letter.lower() in word.lower()) #improved
movie = "The Hangover"
print("My favorite movie is {}.".format(movie))
print("My favorite movie is %s" % movie)
print(f"My favorite movie is {movie}")
nl()
#DICTIONARIES - key/value pairs {}
drinks = {"White Russian": 7, "Old Fashion": 10, "Lemon Drop": 8} #drink is key, price is value
print(drinks)
employees = {"Finance": ["Bob", "Linda", "Tina"], "IT": ["Gene", "Louise", "Teddy"], "HR": ["Jimmy Jr.", "Mort"]}
employees['Legal'] = ["Mr. Frond"] #adds new key:value pair
print(employees)
employees.update({"Sales": ["Andie", "Ollie"]}) #adds new key:value pair
print(employees)
drinks['White Russian'] = 8
print(drinks)
print(drinks.get("White Russian"))
nl()