if 1 == 1:
print "wow"
elif not "1" == 1:
print "so much wow"
else:
print "if all else fails trust the else"
False | True |
False (== 0) | True (== 1) |
"" (empty string) | any string but "" (" ", "anything") |
0, 0.0 | any number but 0 (1, 0.1, -1, 3.14) |
[], (), {}, set() | any non-empty container ([0], (None,), ['']) |
None | almost any object that's not explicitly False |
myList = [1,2,"3"]
myList.append("test")
myList.remove("test")
print myList[1] # prints 2
myDict = {"key": "val"}
myDict['new key'] = "new val"
del myDict['new key'] # del is global
print myDict["key"] # prints "val"
mySet = set(1,2,3)
mySet.append(1)
mySet.remove(2)
print mySet[2] # prints "3"
len(collection) # prints the size
myList = [1,2,3]
for var in myList:
print var
while 1==1:
print "Im crashing ur computer..."
myDict = {"some key": "some val", 1: 2}
for key, val in myDict.items():
print key, ":", val # prints some key : some val\n1 : 2
for key in myDict.keys():
print key # prints some key\n1
for key in myDict:
print key # prints some key\n1
myDict = {
1:3,
2:2,
3:1
}
for key, val in sorted(myDict.items()):
print key, val
"Hard"
for key, val in sorted(myDict.items(), key=lambda t: t[1]):
print key, val
for key in sorted(myDict, key=lambda key: myDict[key]):
print key, myDict[key]
theList = [1,2,3,1,2,3,1,2,3,1,2,3,1,3,2,1,2,3,1,3,2,1]
theDict = {}
for number in theList:
if number not in theDict:
theDict[number] = 0
theDict[number] += 1
The "right" way:
for number in theList:
theDict.setdefault(number, 0)
theDict[number] += 1
theList = [1,2,3,1,2,3,1,2,3,1,2,3,1,3,2,1,2,3,1,3,2,1]
theDict = {}
for number in theList:
if number not in theDict:
theDict[number] = []
theDict[number].append("BO!")
The "right" list way:
for number in theList:
theDict.setdefault(number, []).append("BO!")
print "hello world 2.X"
print("hello world 3.X")
print "one", "two" # prints "one two\n"
print("one", "two") # prints "one two\n"
print "no", "newline", # prints "no newline"
print("no", "newline", end="") # prints "no newline"
print("vv%svv" % 123) # prints "vv123vv\n"
print("%s: %s" % ("one", "two")) # prints "one: two\n"
print('%d-%0d-%5d-%05d' % (34, 34, 34, 34)) # prints "34-34- 34-00034\n"
f = open('myfile.txt') # contains "1,2,3\n4,5,6"
for line in f:
first, second, third = line.strip().split(',')
print(second) # prints 2\n5
def someFunc(someParam):
return someParam + 1
class MyClass():
member = 1
def method(self, methodParam):
return methodParam + self.member
class MySecondClass(MyClass):
def someThing(self):
return member
class myClass:
x = "text"
class C(object):
_x = 0
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value+2000
print("hello world")
python hello.py
def main():
print myfunk(1)
def myfunk(n):
return n + 2
if __name__ == '__main__': main()
import filename
import filename, filename2
import method from filename
import method from filename as newName
sys <-- used to get argv
os <-- used to get system variables and the like
re <-- regex
":".join([1,2,3]) # becomes "1:2:3"
elif: # why not else if???
__someFunk__() # "private" methods
if 44 < var < 50:
if var % 2:
return true
auto unpacking
one, two, three = [1,2,3]
oldArray = [[1,2],[3,4],[5,6]]
newArray = [x[1] for x in oldArray]
if not someTruthyVar: doSomething()
if someVar in list: doSomethingElse()