Last active
December 26, 2015 14:28
-
-
Save j0lvera/7165431 to your computer and use it in GitHub Desktop.
python in a nutshell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multiline | |
print 'something\ | |
else' | |
print """algo | |
>> algo """ | |
# Length | |
len('string') | |
len(variable) | |
# Slicing | |
a = 'string' | |
a[0] # 's' | |
a[-1] # 'g' | |
a[1:4] # 'trin' | |
a[1:] # 'tring' | |
# Capturing from user | |
raw_input('Msg to user here') | |
# Lists | |
a = ['one', 'two', 'three'] | |
nested = ['four', 'five', 'six', a] | |
>>> nested | |
['four', 'five', 'six', ['one', 'two', 'three']] | |
'four' in nested # true | |
'one' in nested # false | |
nested[1] # 'four' | |
nested[-1] # ['one','two','three'] | |
nested[-1][1] # 'two' | |
## Slicing Lists | |
nested[0:2] # ['four', 'five'] | |
nested[2:-2] # ['three'] | |
nested[1] = 'nine' # replacing 'five' with 'nine' | |
nested.append('string') # appending (insert in the end of the list) | |
nested.insert(1, 'string') # first argument is the index, and the string | |
nested.extend(another_list) # insert another list elements into nested, just take the content | |
nested.remove('nine') # removing 'nine' from the list | |
# Tupal, is the same but you can't change it, once is created, it permanent as that | |
# you basically can grab data, the same way that lists, but not changing it | |
b = 'one', 'two', 'three' | |
>>> b | |
('one', 'two', 'three') | |
b[1] = '2' # ERROR! not mutable | |
print b[1] # 'two' | |
# Sets, pretty the same as Lists, but not duplicates | |
c = ['honda','ford','dodge','honda'] | |
autos = set(c) | |
d = ['honda'] | |
cars = set(d) | |
>>> autos | |
set(['honda','ford','dodge']) # just return the unique elements | |
>>> autos - cars | |
set(['ford','dodge']) | |
>>> autos & motos | |
set(['honda']) # what element have in common both Sets | |
# Dictionaries, like JSON | |
dict = {'key': 'value'} | |
dict['key'] # return 'value' | |
del dict['key'] # delete the key value | |
dict.keys() # return only the keys: ['key'] | |
>>> 'key' in dict | |
True | |
# Conditionals | |
name = raw_input('Please type in your name: ') | |
if len(name) < 5: | |
print "Your name has fewer than 5 chars" | |
elif len(name) == 5: | |
print "Your name has exactly 5 chars" | |
if name == "Jesse": | |
print "Hey Jesse!" | |
else: | |
print "Your name has greater than 5 chars" | |
## another example | |
language = raw_input('Please enter a programming language') | |
if language in ['C++', 'Python', 'Java']: | |
print language, "rocks" | |
if language not in ['C++', 'Python', 'Java']: | |
print language, "not here" | |
if language == "C++" or language == "Python": | |
print "Nice!" | |
else: | |
print "Nothing here!" | |
# Loops | |
languages = ['python', 'java', 'php'] | |
for language in languages: | |
print language, "rocks!" | |
## another example | |
dict = {"name":"Juan", "location":"TX"} | |
for key in dict: | |
print "His", key, "is", dict[key] | |
## range | |
print range(10) # return [0,1,2,3,4,5,6,7,8,9] | |
for int in range(10): | |
print "i =", int | |
## while | |
count = 0 | |
while count < 10: | |
print count, "is less than 10" | |
count +=1 | |
## another example | |
for i in range(10): | |
print "i =", i | |
if i == 5: | |
break | |
print "done looping" | |
# Functions | |
def myFunction(name): | |
print "Hello %s" % name | |
myFunction('Juan') # return "Hello Juan" | |
## another example | |
def a(adj="thirsty", name="juan"): | |
print "the %s %s ate all the pizza" % (adj, name) | |
a() # return "the thirsty juan ate all the pizza" | |
## another example | |
def shoppingCart(itemName, *avgPrices): | |
for price in avgPrices: | |
print 'price:', price | |
shoppingCart('computer', 100, 120) # return (100, 120) | |
## another example | |
def shoppingCart(itemName, **avgPrices): | |
for price in avgPrices: | |
print price, 'price: ', avgPrices[price] | |
shoppingCart('computer', amazon=100, ebay=120) # return 'amazon price: 100 ebay price: 120' | |
## another example | |
def dbLookup(): | |
dict = {} | |
dict['amazon'] = 100 | |
dict['ebay'] = 120 | |
dict['bestBuy'] = 34 | |
return dict | |
def shoppingCart(itemName, avgPrices): | |
print 'item', itemName | |
for price in avgPrices: | |
print price, 'price: ', avgPrices[price] | |
shoppingCart('computer', dbLookup()) | |
# return | |
# item: computer | |
# amazon price: 100 | |
# ebay price: 120 | |
# bestBuy price: 34 | |
# OOP | |
class house: | |
doors = 10 | |
__cost = 12000 # this attr is private because the two _ before the name, so no one can access it | |
def addDoors(self, number): | |
self.doors = number | |
def slamDoors(self): | |
for door in range(self.doors) # this will create a list | |
print "SLAM!" | |
myHouse = house() | |
print myHouse.doors # return 10 | |
myHouse.doors = 5 | |
print myHouse.doors # return 5 | |
myHouse.slamDoors() | |
# return | |
# SLAM! | |
# SLAM! | |
# SLAM! | |
# SLAM! | |
# SLAM! | |
myHouse.addDoors(2) | |
myHouse.slamDoors() | |
# return | |
# SLAM! | |
# SLAM! | |
## another example | |
class house: | |
# Constructor | |
def __init__(self): | |
self.doors = 4 | |
def slamdDoors(self): | |
for door in range(self.doors): | |
print "SLAM!" | |
# extends house, we already have access to all the attrs and methods of house | |
class castle(house): | |
def fireCannons(self, number) | |
for cannon in range(number) | |
print "firing cannon number", cannon, " boom!" | |
myHouse = house() | |
print myHouse.doors | |
myHouse.fireCannons(2) | |
# firing cannon number 0 | |
# firing cannon number 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment