python3.6 -m venv
# setting up variables… easy my_variable = "deez nuts" your_mama_is_fat = True c = "cats" # 'c' """ MULTILINE COMMENT BITCH """
import math # Imports the math module everything = dir(math) # Sets everything to a list of things from math print everything # Prints 'em all!
Methods that use dot notation only work with strings. On the other hand,
str() can work on other data types. Sometimes you need to combine a string with something that isn't a string. In order to do that, you have to convert the non-string into a string.
You can also format strings with out concatenating by using
name = raw_input("What is your name?") quest = raw_input("What is your quest?") color = raw_input("What is your favorite color?") print "Ah, so your name is %s, your quest is %s, \ and your favorite color is %s." % (name, quest, color)
There are also some builtin math functions:
from datetime import datetime now = datetime.now() current_year = now.year current_month = now.month current_day = now.day print now print current_year print current_month print current_day print '%s/%s/%s' % (now.month, now.day, now.year) print '%s:%s:%s' % (now.hour, now.minute, now.second)
def shut_down(s): if s.lower() == ('yes' or 'y'): return 'Shutting down' elif s.lower() == ('no' or 'n'): return 'Shutdown aborted' else: return 'Sorry'
- Boolean operators (in order of operations):
Most importantly, you need to know if-else.
if 8 > 9: print "I don't get printed!" elif 8 < 9: print "I get printed!" else: print "I also don't get printed!"
You also really need to understand for loops.
Lists & Dictionaries
zoo_animals = ["pangolin", "cassowary", "sloth", "me" ]; zoo_animals.append("tortoise")
You can also use the slice method on strings.
animals = "catdogfrog" cat = animals[:3] # The first three characters of animals dog = animals[3:6] # The fourth through sixth characters frog = animals[6:] # From the seventh character to the end
f = open(‘work file’, ‘w’) mode: w = write, r = read f.read() f.readline() f.close()
Debugging & Errors
pdb.set_trace() # c - continue
Especially worth considering ipdb for iPython. This features tab completions and object introspection . It also features food syntax highlighting