Tuesday, March 15, 2016

Symbol Review

t's time to review the symbols and Python words you know and to try to pick up a few more for the next few lessons. I have written out all the Python symbols and keywords that are important to know.
In this lesson take each keyword and first try to write out what it does from memory. Next, search online for it and see what it really does. This may be difficult because some of these are difficult to search for, but try anyway.
If you get one of these wrong from memory, make an index card with the correct definition and try to "correct" your memory.
Finally, use each of these in a small Python program, or as many as you can get done. The goal is to find out what the symbol does, make sure you got it right, correct it if you do not, then use it to lock it in.

Keywords

KEYWORDDESCRIPTIONEXAMPLE
andLogical and.True and False == False
asPart of the with-as statement.with X as Y: pass
assertAssert (ensure) that something is true.assert False, "Error!"
breakStop this loop right now.while True: break
classDefine a class.class Person(object)
continueDon't process more of the loop, do it again.while True: continue
defDefine a function.def X(): pass
delDelete from dictionary.del X[Y]
elifElse if condition.if: X; elif: Y; else: J
elseElse condition.if: X; elif: Y; else: J
exceptIf an exception happens, do this.except ValueError, e: print e
execRun a string as Python.exec 'print "hello"'
finallyExceptions or not, finally do this no matter what.finally: pass
forLoop over a collection of things.for X in Y: pass
fromImporting specific parts of a module.import X from Y
globalDeclare that you want a global variable.global X
ifIf condition.if: X; elif: Y; else: J
importImport a module into this one to use.import os
inPart of for-loops. Also a test of X in Y.for X in Y: pass also 1 in [1] == True
isLike == to test equality.1 is 1 == True
lambdaCreate a short anonymous function.s = lambda y: y ** y; s(3)
notLogical not.not True == False
orLogical or.True or False == True
passThis block is empty.def empty(): pass
printPrint this string.print 'this string'
raiseRaise an exception when things go wrong.raise ValueError("No")
returnExit the function with a return value.def X(): return Y
tryTry this block, and if exception, go to except.try: pass
whileWhile loop.while X: pass
withWith an expression as a variable do.with X as Y: pass
yieldPause here and return to caller.def X(): yield Y; X().next()

Data Types

For data types, write out what makes up each one. For example, with strings write out how you create a string. For numbers write out a few numbers.
TYPEDESCRIPTIONEXAMPLE
TrueTrue boolean value.True or False == True
FalseFalse boolean value.False and True == False
NoneRepresents "nothing" or "no value".x = None
stringsStores textual information.x = "hello"
numbersStores integers.i = 100
floatsStores decimals.i = 10.389
listsStores a list of things.j = [1,2,3,4]
dictsStores a key=value mapping of things.e = {'x': 1, 'y': 2}

String Escape Sequences

For string escape sequences, use them in strings to make sure they do what you think they do.
ESCAPEDESCRIPTION
\\Backslash
\'Single-quote
\"Double-quote
\aBell
\bBackspace
\fFormfeed
\nNewline
\rCarriage
\tTab
\vVertical tab

String Formats

Same thing for string formats: use them in some strings to know what they do.
ESCAPEDESCRIPTIONEXAMPLE
%dDecimal integers (not floating point)."%d" % 45 == '45'
%iSame as %d."%i" % 45 == '45'
%oOctal number."%o" % 1000 == '1750'
%uUnsigned decimal."%u" % -1000 == '-1000'
%xHexadecimal lowercase."%x" % 1000 == '3e8'
%XHexadecimal uppercase."%X" % 1000 == '3E8'
%eExponential notation, lowercase 'e'."%e" % 1000 == '1.000000e+03'
%EExponential notation, uppercase 'E'."%E" % 1000 == '1.000000E+03'
%fFloating point real number."%f" % 10.34 == '10.340000'
%FSame as %f."%F" % 10.34 == '10.340000'
%gEither %f or %e, whichever is shorter."%g" % 10.34 == '10.34'
%GSame as %g but uppercase."%G" % 10.34 == '10.34'
%cCharacter format."%c" % 34 == '"'
%rRepr format (debugging format)."%r" % int == "<type 'int'>"
%sString format."%s there" % 'hi' == 'hi there'
%%A percent sign."%g%%" % 10.34 == '10.34%'

Operators

Some of these may be unfamiliar to you, but look them up anyway. Find out what they do, and if you still can't figure it out, save it for later.
OPERATORDESCRIPTIONEXAMPLE
+Addition2 + 4 == 6
-Subtraction2 - 4 == -2
*Multiplication2 * 4 == 8
**Power of2 ** 4 == 16
/Division2 / 4.0 == 0.5
//Floor division2 // 4.0 == 0.0
%String interpolate or modulus2 % 4 == 2
<Less than4 < 4 == False
>Greater than4 > 4 == False
<=Less than equal4 <= 4 == True
>=Greater than equal4 >= 4 == True
==Equal4 == 5 == False
!=Not equal4 != 5 == True
<>Not equal4 <> 5 == True
( )Parenthesislen('hi') == 2
[ ]List brackets[1,3,4]
{ }Dict curly braces{'x': 5, 'y': 10}
@At (decorators)@classmethod
,Commarange(0, 10)
:Colondef X():
.Dotself.x = 10
=Assign equalx = 10
;semi-colonprint "hi"; print "there"
+=Add and assignx = 1; x += 2
-=Subtract and assignx = 1; x -= 2
*=Multiply and assignx = 1; x *= 2
/=Divide and assignx = 1; x /= 2
//=Floor divide and assignx = 1; x //= 2
%=Modulus assignx = 1; x %= 2
**=Power assignx = 1; x **= 2

No comments:

Post a Comment