-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonNotesBasic
286 lines (218 loc) · 6.33 KB
/
PythonNotesBasic
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
279
280
281
282
283
284
285
#IF LOOP
x = 5
if x < 10:
print('Smaller')
if x > 20:
print('Bigger')
print('Finish the IF statement')
#Nested IF
x =4
if x > 4:
print('einai megaliteros apo to :',x)
elif x<4:
print('einai mikrotero')
else:
print('ante re .. to brikes',x)
# Types
print('HOW TO CHANGE str variable --> integer (using int(string))')
skata='123'
print(skata)
print(type(skata))
skatanumber=int(skata)
print(skatanumber)
print(type(skatanumber))
#try/except
#if you arent sure if something blue up the code , you use those ones
#so its like, i know it may blow but i dont want my code to die.
astr='Hello Bob'
try:
print('trying the exception')
istr=int(astr)
except:
istr=-1
print('If you see this code means except activaded so value is -1')
print(istr)
#FUNCTIONS
print('There are 2 kind of Functions: Build-in functions like')
print('like float() etc and Fucntions taht we define ourselves')
def greet(lang): #<-How you make FUNCTIONs
if lang=='es':
print('Hola')
elif lang=='fr':
print('Bonjoour') #<--IF statement
else:
print('Hello')
return(lang) #<--- this is what returns
greet('fr')
print(greet('brbbr'))
print('Hello with language',greet('fr'))
#While loop
n=5
while n > 0:
print(n)
n = n -1
print('Final value of n = ', n)
#WHILE loop + break
n=5
while n > 0:
print(n)
n = n -1
if n ==3:
print('Break loop at 3')
break
#WHILE TRUE
while True:
line=input('Grapse kati # agnonei me continue Done kanei break')
if line[0]=='#': #<---If you type #asdas it wont do anything
continue
if line =='done': #<-- If you type 'done' , program will finish
break
print(line) #<-- prints what you typed
print('Finished program')
#FOR LOOP
for i in [5,4,3,2,1]:
print(i)
print('Finished for loop ')
friends=['Alexandra','Takis','Alex']
for friend in friends:
print('kalispera file : ', friend)
print('Completed Greetings in loop of LIST')
#Concatenation(combining strings)
first_name='Albert'
last_name='Einstein'
fullname=first_name + ' ' + last_name
print(fullname)
count=0
print('the print adds a new line everytime it prints.')
print('So even if we have okay code it prints the new lines')
file_handle=open('/Users/konstantinossiachras/Desktop/Python Scripting/skata.txt')
for line in file_handle:
print(line)
count=count +1
print(count)
print('To correct this problem: make the gaps dissapear')
#Every line into the file has \n at the end. So when print() gives \n as well . We have 2 lines
#To make this better we need to use .rstrip
#line=line.rstrip()
count=0
print('the print adds a new line everytime it prints.')
print('So even if we have okay code it prints the new lines')
file_handle=open('/Users/konstantinossiachras/Desktop/Python Scripting/skata.txt')
for line in file_handle:
line=line.rstrip()
print(line)
count=count +1
print(count)
print('*************************************')
file_handle=open('/Users/konstantinossiachras/Desktop/Python Scripting/skata.txt')
for line in file_handle:
line=line.rstrip()
if not line.startswith('I '):
continue
print(line)
print('**********Ignore lines that dont have @hotmail.com ***********************')
file_handle=open('/Users/konstantinossiachras/Desktop/Python Scripting/skata.txt')
for line in file_handle:
line=line.rstrip()
if not '@hotmail.com' in line:
continue
print(line)
x =list()
type(x)
dir(x)
print(type(x))
print(dir(x))
print('dir(x) if you wanna check if you want to check what a type of variable can do')
print('if you wanna check if somehing is in a list. Use "in" or "not in"')
#How to split words, and this returns them like a list
abc = ' With three words'
stuff = abc.split()
print(stuff)
print(len(stuff))
print(stuff[0])
print(stuff[2])
print(stuff[:])
print('You can always put a for loop after to check all the words for something')
print('And you can aways do a second split(''), this way for example you specify the parameter, in this case you split mails')
#How to split words from a text, and this returns them like a list
file_handle=open('/Users/konstantinossiachras/Desktop/Python Scripting/skata.txt')
for line in file_handle:
line=line.rstrip()
if not line.startswith('I am'): continue
words=line.split()
print(words)
#EMPTY LIST
print([])
#LIST INSIDE LIST
print([1,[2,5],3])
#Concatenating Lists using +
a=[1,2,3]
b=[4,5,6]
c=a+b
print(c)
#If you want to create a List fast
list_of_ints = list(range(3))
print(list_of_ints)
print(len(list_of_ints))
#If you want to make a list of the result of something you can just do it(in this case i just take a length)
print(list(range(len(list_of_ints))))
#LOOP in List
friends=['John','George','Alex']
print(friends)
print(friends[1])
for friend in friends:
print('Happy new Year: ', friend)
#LOOP in list using range , more complicate but you can have a counter , its good to use it if we need to change
#something, or you need to know where you currently are.
friends=['John','George','Alex']
for i in list(range(len(friends))):
friend=friends[i]
print('happy new year :', friend)
#Lists can be slised using
t=[1,2,3,4,5,6,7,8,9]
print(t[1:3])
print(t[:4])
print(t[3:])
#Building a List with .append (starting with an empty list)
stuff=list()
stuff.append('ante')
stuff.append(99)
print(stuff)
#SORT a list , this alterts the list
friends=['John','George','Alex']
print(friends)
friends.sort()
print(friends)
#DICNTIONARIES ARE FASTER and its random located inside
# List is a collection of values in order but dictionaries like random staff')
#Dictionaries have labels to define what is inside , So they has KEY / VALUES')
purse=dict()
purse['money']=12
purse['candy']=3
purse['tissue']=75
purse['candy']=purse['candy'] +2
print(purse)
skatadict={'skata1':1 , 'skata2':2, 'skata3':3}
print(skatadict)
#LIST EXERCISE
fhand=open('/Users/konstantinossiachras/Desktop/Python Scripting/skata.txt')
for line in fhand:
line=line.rstrip()
#This is the 1st way to avoid blank lines creating issue
#if line= "" : continue
words=line.split()
#this is 2nd way to avoid blank lines creating issues
#if len(words) < 1 : continue
#this is the 3rd way
if words == [] : continue
if words[0] != 'I' : continue
print(words[5])
year=2000
if (year % 4==0):
if (year % 400 ==0):
leap=False
elif (year % 100 ==0):
leap=False
else:
leap=True
print(leap)