-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiscellaneous.py
More file actions
executable file
·97 lines (72 loc) · 2.25 KB
/
miscellaneous.py
File metadata and controls
executable file
·97 lines (72 loc) · 2.25 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
#!/usr/bin/python
import functools
import getpass
import sys
username = getpass.getuser()
if (username == "root"):
print "You shouldn't run this as root"
else:
print "You're not root. That's good"
lst1=[1, 2, 3, 4]
lst2=[40, 30, 20, 10]
print "lst1=", lst1
print "lst2=", lst2
#mapping a function to a list
strlst=map(str, lst1)
print "strlst = map(str, lst1) = ", strlst
#joining a list into a string using a seperator
jn=' '.join(strlst)
print "' '.join(strlst)=", jn
#zipping two lists together
zp=zip(lst1, lst2, strlst)
print "zp= zip(lst1, lst2, strlst) =", zp
zp.sort(key=lambda x:x[1]) #tells sort to use the second element in each list element
print "zp.sort(key=lambda x:x[1])=", zp
lst3=range(5, -5, -1)
print "lst3=", lst3
#lambda
negs=filter(lambda x: x<0, lst3)
print "filter(lambda x: x<0, lst3)=", negs
#reduce
product=reduce ((lambda x, y: x*y), lst1)
print "reduce ((lambda x, y: x*y), lst1)=", product
#for-else example
#while-else is also legal
def printlist(start, end, quitval):
for a in range (start, end):
print a,
if (a==quitval):
print "quit"
break
else: #executed if the for loop ends (breaks) early
print "Reached the end of the list"
printlist(0, 10, 5)
printlist(0, 10, 11)
printlist_partialfunc=functools.partial(printlist, 0, 10)
#defines a partial function identical to printlist, except the first argument is pegged
#to 0 and the second argument is pegged to 10
printlist_partialfunc(5)
printlist_partialfunc(11)
#try-except-else
try:
print "Executing try statement : ",
except:
print "Whoops, something went wrong"
else:
print "Nothing went wrong"
#list comprehensions
nums=range(0, 10)
squares=[i**2 for i in nums] #simple
print "First 10 square numbers:", squares
evens=[i for i in nums if i%2==0] #conditional
print "First 5 even numbers:", evens
filterlist=[i if i%2==0 else 0 if i%3==0 else -1 for i in nums] #conditional with if/else
print "Filtered list=", filterlist
#underscores are used when the programmer doesn't care about a value
for _ in range(0, 3):
print "Hi!"
[x, _, z] = "Hello. Cat. Moose".split(".")
#an asterisk is used to convert a list into comma-seperated parameters in a function call
def add4(a, b, c, d):
return a+b+c+d
print add4(*[1, 2, 3, 4])